11

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

Sagar
  • 2,315
  • 7
  • 25
  • 34
  • see https://stackoverflow.com/questions/31773546/the-best-way-to-run-npm-install-for-nested-folders – anneb Sep 03 '19 at 10:29

3 Answers3

8

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.

Anshita Singh
  • 1,583
  • 1
  • 17
  • 40
4

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

Community
  • 1
  • 1
br3w5
  • 4,403
  • 5
  • 33
  • 42
  • 1
    each packages have different dependencies which I don't want to list out on root package.json..I want to keep them separate according to package level, as some times I need different version for same dependency on different module. – Sagar Oct 08 '15 at 11:18
  • Yes so you need to reference only the child package name and version in your root package.json then the child package.json files can reference their own dependencies – br3w5 Oct 08 '15 at 11:21
  • child packages are my own module, Can you provide me short example, how it will be hold inside package.json of root ? – Sagar Oct 08 '15 at 11:34
  • 1
    I think we should pass dependency using `file:path` as local path in `package.json` then only it will install dependency..I tried above example but it just copied `package.json` of child package in `node_modules` directory. Is it possible not to throw child package under `node_module` directory and it's dependency should get install under it's own package? – Sagar Oct 09 '15 at 06:10
  • Yes you should use file path for local modules otherwise npm will try to fetch it from the package index (ie. you would need to publish your modules to the package index but you may not want to) – br3w5 Oct 09 '15 at 06:16
  • Correct me if I am wrong..I want to keep the child dependency under `child-> node_modules` directory instead of `parent->node_modules`. Is this a right way to have module structure? and is it possible with `local path` in `package.json`? – Sagar Oct 09 '15 at 06:27
  • Yes it will create a node_modules directory the child's dependencies – br3w5 Oct 09 '15 at 08:57
  • No it's not creating a `node_modules` directory under child package..what it does it :- take child package and pushes it in `parent's` `node_modules` directory..and then it create `node_modules` under it..I want `node_modules` directory directly under child package in it self..not under `parents-> node_modules -> child-> node_modules` – Sagar Oct 09 '15 at 09:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91828/discussion-between-br3w5-and-sagar). – br3w5 Oct 09 '15 at 10:12
0

One bash line... find . -type f -name package.json -exec bash -c 'yarn install --cwd $(dirname {})' \;

Aleks
  • 894
  • 10
  • 14