是否可以在项目之间共享 node_modules 目录
Is it possible to have a node_modules directory shared between projects
我的项目设置如下:
workspace
└cache
└node_modules
└gulp (and gulp-plugins, express etc.)
└nodejs
└node.exe
└project1
└gulpfile.js
└project2
└gulpfile.js
现在我想在项目目录中执行 gulpfile:
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
在两个项目文件夹中,gulpfile 是相似的并且使用了相似的插件集。我真的很想只拥有一次依赖项(因为我可能有多达 25 个项目共享相同的 node_modules)。
这种设置是否可行,或者单独的项目目录是否需要有自己的 node_modules 文件夹?
您始终可以在 npm install 'package-name' 中使用“-g”参数,以使该模块在全球范围内可用,以便跨不同的项目访问。
查看以下链接
- what does the "-g" flag do in the command "npm install -g <something>"?
- How do I install a module globally using npm?
- 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.
Gulp 要求您同时进行全局安装和本地安装。所以你需要让你的 Gulp 相对于你的 Gulp 文件。如果你的 package.json
位于 workspace
而你的 node_modules
位于 workspace/node_modules
一切都会正常工作,因为 Node 的搜索树,但如果你不能移动它们,使其工作的唯一方法是 "fake" node_modules
文件夹。
您可以通过创建符号 link.
这是 Unix/Linux/Mac:
ln -s ../cache/node_modules node_modules
这里是 Windows
mklink /D node_modules ../cache/node_modules
(后一个可能工作不同,我不是在 Win 机器上)
你也可以试试pkglink
来自描述:
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.
编辑:ddprt
在 Windows
mklink /D node_modules "C:/fullPATH/cache/node_modules"
我的项目设置如下:
workspace
└cache
└node_modules
└gulp (and gulp-plugins, express etc.)
└nodejs
└node.exe
└project1
└gulpfile.js
└project2
└gulpfile.js
现在我想在项目目录中执行 gulpfile:
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
在两个项目文件夹中,gulpfile 是相似的并且使用了相似的插件集。我真的很想只拥有一次依赖项(因为我可能有多达 25 个项目共享相同的 node_modules)。 这种设置是否可行,或者单独的项目目录是否需要有自己的 node_modules 文件夹?
您始终可以在 npm install 'package-name' 中使用“-g”参数,以使该模块在全球范围内可用,以便跨不同的项目访问。
查看以下链接
- what does the "-g" flag do in the command "npm install -g <something>"?
- How do I install a module globally using npm?
- 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.
Gulp 要求您同时进行全局安装和本地安装。所以你需要让你的 Gulp 相对于你的 Gulp 文件。如果你的 package.json
位于 workspace
而你的 node_modules
位于 workspace/node_modules
一切都会正常工作,因为 Node 的搜索树,但如果你不能移动它们,使其工作的唯一方法是 "fake" node_modules
文件夹。
您可以通过创建符号 link.
这是 Unix/Linux/Mac:
ln -s ../cache/node_modules node_modules
这里是 Windows
mklink /D node_modules ../cache/node_modules
(后一个可能工作不同,我不是在 Win 机器上)
你也可以试试pkglink
来自描述:
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.
编辑:ddprt
在 Windows
mklink /D node_modules "C:/fullPATH/cache/node_modules"