Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make multiple projects share node_modules directory?

Tags:

node.js

npm

Whenever I make projects, I have to download all dependencies of node modules. Without copying the node_modules, Is there anyway to share the central node_modules in multiple projects?

like the followings, I have to run many commands every time..

npm install gulp-usemin                                                                         npm install gulp-wrap npm install gulp-connect npm install gulp-watch npm install gulp-minify-css npm install gulp-uglify npm install gulp-concat npm install gulp-less npm install gulp-rename npm install gulp-minify-html 
like image 867
verystrongjoe Avatar asked Apr 22 '15 02:04

verystrongjoe


People also ask

Can I copy node_modules folder to another project?

You can copy but it will be of no use, its as simple as copy the package. json entry for "react-native-fetch-blob" and do npm install. Also you can do "npm install react-native-fetch-blob@<version>" if you want a specific version else latest will be installed.

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.


1 Answers

You absolutely can share a node_modules directory amongst projects.

From node's documentation:

If the module identifier passed to require() is not a native module, and does not begin with '/', '../', or './', then node starts at the parent directory of the current module, and adds /node_modules, and attempts to load the module from that location.

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.

For example, if the file at '/home/ry/projects/foo.js' called require('bar.js'), then node would look in the following locations, in this order:

/home/ry/projects/node_modules/bar.js /home/ry/node_modules/bar.js /home/node_modules/bar.js /node_modules/bar.js

So just put a node_modules folder inside your projects directory and put in whatever modules you want. Just require them like normal. When node doesn't find a node_modules directory in your project folder, it will check the parent folder automatically. So make your directory structure like this:

-myProjects --node_modules --myproject1 ---sub-project --myproject2 

So like this, even your sub-project's dependencies can draw on your main node_modules repository.

One drawback to doing it this way is you will have to build out your package.json file manually (unless someone knows a way to automate this with grunt or something). When you install your packages and add the --save arg to an npm install command it automatically appends it to the dependencies section or your package.json, which is convenient.

like image 50
tpie Avatar answered Sep 24 '22 23:09

tpie