Why does npm install modules locally by default?

I'm new to the Node / Angular world (came from Ruby) and I'm just curious ...

Why does npm install modules locally in the default project?

I used both yeoman.io and mean.io to create two separate projects and in both cases they added a whole bunch of folders to the project directory node_modules

for grunt and karma and expression and all that stuff. I know you can add a folder to .gitignore, but that doesn't change the fact that you will likely have a whole bunch of duplicate instances of the same libraries sitting on your computer.

Why are packages added to the default local project directory? Is there a way to force npm to install globally instead of the local default?

Note. In the Ruby world, gem

(equivalently npm

) keeps track of which specific version was installed by automatically generating a "lock file". This allows people to have complete control over which version of the dependency depends on each project. I'm starting to wonder if the only reason npm doesn't install globally is because it doesn't have a "lock file" to keep track of which specific applications are being used by the application.

+3


source to share


2 answers


In the early days when num was being developed, there was a lot of discussion about this. People wanted to avoid the version incompatibility issues they encountered using Ruby, Perl, etc. In the end, it was simply decided that the simplest solution was to have no global library path, but each application must have its own library path.

This strategy is not really new. Many enterprise / mission critical Ruby, Perl and Python applications use their own compiled interpreters with custom library paths. For a long time, the standard advice has been that unless you really want clients or OS distributions to accidentally break your application, the safest method is not to use the standard installed library.



Npm accepted this as its default configuration. And when people asked why, the developers replied that it was the simplest solution, and the traditional way of doing things had no advantage in today's world of cheap terabyte hard drives.

I personally would not change the default way of working, despite the fact that I was originally one of the advocates for the shared library implementation. The Node authors of the module have assumed that the default configuration is what you have and so don't be afraid to make changes to your API.

+5


source


Npm modules are not installed globally

In ruby, this is standard for your gems to be installed along with your Ruby version.

By default, this is not the case for Node.js.



To install a module globally, you can use

$ npm install -g <module>

      

It is generally best to install packages locally, so each project can define its own dependencies.

+1


source







All Articles