Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have a node_modules directory shared between projects

Tags:

node.js

gulp

I have a project setup that is as follows:

workspace
└cache
  └node_modules
    └gulp (and gulp-plugins, express etc.)
└nodejs
  └node.exe
└project1
  └gulpfile.js
└project2
  └gulpfile.js

Now I want to execute the gulpfile in the project directories:

set NODE_PATH='C:\workspace\cache\node_modules\'
cd C:\workspace\project1\
C:\workspace\nodejs\node.exe C:\workspace\cache\node_modules\gulp\bin\gulp.js watch

and I get the following output:

[12:06:04] Local gulp not found in C:\workspace\project1
[12:06:04] Try running: npm install gulp

In both project folders the gulpfile is similar and uses a similar set of plugins. I'd really like to have the dependencies only once (because potentially I have up to 25 projects sharing the same node_modules). Is this setup possible, or does the seperate project directories need to have their own node_modules folders?

like image 299
tuvokki Avatar asked May 04 '15 10:05

tuvokki


1 Answers

Gulp requires you to have both a global installation as well as a local one. So you need to have your Gulp relatively to your Gulpfile. If your package.json would be located in workspace and your node_modules would be in workspace/node_modules everything would work fine because of Node's search tree, but if you can't move them, the only way to make it work is to "fake" the node_modules folder.

You can do this by creating a symbolic link.

Here's on Unix/Linux/Mac:

ln -s ../cache/node_modules node_modules

Here's on Windows

mklink /D node_modules ../cache/node_modules

(the latter one might work different, I'm not on a Win machine)

like image 101
ddprrt Avatar answered Sep 28 '22 04:09

ddprrt