Difference between ~ / .npm, $ PROJECT / node_modules and / usr / lib / node_modules?

I installed npm and when I did my first one sudo npm install some-package -g

, it installed this package in / usr / lib / node_modules as I expected, but then it also created some files in ~ / .npm. What's the difference between these locations?

The other answers here say that using a global install -g

should set it to your default home directory, but for me, it installs it in / usr / lib / node_modules, am I doing something wrong?

And when I do a local install without -g

, it gets installed to the current $ PROJECT / node_modules directory. What's the difference between all these locations and where should they go?

+3


source to share


1 answer


The system package installation directory, usually pod is /usr/lib

usually used for globally installed packages that provide a binary that must be available in yours PATH

(so that it can be executed from anywhere).

The local installation directory node_modules

created npm install

at the location you run npm

is usually found in your project directory and is typically used for project dependencies.

~/.npm

contains packages already downloaded. When installing the same package elsewhere, npm first looks for that package in that cache directory.

Link: https://docs.npmjs.com/files/folders

Associated files:

  • ... npmrc - NPM configurations in various locations
  • package.json - Packages and their versions for the project



Hypothetical scenario: Two projects using Grunt (Javascript based scripting tool):

Both projects use different versions of Grunt. One project is older. Grunt cannot be updated without having to adapt the entire build process, another project has just started.

You need to install the "grunt-cli" system (using a flag -g

) as it provides a binary grunt

. This CLI binary will look for a local "grunt" in your current project directory. On the other hand, "runt" npm installed locally (without -g

) will load the CLI. When grunt is loaded for the first project, npm will store the downloaded packages in ~/.npm

, when installing grunt for the second project, npm will look for packages common to both projects in ~/.npm

.

There are other reasons for installing packages around the world, with most of the time providing a binary that should be located in your PATH.




Alternatively, some packages that would normally need to be installed globally can also be installed locally. Then you need to add the path to that binary (for example path/to/your/node_modules/.bin/<BINARY>

) to your variable, PATH

or just give the full execution path.

+2


source







All Articles