How to do recursive installation of dependencies in Nodejs?

I have following structure of project.

parentDir
---> Child1
---- [Child1] package.json
---> Childe2
---- [Child2] package.json
     -----> SubChild3
     ------ [subchild3] package.json
---- [parent] package.json

I have separate module which has it's own dependencies and I want to do installation of all packages at once, I don't want to go in specific directory. Is this possible ? I tried with shell script which hold the directory path but code bases changes drastically so can't always update shell scripts directory entries. How to achieve this in Nodejs using any task runner like Grunt etc.?


Assuming you are creating packages for each of your modules, you just need a package.json in the root with all dependencies named. Each one of those packages has their own package.json with dependencies. Then from your project root (where the package.json is) just run

npm install

npm will take care of installing the dependencies' dependencies. Example:

// 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",
...
}

Even the dependencies above will have their own package files which npm works it's way through when you run npm install at the root. It prints its results to the command line (if you run it from there). If I try a simple (global) install of grunt I see on the command line:

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) 

The child dependencies are listed vertically and children of children are listed horizontally eg the child dependency js-yaml is listed as:

js-yaml@2.0.5 (esprima@1.0.4, argparse@0.1.16)

Here's the accepted answer on 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.xx release including 1.3.0, but will hold off on 2.0.0.

链接地址: http://www.djcxy.com/p/27658.html

上一篇: node.js依赖列表中的“^”是什么意思?

下一篇: 如何在Nodejs中进行依赖关系的递归安装?