I'm new to JS development and was wondering given that the size of node modules is an issue of concern where one has to even go on deleting the folder when many projects pile up and is not used, why can't we have a global node modules folder, like m2 for maven and then reference it in our projects?
Why do we need multiple node modules folders inside each project?
CodePudding user response:
You can install packages globally and use them with the -g flag when using npm install
.
But this isn't done often, and often isn't recommended, because the same package can have many, many different versions. SomeLibrary version 5.6.1 can be significantly different from SomeLibrary version 5.7.1. If you have multiple projects, they'll very often have different dependencies, and different dependency versions. If you try to source them all from the same global install, you'll usually run into problems very quickly. Having a separate node_modules
folder for each project solves this problem for you; often, package versioning for each project you have will "just work" without any extra configuration.
There are also managers like Yarn which can cache package downloads, so you don't re-download huge numbers of duplicate megabytes over multiple projects
CodePudding user response:
Variety of reasons. Just FYI, you can install npm packages globally using the -g flag when installing.
Here's some reasons:
- Different project may require different versions of a package.
- A single machine can consist of multiple different environment, i.e containers or virtual machines.
- Sharing code between projects/environments introduces potential security risks.
- For the above reasons, and others, you might not actually want a projects packages to leave the scope of that project.
CodePudding user response:
since java script is not compiled, we will need to have all the plugins in each deployment verses maven is compiled so we can compile it to machine code with the plugin globally installed