Mandeep Singh Mandeep Singh - 13 days ago 8
Node.js Question

How to set global node modules so that all application can use same

I am new to node, i have made few small application using node, but everytime i have to use

npm install
for every application which download the required dependencies in
node_modules
folder. There are many libraries which are common.

I tried installing using
npm install express -g
but i was not sure how to use this dependency in other application which is in some other folder.

Is there any way i can have only one folder like in
D:\Users\User\AppData\Roaming\npm\node_modules
from where my all applications can have the module which they need ?

Can anyone let me know how to do the settings for the same ?

Any help would be highly appreciated !!

Answer

Every node application that has a package.json has a specific set of rules for using specific versions of it's modules. You can install globally only one version of a specific module, but if you happen to have an application that needs an older / newer version that is not installed globally on your dev environment, then it will fail to work.

The recommended way of using node modules ( packages ) is to have a local directory inside your project, which contains all libraries that the project needs. This practice is everywhere and so you should follow it.

There are some ways to mitigate the slow npm install, though.

There is a new npm-replacement, created and maintained by Facebook, called yarn.

What yarn does is it creates a local cache of all installed packages and then symlinks them to your project folder from your local computer cache. This way the npm install procedure becomes very fast.