Why is npm used for client-side code?


(smchughinfo) #1

I’ve always used npm for node.js code. But I’m learning ASP.NET Core MVC and they seem to be recommending npm for client-side libraries. Well, you can imagine my surprise. npm in an ASP.NET project for client-side code. Why not!

I’m not really sure what purpose npm is serving here. What does it do other than be a json file that lists what JavaScript and CSS files the site uses? Also, how do you know if a package is for the browser or for node.js? For those that know ASP.NET Core MVC what if I want a JavaScript library that isn’t in npm? Do I just like download that file manually and/or link to it in my code? Then why not just do that all the time?


Why was my question flagged?
(Williwyg) #2

It depends on how you want to build your clientside GUI.

In modern web applications the GUI often is completely build clientside with Javascript. (Single Page Application) The server only defines endpoints to read and write data mostly in the json format. No html comming from the server any more.

In this scenario you typically use npm packages to serve as building blocks for your client side code.

This architecture feels like rich-client development with clear separation of concerns. Ones you are used to this way of working you will never go back to server side rendering.


(smchughinfo) #3

Thanks for the reply. Is npm becoming more ubiquitous across different types of web serverrs? Like ruby and php? When you use npm in a node project do you have a seperate node_modules directory for client-side and a server-side code? In ASP.NET Core MVC they seem to be recommending putting node_modules in the root directory and then using webpack to push/manipulate code into the public directory.

Is npm becoming THE repository for client-side code sort of like steam is THE (main) repository for games?


(Williwyg) #4

Hi,

Because I have a strong Microsoft oriented background my preferred development tool is Visual Studio. Support for building web client applications in Visual Studio is very well supported. Typically I separate Client side code and Server side code completely into separate projects because they are based on completely different technology. So each project will have its own dependencies. The Client side project is based on the Node.js template and server side project base on a template you prefer (WCF, Webapi, .net core or ……Whatever you prefer). Server side you simply need something that can handle http requests and serve json.

If you want to have an example of the client side code have a look at https://github.com/williwyg/jpf
This is a small library I am working on at the moment. This might give you an idee howto structure your client side code.


(smchughinfo) #5

Thanks for the reply. Good luck with your project. At the moment I only need npm for the client-side since I’m learning about all this while learning ASP.NET Core MVC so client/server structure doesn’t matter AS much right now. I’m mostly curious about how and why npm has transcended node.js.


(Frédéric Harper) #6

@smchughinfo: I answered your question about your post being flagged in your other support question.

As for this one, I moved it to the development category as it’s more of a discussion that an issue with the platform.

Lastly, I feel that the great answers from @williwyg are what you needed, but if you want more information and continue the discussion, feel free to ask more question in this thread.


(smchughinfo) #7

Can you please just delete this question. It’s actually still entirely unanswered as far as I am concerned but I do appreciate @williwyg’s reply.


(Frédéric Harper) #8

If you don’t think it’s still useful for you, maybe it can be for others? I also would find it sad to throw away the time @williwyg spent trying to answer your question as there is valuable information in his answers.

Why not try to clarify your question @smchughinfo so we can help you?


(Williwyg) #9

@smchughinfo

I think your feeling that your question has not being answered might come from the fact that you do not have a clear view on Single Page Application Technology vs Server side rendering. If you are still living in the MVC server side rendering world than it is difficult to grasp the idee of why npm packages are important.

I would suggest you to read more about SPA technology to figure out the differences. As I already said once you get familiar with client side rendering and its advantages you probably never wonna go back to server side rendering.


(system) closed #10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.