A just started getting familiar with npm packages and node. When installing packages they create a lot of folders in the "node modules" and i wonder if when i'll create a completely new project should i install all these packages again while having them already installed in previous projects? Or i just need to link the new projects with these packages with the package.json file?
3 Answers
You definitely shouldn't install all the packages - just those that you need.
While there are some ways to save space in node_modules, but unless it's an exceptional situation, I wouldn't start with them. It's much simpler to manage dependencies for every project independently, and it would save you a world of trouble if every project own its own dependencies and can upgrade/downgrade/replace them if needed.

- 297,002
- 52
- 306
- 350
-
Thank you. And what if i need the exact same packages i used in the previous project in the next one? For example i have 3 npm packages in the first project and i need all of them in the next one as well, should i install them again in the new project's folder? Or there is a way to link the new project with the packages already installed in the previous? – Adonxx Dec 05 '20 at 17:04
-
@Adonxx I'd just reinstall them in the second project, as you suggested – Mureinik Dec 05 '20 at 17:05
If you want to keep your project portable or make them usable for others, I you should just redownload the modules. If you link a folder on your PC and then send the code to someone else, they will not be able to execute it. BTW if you want to publish your code on Github (or something similar) then you can add node_modules to .gitignore .

- 161
- 1
- 14
It always depends on what you're wanting to build. If you know exactly which packages you need to install, then you can install all of them beforehand. But, most of the time, only "npm install" something when needed. Don't forget to add node_modules to .gitignore file too.

- 26
- 2