Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

single node_modules folder for multiple projects

I have a list of websites like this:

project1/site
project2/site
project4/site
...
projectN/site

Each site folder has inside node_modules folder, gruntfile.js file and a package.json file. So they look like this:

project1/site/gruntfile.js
project1/site/package.json
project1/site/node_modules/
...
projectN/site/gruntfile.js
projectN/site/package.json
projectN/site/node_modules/

Everything is working fine with my tasks, plugins and stuff.

I am wondering if I can work this out with just one node_modules folder on my root folder as my websites are using similar tasks.

project1/site/
project2/site/
...
projectN/site/
/node_modules/

Is this even possible? What do I need to do?

like image 746
Miguel Garrido Avatar asked Jan 28 '15 02:01

Miguel Garrido


People also ask

Do I need to install node modules for every project?

We do not need to install a module every time when installed globally. It takes less memory as only one copy is installed. We can make . js scripts and run them anywhere without having a node_modules folder in the same directory when packages are installed globally.

Can I copy node_modules folder to another project?

Yes you can copy whole node_modules (have done it multiple times) from one project to another and use same package. json and package-lock (Will only save time in dependencies installation/download)

What is .staging folder in node_modules?

While doing npm install, inside node_modules . staging folder is getting created. Reasons: This is a temporary folder where the modules will be kept untill npm downloads all the modules specified in the package. json.


1 Answers

You can use the -g flag with npm to install modules globally, as in: npm install -g <modulename> This will install all the modules so they are accessible to all your node projects.

And then you can create a link for each of the projects to the required modules using npm link, as explained in the link @jakerella referred to:

...there are some cases where you want to do both. Coffee-script and Express both are good examples of apps that have a command line interface, as well as a library. In those cases, you can do one of the following:

  1. Install it globally, and then npm link coffee-script or npm link express (if you’re on a platform that supports symbolic links.) Then you only need to update the global copy to update all the symlinks as well.

See the npm documentation.

like image 173
Ozk Avatar answered Sep 19 '22 17:09

Ozk