node.js 如何使用 npm 在当前目录中安装 package.json 依赖项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8367031/
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
How do I install package.json dependencies in the current directory using npm
提问by Daniel Beardsley
I have a web app: fooapp. I have a package.jsonin the root. I want to install all the dependencies in a specific node_modules directory. How do I do this?
我有一个网络应用程序:fooapp。我package.json在根中有一个。我想将所有依赖项安装在特定的node_modules directory. 我该怎么做呢?
What I want
我想要的是
Lets say I have two widgetdependencies. I want to end up with a directory structure like this:
假设我有两个widget依赖项。我想以这样的目录结构结束:
node_modules/
widgetA
widgetB
fooapp/
package.js
lib
..
What I get
我得到的
when I run npm install fooapp/I get this:
当我跑步时,npm install fooapp/我得到了这个:
node_modules/
fooapp/
node_modules/
widgetA
widgetB
package.js
lib/
..
fooapp/
package.js
lib/
..
npm makes a copy of my app directory in the node_modules dir and installs the packages inside anothernode_modules directory.
npm 在 node_modules 目录中复制我的 app 目录,并将软件包安装在另一个node_modules 目录中。
I understand this makes sense for installing a package. But I don't require()my web app inside of something else, I run it directly. I'm looking for a simple way to install my dependencies into a specific node_modules directory.
我知道这对于安装软件包很有意义。但是我没有将require()我的网络应用程序放在其他东西中,而是直接运行它。我正在寻找一种简单的方法来将我的依赖项安装到特定的 node_modules 目录中。
回答by ireddick
Running:
跑步:
npm install
from inside your app directory (i.e. where package.json is located) will install the dependencies foryour app, rather than install it as a module, as described here. These will be placed in ./node_modules relative to your package.json file (it's actually slightly more complex than this, so check the npm docs here).
从您的应用程序目录中(即其中的package.json所在地)将安装依赖于你的应用程序,而不是将它安装为一个模块,如本文所述。这些将放置在相对于您的 package.json 文件的 ./node_modules 中(它实际上比这稍微复杂一些,因此请在此处查看npm 文档)。
You are free to move the node_modules dir to the parent dir of your app if you want, because node's 'require' mechanism understands this. However, if you want to update your app's dependencies with install/update, npm will not see the relocated 'node_modules' and will instead create a new dir, again relative to package.json.
如果需要,您可以自由地将 node_modules 目录移动到应用程序的父目录,因为节点的“要求”机制理解这一点。但是,如果您想使用安装/更新来更新应用程序的依赖项,npm 将不会看到重新定位的“node_modules”,而是会创建一个新目录,同样是相对于 package.json。
To prevent this, just create a symlink to the relocated node_modules from your app dir:
为了防止这种情况,只需从您的应用程序目录创建一个指向重新定位的 node_modules 的符号链接:
ln -s ../node_modules node_modules
回答by aesede
In my case I need to do
就我而言,我需要做
sudo npm install
my project is inside /var/www so I also need to set proper permissions.
我的项目在 /var/www 里面,所以我还需要设置适当的权限。

