Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm is installing dependencies in a weird recursive way

Tags:

npm

I used npm install --dev to install the dependencies listed in a package.json file and it seems that rather than installing all the packages in a flat structure, it is installing the dependencies of each module inside the module's directory.

I had to cancel it because it was taking a lot, because many dependencies should have been already satisfied but it had to re-download them in a different nested directory (and I hope that there are not loops and it would have eventually finished)...

Using the tree command this is an example of what I am getting:

├── grunt-cli
│   └── node_modules
│       ├── findup-sync
│       │   └── node_modules
│       │       ├── glob
│       │       │   └── node_modules
│       │       │       ├── minimatch
│       │       │       │   └── node_modules
│       │       │       │       ├── lru-cache
│       │       │       │       │   └── node_modules
│       │       │       │       │       └── weak
│       │       │       │       │           └── node_modules
│       │       │       │       │               └── mocha
│       │       │       │       │                   └── node_modules
│       │       │       │       │                       ├── coffee-script
│       │       │       │       │                       │   └── node_modules
│       │       │       │       │                       │       └── jison
│       │       │       │       │                       │           └── node_modules
│       │       │       │       │                       │               └── browserify
│       │       │       │       │                       │                   └── node_modules
│       │       │       │       │                       │                       ├── browser-resolve
│       │       │       │       │                       │                       │   └── example

Any ideas why this might be happening?

like image 497
fortran Avatar asked Aug 14 '13 20:08

fortran


People also ask

How do I fix npm dependencies?

The easy fix is to use the npm audit --fix which will look for updates that can be updated to fix those automatically.

How do I force npm to install dependencies?

NPM installs devDependencies within the package. json file. The 'npm install' command should add all the dependencies and devDependencies automatically during installation. If you need to add specific devDependencies to your project, you can use this command- 'npm install --save-dev'.

Does npm install automatically install dependencies?

By default, npm install will install all modules listed as dependencies in package.json . With the --production flag (or when the NODE_ENV environment variable is set to production ), npm will not install modules listed in devDependencies .


2 Answers

Don't worry, that's actually the way Npm was designed. You may even see the same module installed many times (though typically with different versions).

The idea is that each module installed under node_modules is a somewhat self-contained installation of that module with its dependencies and the necessary versions -- it doesn't depend on modules installed globally.

If you are worried about inefficiency of modules being installed multiple times, the experimental npm dedupe.

like image 169
Jonathan Warden Avatar answered Nov 11 '22 22:11

Jonathan Warden


dedupe is helpful, as Jonathan Warden says. However, try npm3. For a simple package such as gulp-jscs the install of npm3 was 20% fewer files, 5% less disk space. And tree depth went from 22 to 5. However npm3 was 2x slower for me.

like image 26
John Henckel Avatar answered Nov 11 '22 23:11

John Henckel