假设您正在为每个模块创建包,您只需要在根目录中使用 package.json 并命名所有依赖项。这些包中的每一个都有自己的 package.json 和依赖项。然后从您的项目根目录(package.json 所在的位置)运行
npm install
npm 将负责安装依赖项的依赖项。示例:
// parent package.json
{
"name": "yourApp",
"description": "An app for doing stuff",
"version": "1.0.0",
"scripts": {
"init": "npm install",
"install": "bower install",
"start": "node src/server/app.js",
"test": "gulp test"
},
"dependencies": {
"angular-ui-router": "^0.2.15",
"body-parser": "^1.8.2",
"express": "^4.9.3",
"express-content-length-validator": "^1.0.0"
}
}
// child dependency (this example is part of angular-ui-router's package.json)
{
"name": "angular-ui-router",
"description": "State-based routing for AngularJS",
"version": "0.2.15",
"homepage": "http://angular-ui.github.com/",
...
"dependencies": {},
"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-concat": "~0.3.0",
"grunt-contrib-uglify": "~0.4.0",
"grunt-contrib-jshint": "~0.8.0",
"grunt-contrib-watch": "~0.5.3",
"grunt-contrib-connect": "~0.7.1",
"grunt-contrib-clean": "~0.5.0",
...
}
即使是上面的依赖项也会有自己的包文件,当您在根目录下运行 npm install 时,npm 可以正常工作。它将结果打印到命令行(如果您从那里运行它)。如果我尝试简单(全局)安装grunt,我会在命令行上看到:
grunt@0.4.5 node_modules/grunt
├── which@1.0.9
├── dateformat@1.0.2-1.2.3
├── eventemitter2@0.4.14
├── getobject@0.1.0
├── rimraf@2.2.8
├── colors@0.6.2
├── async@0.1.22
├── hooker@0.2.3
├── grunt-legacy-util@0.2.0
├── exit@0.1.2
├── nopt@1.0.10 (abbrev@1.0.7)
├── minimatch@0.2.14 (sigmund@1.0.1, lru-cache@2.7.0)
├── glob@3.1.21 (inherits@1.0.2, graceful-fs@1.2.3)
├── lodash@0.9.2
├── coffee-script@1.3.3
├── underscore.string@2.2.1
├── iconv-lite@0.2.11
├── findup-sync@0.1.3 (glob@3.2.11, lodash@2.4.2)
├── grunt-legacy-log@0.1.2 (grunt-legacy-log-utils@0.1.1, underscore.string@2.3.3, lodash@2.4.2)
└── js-yaml@2.0.5 (esprima@1.0.4, argparse@0.1.16)
子依赖项垂直列出,子项的子项水平列出,例如子依赖 js-yaml 被列为:
js-yaml@2.0.5 (esprima@1.0.4, argparse@0.1.16)
这是the difference between ~ and ^上接受的答案
简单来说,波浪号匹配最新的次要版本
(中间数)。 ~1.2.3 将匹配所有 1.2.x 版本,但将
错过 1.3.0。
另一方面,插入符号更轻松。它会更新你
最新的主要版本(第一个数字)。 ^1.2.3 将匹配
任何 1.x.x 版本,包括 1.3.0,但将推迟到 2.0.0。