How to install npm peer dependencies automatically?

node.jsNpm

node.js Problem Overview


For example, when I install Angular2:

npm install --save angular2
temp@1.0.0 /Users/doug/Projects/dougludlow/temp
├── angular2@2.0.0-beta.3 
├── UNMET PEER DEPENDENCY es6-promise@^3.0.2
├── UNMET PEER DEPENDENCY es6-shim@^0.33.3
├── UNMET PEER DEPENDENCY reflect-metadata@0.1.2
├── UNMET PEER DEPENDENCY rxjs@5.0.0-beta.0
└── UNMET PEER DEPENDENCY zone.js@0.5.11

npm WARN angular2@2.0.0-beta.3 requires a peer of es6-promise@^3.0.2 but none was installed.
npm WARN angular2@2.0.0-beta.3 requires a peer of es6-shim@^0.33.3 but none was installed.
npm WARN angular2@2.0.0-beta.3 requires a peer of reflect-metadata@0.1.2 but none was installed.
npm WARN angular2@2.0.0-beta.3 requires a peer of rxjs@5.0.0-beta.0 but none was installed.
npm WARN angular2@2.0.0-beta.3 requires a peer of zone.js@0.5.11 but none was installed.

Is there a magic flag that I can pass to npm that will install the peer dependencies as well? I haven't been able to find one... It's tedious to manually copy and paste the peer dependencies and make sure I have the correct versions.

In other words, I'd rather not have to do:

npm install --save angular2@2.0.0-beta.3 es6-promise@^3.0.2 es6-shim@^0.33.3 reflect-metadata@0.1.2 rxjs@5.0.0-beta.0 zone.js@0.5.11

What is the better way?

node.js Solutions


Solution 1 - node.js

The automatic install of peer dependencies was explicitly removed with npm 3, as it cause more problems than it tried to solve. You can read about it here for example:

So no, for the reasons given, you cannot install them automatically with npm 3 upwards.

NPM V7

NPM v7 has reintroduced the automatic peerDependencies installation. They had made some changes to fix old problems as version compatibility across multiple dependants. You can see the discussion here and the announcement here

Now in V7, as in versions before V3, you only need to do an npm i and all peerDependences should be automatically installed.

Solution 2 - node.js

I solved it by rewriting package.json with the exact values warnings were about.

Warnings when running npm:

npm WARN angular2@2.0.0-beta.3 requires a peer of es6-shim@^0.33.3 but none was installed.
npm WARN angular2@2.0.0-beta.3 requires a peer of reflect-metadata@0.1.2

In package.json, write

"es6-shim": "^0.33.3",
"reflect-metadata": "0.1.2",

Then, delete node_modules directory.

Finally, run the command below:

npm install

Solution 3 - node.js

Cheat code helpful in this scenario and some others...

├── UNMET PEER DEPENDENCY @angular/common@4.0.2
├── UNMET PEER DEPENDENCY @angular/compiler@4.0.2
├── UNMET PEER DEPENDENCY @angular/compiler-cli@4.0.2
├── UNMET PEER DEPENDENCY @angular/core@4.0.2
├── UNMET PEER DEPENDENCY @angular/forms@4.0.2
├── UNMET PEER DEPENDENCY @angular/http@4.0.2
├── UNMET PEER DEPENDENCY @angular/platform-browser@4.0.2
├── UNMET PEER DEPENDENCY @angular/platform-browser-dynamic@4.0.2 >
  1. copy & paste your error into your code editor.
  2. Highlight an unwanted part with your curser. In this case ├── UNMET PEER DEPENDENCY
  3. Press command + d a bunch of times.
  4. Press delete twice. (Press space if you accidentally highlighted ├── UNMET PEER DEPENDENCY )
  5. Press up once. Add npm install
  6. Press down once. Add --save
  7. Copy your stuff back into the cli and run
npm install @angular/common@4.0.2 @angular/compiler@4.0.2 @angular/compiler-cli@4.0.2 @angular/core@4.0.2 @angular/forms@4.0.2 @angular/http@4.0.2 @angular/platform-browser@4.0.2 @angular/platform-browser-dynamic@4.0.2 --save

Solution 4 - node.js

I experienced these errors when I was developing an npm package that had peerDependencies. I had to ensure that any peerDependencies were also listed as devDependencies. The project would not automatically use the globally installed packages.

Solution 5 - node.js

The project npm-install-peers will detect peers and install them.

As of v1.0.1 it doesn't support writing back to the package.json automatically, which would essentially solve our need here.

Please add your support to issue in flight: https://github.com/spatie/npm-install-peers/issues/4

Solution 6 - node.js

I was facing the same issue, lucky I found an alternative way to install peer dependencies along with the install command.

Step 1: $ npm i npm-install-peers -D

for more clarity about the plugin: https://www.npmjs.com/package/npm-install-peers

Step 2: Update package.json for magical script

  ....
   "scripts": {
    ...
    "postinstall": "npm-install-peers"
  },
  ....

Step 3: Just need to hit the install command to get installed all plugins

$ npm install

Solution 7 - node.js

Might be a little outdated but you can run npx install-peerdeps --yarn --dev PACKAGE_NAME. Pass the --yarn flag if you wanna use yarn and --dev flag if you want the package and its peer deps to be added as dev dependencies. Hope this helps

Solution 8 - node.js

Install yarn and then run:

yarn global add install-peerdeps

Solution 9 - node.js

Execute this: npm install-test

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDouglas LudlowView Question on Stackoverflow
Solution 1 - node.jscrackmiggView Answer on Stackoverflow
Solution 2 - node.jsMrLehisteView Answer on Stackoverflow
Solution 3 - node.jszoomlarView Answer on Stackoverflow
Solution 4 - node.jsjoshweirView Answer on Stackoverflow
Solution 5 - node.jsdeepelementView Answer on Stackoverflow
Solution 6 - node.jsVinesh GoyalView Answer on Stackoverflow
Solution 7 - node.jsRoy kathurimaView Answer on Stackoverflow
Solution 8 - node.jsCarlosView Answer on Stackoverflow
Solution 9 - node.jsIvan FerrerView Answer on Stackoverflow