如何在 Nodejs 中递归安装依赖项?
How to do recursive installation of dependencies in Nodejs?
我有以下项目结构。
parentDir
---> Child1
---- [Child1] package.json
---> Childe2
---- [Child2] package.json
-----> SubChild3
------ [subchild3] package.json
---- [parent] package.json
我有一个单独的模块,它有自己的依赖项,我想一次安装所有包,我不想进入特定目录。这可能吗 ?我尝试使用保存目录路径的 shell 脚本,但代码库发生了巨大变化,因此不能总是更新 shell 脚本目录条目。如何使用 Grunt 等任何任务运行器在 Nodejs 中实现这一点?
假设您正在为每个模块创建包,您只需要在根目录中有一个 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 ^
上接受的答案
In the simplest terms, the tilde matches the most recent minor version
(the middle number). ~1.2.3 will match all 1.2.x versions but will
miss 1.3.0.
The caret, on the other hand, is more relaxed. It will update you to
the most recent major version (the first number). ^1.2.3 will match
any 1.x.x release including 1.3.0, but will hold off on 2.0.0.
几天前我正在阅读 recursive-install node library which will recursively install all package.json inside child-module or sub-child-module even. And using this library you don't have to mention child-modules and sub-child-module in parent package.json. And if you will see the implementation of this library (github-link) 它只是一个递归分析您的子模块的 JS 文件。所以要么你可以使用这个库,要么你可以编写类似于这个库的 js 脚本。
我有以下项目结构。
parentDir
---> Child1
---- [Child1] package.json
---> Childe2
---- [Child2] package.json
-----> SubChild3
------ [subchild3] package.json
---- [parent] package.json
我有一个单独的模块,它有自己的依赖项,我想一次安装所有包,我不想进入特定目录。这可能吗 ?我尝试使用保存目录路径的 shell 脚本,但代码库发生了巨大变化,因此不能总是更新 shell 脚本目录条目。如何使用 Grunt 等任何任务运行器在 Nodejs 中实现这一点?
假设您正在为每个模块创建包,您只需要在根目录中有一个 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 ^
In the simplest terms, the tilde matches the most recent minor version (the middle number). ~1.2.3 will match all 1.2.x versions but will miss 1.3.0.
The caret, on the other hand, is more relaxed. It will update you to the most recent major version (the first number). ^1.2.3 will match any 1.x.x release including 1.3.0, but will hold off on 2.0.0.
几天前我正在阅读 recursive-install node library which will recursively install all package.json inside child-module or sub-child-module even. And using this library you don't have to mention child-modules and sub-child-module in parent package.json. And if you will see the implementation of this library (github-link) 它只是一个递归分析您的子模块的 JS 文件。所以要么你可以使用这个库,要么你可以编写类似于这个库的 js 脚本。