是否可以在项目之间共享node_modules目录

时间:2015-05-04 10:21:46

标签: node.js gulp

我的项目设置如下:

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文件夹?

3 个答案:

答案 0 :(得分:6)

Gulp要求您同时拥有全局安装和本地安装。所以你需要将Gulp相对于你的Gulpfile。如果您的package.json位于workspacenode_modules位于workspace/node_modules,那么一切都会因为Node的搜索树而正常运行,但如果可以&# 39;移动它们,唯一能让它发挥作用的方法就是“假装”#34; node_modules文件夹。

您可以通过创建符号链接来完成此操作。

在Unix / Linux / Mac上:

ln -s ../cache/node_modules node_modules

Windows上的

mklink /D node_modules ../cache/node_modules

(后者可能有所不同,我不在Win机器上)

答案 1 :(得分:0)

你也可以试试pkglink

来自描述:

<块引用>

节省空间的 Node.js 包硬链接器。 pkglink 从您的 node_modules 目录中定位常见的 JavaScript/Node.js 包并硬链接包文件,以便它们共享磁盘空间。

答案 2 :(得分:-1)

你总是可以使用&#39; -g&#39;参数与npm install&#39; package-name&#39;,以便使模块可以全局访问不同的项目。

请参阅以下链接

  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
  4.   

    将包放入前缀下的node_modules文件夹中。当本地安装,这意味着,可以要求(&#34;包名&#34),以加载其主模块,或者需要(&#34;包名/ LIB /路径/到/子/模块&#34;)加载其他模块。

         

    Unix系统上的全局安装转到{prefix} / lib / node_modules。   Windows上的全局安装转到{prefix} / node_modules(即没有   lib文件夹。)

         

    Scoped软件包以相同的方式安装,除非它们已分组   一起在相关node_modules文件夹的子文件夹中   @符号的范围前缀的名称,例如npm安装   @ myorg / package会将包放入   {前缀} / node_modules / @ MYORG /包。

相关问题