tuvokki
tuvokki

Reputation: 740

Is it possible to have a node_modules directory shared between projects

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?

Upvotes: 5

Views: 6461

Answers (4)

whup
whup

Reputation: 11

Edit: ddprt

On Windows

mklink /D node_modules "C:/fullPATH/cache/node_modules"

Upvotes: 0

Ilya Dyachenko
Ilya Dyachenko

Reputation: 115

You could also try pkglink

From description:

Space saving Node.js package hard linker. pkglink locates common JavaScript/Node.js packages from your node_modules directories and hard links the package files so they share disk space.

Upvotes: 0

ddprrt
ddprrt

Reputation: 7584

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)

Upvotes: 9

Robin
Robin

Reputation: 864

you could always use the '-g' parameter with npm install 'package-name', so as to make the module available globally to access across different projects.

See the following links

  1. what does the "-g" flag do in the command "npm install -g <something>"?
  2. How do I install a module globally using npm?
  3. https://docs.npmjs.com/files/folders

Packages are dropped into the node_modules folder under the prefix. When installing locally, this means that you can require("packagename") to load its main module, or require("packagename/lib/path/to/sub/module") to load other modules.

Global installs on Unix systems go to {prefix}/lib/node_modules. Global installs on Windows go to {prefix}/node_modules (that is, no lib folder.)

Scoped packages are installed the same way, except they are grouped together in a sub-folder of the relevant node_modules folder with the name of that scope prefix by the @ symbol, e.g. npm install @myorg/package would place the package in {prefix}/node_modules/@myorg/package.

Upvotes: -2

Related Questions