Is it possible to have a node_modules directory shared between projects
Asked Answered
E

4

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?

Encipher answered 4/5, 2015 at 10:21 Comment(0)
S
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)

Suzetta answered 4/5, 2015 at 10:39 Comment(1)
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.Encipher
C
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.

Craniate answered 31/7, 2021 at 6:37 Comment(0)
A
0

Edit: ddprt

On Windows

mklink /D node_modules "C:/fullPATH/cache/node_modules"
Androw answered 6/12, 2021 at 15:58 Comment(0)
C
-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.

Coggins answered 4/5, 2015 at 10:33 Comment(2)
Gulp needs both a local and a global installation. That's part of their tooling process.Suzetta
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.Encipher

© 2022 - 2024 — McMap. All rights reserved.