node.js 如何编写 package.json 文件,以便使用“npm install”下载所有依赖项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12127114/
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 to write a package.json file so that all dependencies are downloaded with "npm install"
提问by Merc
I wrote a simple application using node. It depends on express, mongodb and mongoose (easy).
So, I created a file called package.jsonand put this in it:
我使用 node.js 编写了一个简单的应用程序。这取决于 express、mongodb 和 mongoose(简单)。因此,我创建了一个名为的文件package.json并将其放入其中:
{
"name": "booking-dojo",
"description": "Booking dojo app",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "3.x",
"mongodb": "*",
"mongoose": "*"
}
}
I then ran npm install, expecting npm to install those modules andtheir dependencies.
The result was disappointing:
然后我跑了npm install,期待NPM安装这些模块和它们的依赖。结果令人失望:
[email protected] /home/merc/Synced/Development/Bookings/app/server
├─┬ [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ └─┬ [email protected]
│ └── [email protected]
├─┬ [email protected]
│ └── [email protected]
└─┬ [email protected]
├── [email protected]
└── [email protected]
I am confused by this, as I knowthat expressneeds jade(and much more), and mongooseneeds mongodb.
If I go into node_modules/jadeand run npm install, the result from the main tree is very different:
我为此感到困惑,因为我知道这express需要jade(以及更多),以及mongoose需求mongodb。
如果我进入node_modules/jade并运行npm install,主树的结果非常不同:
[email protected] /home/merc/Synced/Development/Bookings/app/server
├─┬ [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ └── [email protected]
│ ├─┬ [email protected]
│ │ └─┬ [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ └─┬ [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ └─┬ [email protected]
│ └─┬ [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ └── [email protected]
├─┬ [email protected]
│ └── [email protected]
└─┬ [email protected]
├── [email protected]
└── [email protected]
So, expresshas growna lot. It looks like npm installis only loading someof the dependencies of the sub-modules.
因此,express已经成长了很多。看起来npm install只是加载了子模块的一些依赖项。
Can somebody please shed some light on this? Why are some dependencies missing? Am I doing something wrong? (likely)
有人可以对此有所了解吗?为什么缺少某些依赖项?难道我做错了什么?(可能)
Thanks!
谢谢!
Merc.
梅尔。
采纳答案by Merc
The answer was provided by Brandon in a comment to another answer:
布兰登在对另一个答案的评论中提供了答案:
"Another thing to note is that if a package depends on a module that can be resolved further up in the dependency chain, it will. For example, since you have mongodb in your package.json, Mongoose doesn't need to install its own mongodb. – Brandon Tilley 2 days ago
“另一件需要注意的事情是,如果一个包依赖于一个可以在依赖链中进一步解析的模块,它会。例如,因为你的 package.json 中有 mongodb,Mongoose 不需要安装它自己的mongodb. – Brandon Tilley 2 天前
Thank you Brandon! (And this is the answer...)
谢谢布兰登!(这就是答案......)
回答by Peter Lyons
You are confused about at least 2 points.
您对至少 2 点感到困惑。
First, express does not depend on jade, as you can see by reading the node_modules/express/package.jsonfile:
首先,express 不依赖于 jade,通过阅读node_modules/express/package.json文件可以看出:
"dependencies": {
"connect": "2.4.2",
"commander": "0.6.1",
"range-parser": "0.0.4",
"mkdirp": "0.3.3",
"cookie": "0.0.4",
"crc": "0.2.0",
"fresh": "0.1.0",
"methods": "0.0.1",
"send": "0.0.3",
"debug": "*"
}
Express does, however, work withjade ifit is available, as well as many other template engines. So to fix this list jade as a dependency in your package.json file and you'll be fine.
但是,Express 确实可以使用jade(如果可用)以及许多其他模板引擎。因此,将这个列表 jade 修复为 package.json 文件中的依赖项,你会没事的。
Second, npm only installs node packages, not third party things like mongodb. You need to install mongodb and any other dependencies that are not npm modules using other means (apt-get, yum, manual install, etc).
其次,npm 只安装 node 包,而不是像 mongodb 这样的第三方东西。您需要使用其他方式(apt-get、yum、手动安装等)安装 mongodb 和任何其他非 npm 模块的依赖项。
So npm DOES install dependencies recursively, but only npm modules.
所以 npm 确实会递归安装依赖项,但只安装 npm 模块。
回答by M.Ganji
use this sample
使用这个样本
{
"name": "app",
"version": "0.0.1",
"main":"test.js",
"author":"Test",
"description": "For test ",
"dependencies": {
"express": "latest",
"mongoose": "latest"
}
}

