node.js 在 package.json 中指定 node_modules 的路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26293049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Specify path to node_modules in package.json
提问by sturoid
Is there a way to move the node_modules directory in an application to let's say /vendor/node_modules like bower does with the bowerrc file? I thought it could be specified in package.json but I can't seem to find a solution. Your help is much appreciated.
有没有办法在应用程序中移动 node_modules 目录,让我们说 /vendor/node_modules 就像 bower 对 bowerrc 文件所做的那样?我认为它可以在 package.json 中指定,但我似乎找不到解决方案。非常感谢您的帮助。
采纳答案by xShirase
yes you can, just set the NODE_PATH env variable :
是的,你可以,只需设置 NODE_PATH 环境变量:
export NODE_PATH='yourdir'/node_modules
According to the doc :
根据文档:
If the NODE_PATH environment variable is set to a colon-delimited list of absolute paths, then node will search those paths for modules if they are not found elsewhere. (Note: On Windows, NODE_PATH is delimited by semicolons instead of colons.)
Additionally, node will search in the following locations:
1: $HOME/.node_modules
2: $HOME/.node_libraries
3: $PREFIX/lib/node
Where $HOME is the user's home directory, and $PREFIX is node's configured node_prefix.
These are mostly for historic reasons. You are highly encouraged to place your dependencies locally in node_modules folders.They will be loaded faster, and more reliably.
如果 NODE_PATH 环境变量设置为以冒号分隔的绝对路径列表,则节点将在这些路径中搜索模块,如果它们在其他地方找不到。(注意:在 Windows 上,NODE_PATH 由分号而不是冒号分隔。)
此外,节点将在以下位置进行搜索:
1:$HOME/.node_modules
2:$HOME/.node_libraries
3: $PREFIX/lib/node
其中 $HOME 是用户的主目录,$PREFIX 是节点配置的 node_prefix。
这些主要是出于历史原因。强烈建议您将依赖项本地放置在 node_modules 文件夹中。它们将被加载得更快、更可靠。
回答by David Losert
In short: It is not possible, and as it seems won't ever be supported (see here https://github.com/npm/npm/issues/775).
简而言之:这是不可能的,而且似乎永远不会得到支持(请参阅此处https://github.com/npm/npm/issues/775)。
There are some hacky work-arrounds with using the CLI or ENV-Variables (see the current selected answer), .npmrc-Config-Files or npm link- what they all have in common: They are never just project-specific, but always some kind of globalSolutions.
使用 CLI 或 ENV-Variables(请参阅当前选择的答案)、.npmrc-Config-Files 或npm link- 它们都有一些共同点:它们不仅是特定于项目的,而且总是某种类型的的global解决方案。
For me, none of those solutions are really clean because contributors to your project always need to create some special configuration or have some special knowledge - they can't just npm installand it works.
对我来说,这些解决方案都不是真正干净的,因为您的项目的贡献者总是需要创建一些特殊的配置或拥有一些特殊的知识 - 他们不能只是npm install并且它可以工作。
So: Either you will have to put your package.json in the same directory where you want your node_modules installed, or live with the fact that they will always be in the root-dir of your project.
所以:要么你必须把你的 package.json 放在你想要安装 node_modules 的同一目录中,要么接受它们总是在你项目的根目录中的事实。
回答by Rich
I'm not sure if this is what you had in mind, but I ended up on this question because I was unable to install node_modulesinside my project dir as it was mounted on a filesystem that did not support symlinks (a VM "shared" folder).
我不确定这是否是您的想法,但我最终回答了这个问题,因为我无法安装node_modules在我的项目目录中,因为它安装在不支持符号链接的文件系统(VM“共享”文件夹)上)。
I found the following workaround:
我找到了以下解决方法:
- Copy the
package.jsonfile to a temp folder on a different filesystem - Run
npm installthere - Copy the resulting
node_modulesdirectory back into the project dir, usingcp -r --dereferenceto expand symlinks into copies.
- 将
package.json文件复制到不同文件系统上的临时文件夹 - 跑
npm install那里 - 将生成的
node_modules目录复制回项目目录,cp -r --dereference用于将符号链接扩展为副本。
I hope this helps someone else who ends up on this question when looking for a way to move node_modulesto a different filesystem.
我希望这可以帮助其他人在寻找移动node_modules到不同文件系统的方法时最终解决这个问题。
Other options
其他选项
There is another workaround, which I found on the github issue that @Charminbear linked to, but this doesn't work with gruntbecause it does not support NODE_PATHas per https://github.com/browserify/resolve/issues/136:
还有另一种解决办法,这我在GitHub的问题是@Charminbear挂发现,但是这并不工作grunt,因为它不支持NODE_PATH按https://github.com/browserify/resolve/issues/136:
lets say you have
/media/sf_sharedand you can't install symlinks in there, which means you can't actually npm install from/media/sf_shared/myprojectbecause some modules use symlinks.
$ mkdir /home/dan/myproject && cd /home/dan/myproject$ ln -s /media/sf_shared/myproject/package.json(you can symlink in this direction, just can't create one inside of /media/sf_shared)$ npm install$ cd /media/sf_shared/myproject$ NODE_PATH=/home/dan/myproject/node_modules node index.js
假设你有
/media/sf_shared并且你不能在那里安装符号链接,这意味着你实际上不能 npm install from/media/sf_shared/myproject因为某些模块使用符号链接。
$ mkdir /home/dan/myproject && cd /home/dan/myproject$ ln -s /media/sf_shared/myproject/package.json(你可以在这个方向上进行符号链接,只是不能在 /media/sf_shared 中创建一个)$ npm install$ cd /media/sf_shared/myproject$ NODE_PATH=/home/dan/myproject/node_modules node index.js
回答by Vanuan
Yarn supports this feature:
Yarn 支持这个特性:
# .yarnrc file in project root
--modules-folder /node_modules
But your experience can vary depending on which packages you use. I'm not sure you'd want to go into that rabbit hole.
但是您的体验可能会因您使用的软件包而异。我不确定你是否想进入那个兔子洞。

