简体   繁体   English

是否可以在项目之间共享 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

Now I want to execute the gulpfile in the project directories:现在我想在项目目录中执行 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

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.在两个项目文件夹中,gulpfile 是相似的,并且使用了一组相似的插件。 I'd really like to have the dependencies only once (because potentially I have up to 25 projects sharing the same node_modules).我真的很想拥有一次依赖项(因为我可能有多达 25 个项目共享相同的 node_modules)。 Is this setup possible, or does the seperate project directories need to have their own node_modules folders?这种设置是否可行,或者单独的项目目录是否需要有自己的 node_modules 文件夹?

Gulp requires you to have both a global installation as well as a local one. Gulp要求您同时拥有全局安装和本地安装。 So you need to have your Gulp relatively to your Gulpfile. 所以你需要将Gulp相对于你的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. 如果你的package.json将位于workspace并且你的node_modules将位于workspace/node_modules一切都可以正常工作,因为Node的搜索树,但是如果你不能移动它们,那么让它工作的唯一方法就是“伪造” node_modules文件夹。

You can do this by creating a symbolic link. 您可以通过创建符号链接来完成此操作。

Here's on Unix/Linux/Mac: 这是在Unix / Linux / Mac上:

ln -s ../cache/node_modules node_modules

Here's on Windows 这是在Windows上

mklink /D node_modules ../cache/node_modules

(the latter one might work different, I'm not on a Win machine) (后者可能会有所不同,我不是在Win机器上)

You could also try pkglink你也可以试试pkglink

From description:来自描述:

Space saving Node.js package hard linker.节省空间的 Node.js 包硬链接器。 pkglink locates common JavaScript/Node.js packages from your node_modules directories and hard links the package files so they share disk space. pkglink 从 node_modules 目录中定位常见的 JavaScript/Node.js 包,并硬链接包文件,以便它们共享磁盘空间。

Edit: ddprt编辑:ddprt

On Windows在 Windows 上

mklink /D node_modules "C:/fullPATH/cache/node_modules"

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. 你可以随时使用'-g'参数和npm install'package-name',以便使模块可以全局访问不同的项目。

See the following links 请参阅以下链接

  1. what does the "-g" flag do in the command "npm install -g <something>"? “-g”标志在命令“npm install -g <something>”中做了什么?
  2. How do I install a module globally using npm? 如何使用npm全局安装模块?
  3. https://docs.npmjs.com/files/folders https://docs.npmjs.com/files/folders

Packages are dropped into the node_modules folder under the prefix. 包被放入前缀下的node_modules文件夹中。 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. 在本地安装时,这意味着您可以要求(“packagename”)加载其主模块,或者要求(“packagename / lib / path / to / sub / module”)加载其他模块。

Global installs on Unix systems go to {prefix}/lib/node_modules. Unix系统上的全局安装转到{prefix} / lib / node_modules。 Global installs on Windows go to {prefix}/node_modules (that is, no lib folder.) Windows上的全局安装转到{prefix} / node_modules(即没有lib文件夹。)

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, eg npm install @myorg/package would place the package in {prefix}/node_modules/@myorg/package. Scoped软件包以相同的方式安装,除非它们在相关node_modules文件夹的子文件夹中组合在一起,其名称为@符号的范围前缀,例如npm install @ myorg / package会将软件包放在{prefix}中/ node_modules / @ MYORG /包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM