5

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?

tuvokki
  • 720
  • 1
  • 10
  • 18

4 Answers4

9

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)

ddprrt
  • 7,424
  • 3
  • 29
  • 25
  • 2
    Actually you don't need to have gulp installed globally, you need it just for the commandline tools. The command I use (with the full paths to node.exe and gulp.js) is to prevent having to use a globally installed gulp. This approach works with the following createlink command on windows: `mklink /J node_modules ..\SFBC\node_modules` For the record; I'm banging my head against the table because I didn't think of this myself. Coming from a Linux/Mac environment I'm not used that the same methods apply in windows. – tuvokki May 04 '15 at 12:05
0

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.

Ilya Dyachenko
  • 95
  • 1
  • 2
  • 5
0

Edit: ddprt

On Windows

mklink /D node_modules "C:/fullPATH/cache/node_modules"
whup
  • 1
-2

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.

Community
  • 1
  • 1
Robin
  • 854
  • 8
  • 20
  • 1
    Gulp needs both a local and a global installation. That's part of their tooling process. – ddprrt May 04 '15 at 10:40
  • Thanks for the links. Maybe I had to be more explicit about the fact that I don't use a globally installed gulp here, but only a local one. That's the reson for the full pathnames in the command in the question. – tuvokki May 04 '15 at 12:07