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?
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.
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.
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.
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.
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.
The npm 1.0 release notes clarify this rationale:
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.
- 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With