Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.?

like image 541
Sagar Avatar asked Oct 08 '15 11:10

Sagar


2 Answers

Few days back i was reading 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) it's just a JS file which recursively analyze your sub-module. So either you can go with this library or you can write your js script similar to this library.

like image 110
Anshita Singh Avatar answered Sep 30 '22 14:09

Anshita Singh


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:

[email protected] node_modules/grunt
├── [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]) 

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

[email protected] ([email protected], [email protected])

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.x.x release including 1.3.0, but will hold off on 2.0.0.

like image 34
br3w5 Avatar answered Sep 30 '22 12:09

br3w5