1

Recently I started with Angular 2 app development and I came across some problem. I have multiple projects that use certain node modules and I installed those modules with npm install in project folder. But then I started thinking: If I have a lot of projects and install node modules for each of those projects, it requires quite a lot of disk space and there are some node modules installed multiple times (in a multiple projects). So I tried to find a way to install node modules in some common folder in order to make that folder some kind of library that my projects use. But there's another problem that comes to my mind: What if different projects require different versions of the same module? How can I install multiple module versions in the same folder?

Maybe I'm thinking completely the wrong way, so I'm asking for your solutions of my problem. Is there any common way of handling node modules in development stage? Does each project have own node modules folder or is there some common folder to ? What's the most common approach (if there is one)?

Thanks!

EDIT: Found this page in Nexus Repository docs - it says that proxying npm registry is "to reduce duplicate downloads", which I'm trying to do. I already managed to create some proxy npm repository and I specified my proxy url in a .npmrc file. When I run npm install, assets and components are added in my proxy repository but that doesn't solve my problem - node modules are also installed in my project's folder. I don't quite understand how "to reduce duplicate downloads" using Nexus Repository. Can anybody explain to me how to achieve that?

peterremec
  • 488
  • 1
  • 15
  • 46
  • A good practise would be to have seperate node_modules folder for each project. If there are any packages used on a global level without any change i versions, then you can install those modules globally and acces it across all your projects.. If you definitely have to have a common node_modules folder then yu can have it but you need to configure the modules that are required by each project using a bundler like webpack or rollup – Bala Abhinav Jan 13 '17 at 08:57

1 Answers1

2

Generally there is nothing stopping you from sharing node_modules across your projects. From Node documentation:

If the module identifier passed to require() is not a core module, 
and does not begin with '/', '../', or './', then Node.js starts at 
the parent directory of the current module, and adds /node_modules, 
and attempts to load the module from that location. Node will not 
append node_modules to a path already ending in node_modules.
If it is not found there, then it moves to the parent directory, 
and so on, until the root of the file system is reached.

So it's a matter of writing correct paths whenever you refer packages.

Anatolii Gabuza
  • 6,184
  • 2
  • 36
  • 54