是否有可能在项目之间共享node_modules目录 [英] 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
现在我想在项目目录中执行gulpfile:
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
我得到以下输出:
[12:06:04] Local gulp not found in C:\workspace\project1
[12:06:04] Try running: npm install gulp
在两个项目文件夹中,gulp文件都是相似的,并使用一组类似的插件。我真的很想只有一次依赖(因为可能有多达25个项目共享相同的node_modules)。
这种设置是可能的,还是单独的项目目录需要有自己的node_modules文件夹?
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?
推荐答案
Gulp要求您同时拥有全局安装和本地安装。所以你需要让你的Gulp相对于你的Gulp文件。如果您的 package.json
位于工作区
和您的 node_modules
会在 workspace / node_modules
中,因为Node的搜索树,一切都会正常工作,但是如果你不能移动它们,唯一的方法就是假的 node_modules
文件夹。
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.
在Unix / Linux / Mac上:
Here's on Unix/Linux/Mac:
ln -s ../cache/node_modules node_modules
这是在Windows上的
Here's on Windows
mklink /D node_modules ../cache/node_modules
(后一个可能工作不同,我不在Win机器上)
(the latter one might work different, I'm not on a Win machine)
这篇关于是否有可能在项目之间共享node_modules目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!