Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is installing things globally from npm a bad practice?

I'm new to npm here, so maybe I don't get something. I understand that npm can install modules in an npm_modules directory that is local to the project, or by using --global you can install it in a machine-wide location.

Other than for some temporary convenience, why would you install any package globally? For example, I see all sorts of npm configurations / setups that do a global install of typescript. But if I have 5 projects on my machine and 3 of them use different versions of typescript, that is not good...right?

My experience with package management is from the Java/Maven world where all modules are installed in a global location (~/.m2/repository), but to reference ANYTHING (as a cmd/tool/plugin or as a dependency) you need to specify the version number. Thus you get the best of both worlds -- elimination of duplicate package installations and perfectly reproducible builds. I would have thought npm would, in its own way, accomplish the same thing.

What am I missing?

like image 634
HDave Avatar asked Oct 03 '16 14:10

HDave


People also ask

Should you install NPM packages globally?

Tip: If you are using npm 5.2 or higher, we recommend using npx to run packages globally. Installing a package globally allows you to use the code in the package as a set of tools on your local computer.

Is it bad to install packages globally?

Many node packages and tools will encourage you to install their tools globally. This is a bad practice and should be avoided. Some examples of this are Angular, Grunt, Gulp, Karma, Verdaccio, Snyk, React Native.

Can you install npm globally?

Install Package GloballyNPM 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.

Should I install node locally or globally?

In general, the rule of thumb is: If you're installing something that you want to use in your program, using require('whatever') , then install it locally, at the root of your project.


3 Answers

If you are working with multiple tools for transforming your code (Typescript, webpack, babel) and you intend to work with different packages locally and link them together, I would definitely NOT recommend installing things globally, you will suffer a lot when updating any global component. Even webpack team discourage installing globally the second version:

The webpack command is now available globally.

However, this is not a recommended practice. This locks you down to a specific version of webpack and might fail in projects that use a different version.

like image 127
Paulo Madroñero Avatar answered Sep 17 '22 23:09

Paulo Madroñero


The npm 1.0 release notes clarify this rationale:

In general, the rule of thumb is:

  1. If you’re installing something that you want to use in your program, using require('whatever'), then install it locally, at the root of your project.
  2. 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 PATH environment variable.
like image 37
msanford Avatar answered Sep 20 '22 23:09

msanford


Maven installs nothing globally. It keeps a local "global" repository to prevent having to download everything for every new build, but every project has its own versions of all libraries you define in its pom file.

For npm, typically you'd install some tooling using it globally, like grunt-cli and karma-cli, then in the package.json for each project define which modules/libraries are needed in which version for that project.

like image 45
jwenting Avatar answered Sep 19 '22 23:09

jwenting