Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does npm install modules locally by default?

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

Why does npm install modules locally in a project by default?

I've used both yeoman.io and mean.io to create two separate projects, and in both cases they end up adding a whole bunch of folders to the project's node_modules directory for grunt and karma and express and all that stuff. I know you can add the folder to .gitignore, but that doesn't change the fact that you'll likely have a whole bunch of duplicate instances of the same libraries sitting around on your computer.

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

Note: In the Ruby world, gem (equivalent to npm) keeps track of which specific version was installed by auto-generating a "lock file". This allows people to have complete control over which version of a dependency on a per-project basis. I'm beginning to wonder if the only reason why npm doesn't install globally is because it doesn't have a "lock file" to keep track of which specific subversions the app is using.

like image 975
Jon Lemmon Avatar asked Oct 16 '14 05:10

Jon Lemmon


People also ask

Does npm install locally by default?

It's best to install locally when relying on a package from your module, such as Node. js. This is how npm install works by default.

Where does npm install by default?

The npm packages installed locally will always be installed on the node_modules/ folder right in your current working directory. For example, when you're on a directory called project/ from the terminal, then it will be inside the project/node_modules folder.

Is npm install global or local?

local packages are installed in the directory where you run npm install <package-name> , and they are put in the node_modules folder under this directory. global packages are all put in a single place in your system (exactly where depends on your setup), regardless of where you run npm install -g <package-name>

Does npm install globally?

NPM can also install packages globally so that all the node. js application on that computer can import and use the installed packages. NPM installs global packages into /<User>/local/lib/node_modules folder. Apply -g in the install command to install package globally.


1 Answers

In the early days when npm was being developed there were a lot of discussions about this. People wanted to avoid problems with version incompatibility that they experienced using Ruby, Perl etc. In the end it was simply decided that the simplest solution is to not have a global library path at all but for each application to have its own library path.

This strategy is actually not new. A lot of enterprise/mission critical installs of Ruby, Perl and Python use custom compiled interpreters with a custom library path. For a long time the standard advise has been that if you really don't want customers or OS distro accidentally breaking your app the most robust technique is to not use the standard installed library.

Npm took this as the default configuration. And when people asked "why?", the developers answered that it was the simplest solution and the traditional way of doing things had no advantages in today's world of cheap terabyte sized hard disks.

I personally wouldn't change the default way npm works even though I was originally one of those who argued for the shared library implementation. Node module authors have assumed the default configuration is what you have and are therefore not afraid to make breaking changes to their API.

like image 65
slebetman Avatar answered Oct 07 '22 06:10

slebetman