[英]Centralised node_modules
I would like to have my Node modules stored in a centralised place, in say, /var/http/common/
and have my app live/run in a different location, say /var/http/www/apps/APP#1_NAME/
. 我想将我的Node模块存储在一个集中的位置,例如
/var/http/common/
并让我的应用程序在其他位置运行/运行,例如/var/http/www/apps/APP#1_NAME/
。
I was able to set up the requires in server.js
to use relative paths like require('../../../common/express')
, but from reading posts by NPM's author, it sounds like I'm hacking it, and I should use npm link
to create a "local" reference for Node (which symlinks to the real installation). 我能够在
server.js
设置require('../../../common/express')
以使用诸如require('../../../common/express')
类的相对路径,但是从阅读NPM作者的帖子时 ,听起来好像我在黑客我应该使用npm link
为Node创建一个“本地”引用(符号链接到实际安装)。
I first installed my node modules in /var/http/common/
, but then when I tried to create the symlink ( npm link ../../../common/node_modules/express
), npm seems to have treated it like a "global" install, and re-installed express in /usr/local/lib/node_modules/express
(and created a "local" symlink to it ./node_modules/express ->
) which is not what I expected to happen. 我首先在
/var/http/common/
安装了我的节点模块,但是当我尝试创建符号链接( npm link ../../../common/node_modules/express
)时,npm似乎将其视为一个“全局”安装,然后在/usr/local/lib/node_modules/express
重新安装了express(并创建了一个到它的“本地”符号链接./node_modules/express ->
),这不是我期望的。 Is this what I actually want? 这是我真正想要的吗? Should I have used
npm config set prefix
first? 我应该首先使用
npm config set prefix
吗?
Turns out: I should have set npm config set prefix
before doing anything else. 原来:在执行其他操作之前,我应该已经设置了
npm config set prefix
。
It might appear that npm link
and npm install -g
do the same thing; 看起来
npm link
和npm install -g
做同样的事情。 however whilst, npm link
will install the module globally, it also creates a local symbolic link in node_modules
pointing to $prefix/lib/node_modules/$module
. 但是,尽管
npm link
将在全局安装模块,但它还会在node_modules
中node_modules
指向$prefix/lib/node_modules/$module
的本地符号链接。 MaxGfeller is incorrect: Without that local symlink, Node will complain that it cannot find the (globally installed) modules. MaxGfeller不正确:如果没有该本地符号链接,Node将抱怨它找不到(全局安装的)模块。 This is determined thru my own attempts as well as is inferred from
npm help folders
: 这是通过我自己的尝试确定的,也可以从
npm help folders
推论得出:
• Install it locally if you're going to require() it.
•如果需要require() ,请在本地安装。
• Install it globally if you're going to run it on the command line .
•如果要在命令行上运行它,请全局安装它。
• If you need both , then install it in both places, or use npm link .
•如果两者都需要,则将其安装在两个地方,或使用npm link 。
This doesn't specifically address what I asked: I want to have the modules stored in a central location (to be accessed by multiple Node Applications) but I don't care about using them from the command like—I only want to use them in require('')
. 这并没有专门解决我的要求:我想将模块存储在一个中央位置(以供多个节点应用程序访问),但是我不关心从以下命令中使用它们,我只想使用它们在
require('')
。
As for my question about using relative paths in require('')
, I still have not got/found an authoritative answer for that, but it would seem from the existance of npm link
that using rel paths is not the author's intent. 至于我关于在
require('')
使用相对路径的问题,我仍然没有得到/找到一个权威的答案,但是从npm link
的存在看来,使用rel路径并不是作者的意图。 To me it seems like a case of six-of-one , but I would like to remain consistent with Node's standard. 对我来说,这似乎是的情况下六中一 ,但我想保持与节点的标准是一致的。
You can install your node modules globally using by adding '-g' to the command. 您可以通过在命令中添加“ -g”来全局安装节点模块。 For example:
例如:
npm install express -g
In your code you can use it normally: 在您的代码中,您可以正常使用它:
require('express');
The modules are then stored in /usr/local/lib/node_modules 然后将模块存储在/ usr / local / lib / node_modules中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.