Once time, that I was googling for packages to use in my project, I see this command:
npm install -g <package-name>
I knew that -g points to globally install this package, and it means that I can use this package to my all project in my system. Today I face this command again and I curious to compare that with install packages locally.
I found that in npm there are two ways to install things:
- Globally: This drops modules in
{prefix}/lib/node_modules, and puts executable files in{prefix}/bin, where{prefix}is usually something like/usr/local. - Locally: This installs your package in the current working directory. Node modules go in
./node_modules, executables go in./node_modules/.bin/.
If you want to know how to choose to install the packages globally or locally, I have to say that there is a rule:
- If you’re installing something that you want to use in your program, then install it locally, at the root of your project.
- If you’re installing something that you want to use in your shell, on the command line, or something, install it globally, so that its binaries end up in your
PATHenvironment variable.
And of course, there are some cases where you want to do both. In those cases, I recommend installing the package in both places.