如何使用npm在当前目录中安装package.json依赖项 [英] How do I install package.json dependencies in the current directory using npm
问题描述
我有一个网络应用程序: fooapp 。根目录中有一个 package.json
我想在一个特定的 node_modules目录
中安装所有依赖项。我该怎么做?
I have a web app: fooapp. I have a package.json
in the root. I want to install all the dependencies in a specific node_modules directory
. How do I do this?
说我有两个小部件
依赖关系。我想最终得到一个这样的目录结构:
Lets say I have two widget
dependencies. I want to end up with a directory structure like this:
node_modules/
widgetA
widgetB
fooapp/
package.js
lib
..
我获得
当我运行 npm install fooapp /
我得到这个:
node_modules/
fooapp/
node_modules/
widgetA
widgetB
package.js
lib/
..
fooapp/
package.js
lib/
..
npm在node_modules目录中复制我的应用程序目录,并在另一个 node_modules目录中安装软件包。
npm makes a copy of my app directory in the node_modules dir and installs the packages inside another node_modules directory.
我明白这对于安装包是有意义的。但是我不在我的网页应用程序里面,我直接运行它,但是我不需要$ {code> require()我正在寻找一种将依赖关系安装到特定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.
推荐答案
运行:
npm install
从您的应用程序目录(即package.json所在的位置)将安装应用程序的依赖关系,而不是将其作为模块安装如这里所述。这些将放在./node_modules相对于你的package.json文件(实际上比这更复杂一些,所以检查 npm文档在这里)。
from inside your app directory (i.e. where package.json is located) will install the dependencies for your 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).
如果需要,可以将node_modules目录移动到应用程序的父目录,因为节点的需要机制明白这一点但是,如果要使用install / update更新应用程序的依赖项,npm将不会再看到重定位的node_modules,而是相对于package.json再次创建一个新的目录。
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的符号链接:
To prevent this, just create a symlink to the relocated node_modules from your app dir:
ln -s ../node_modules node_modules
这篇关于如何使用npm在当前目录中安装package.json依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!