387

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?

Douglas Ludlow
  • 10,754
  • 6
  • 30
  • 54
  • Have you already solved that problem? I have the same issue installing angular2. Even after manually install es6-promise with -g flag and re-trying npm install -g angular2 I do get the same Error/Warning of 5 unmet peer dependencies es6-promise, es6-shim, reflect-metadata, rxjs and zone.js – nttakr Feb 04 '16 at 17:27
  • 5
    @nttakr - yes, installing the exact versions that it wants as peer dependencies gets rid of the warning. You don't want to install them globally (with the -g flag). You want to install them locally (-S flag), but as I said, they need to be the exact versions (ie: `angular2@2.0.0-beta.3` requires `es6-promise@^3.0.2`). However, I want to know if there is a command/flag which automatically installs the peer dependencies. – Douglas Ludlow Feb 04 '16 at 17:31
  • @DouglasLudlow what version of NPM are you using? – peteb Feb 04 '16 at 17:37
  • @peteb: `npm -v` => `3.5.3` – Douglas Ludlow Feb 04 '16 at 17:42
  • Running `npm install [PACKAGE]` for the package that misses a peer did the trick for me. Obviously, that won't solve the OP's issue, but I spent quite a bit of time researching that, so if this helps somebody... – Nicolai Weitkemper Mar 16 '19 at 21:03

8 Answers8

277

npm version 7 and newer

npm v7 has reintroduced the automatic peerDependencies installation. Now in V7, as in versions before V3, you only need to do an npm i and all peerDependences should be automatically installed.

They had made some changes to fix old problems as version compatibility across multiple dependants. You can see the discussion and the announcement.

Older Answer

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.

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
crackmigg
  • 5,571
  • 2
  • 30
  • 40
  • 4
    Yeah, I saw this: https://github.com/npm/npm/issues/6565... I was just hoping that you could still optionally install them with a flag or something. Guess I'll have to open an issue or something. – Douglas Ludlow Feb 04 '16 at 17:46
  • I do not believe that will happen as npm explicitly wants to remove the burden of managing peer dependencies away from lib authors. – crackmigg Feb 04 '16 at 17:49
  • 199
    How do you deal with this sort of problem? I'm not an npm expert so when I read "x requires a peer of y but none was found", I ask myself, "which peer?" and "how can i just make everything work again?" Is there a process? Do you dig into the code of x and y until you find out what's missing? Once you find out what's missing, what do you do next? Thanks! – Dan Cancro Aug 22 '16 at 16:07
  • 106
    Ah, it's an English problem: "x requires a peer of y but none was installed" should be "x requires the peer, y, but y was not installed". I understood it as "x requires one of y's peers but that peer was not installed and we're not telling you which of y's peers you need". – Dan Cancro Aug 22 '16 at 18:43
  • 1
    Also if I manually install the peer dependencies listed, I get them as `extraneous` packages – plsnoban Aug 24 '16 at 05:22
  • 3
    There are "extraneous" because you need to add them to you package dependencies. – Dinoboff Mar 08 '17 at 11:45
  • 4
    I thought I would add here that you should consider this a bug in angular2, and the real solution is for the authors of that package to stop listing things that are clearly dependencies as peer dependencies. – Elliot Nelson Apr 30 '17 at 03:25
  • Maybe setting both is (with appropriate *semver*) is a good compromise – Kamafeather Jul 17 '18 at 01:49
  • Those articles didn't really explain a whole lot about why they decided to do it. I don't really understand what's the logic in this, because when I get the warning that the peer dependency isn't installed I just install the exact peer dependency that they say is missing, this probably isn't the best but I don't know what else to do. – Sam May 04 '20 at 20:26
  • @Sam it is correct what you are doing and the reasoning is that it caused issues for package maintainers with conflicting peer dependencies between different packages and npm wanted to avoid that. – crackmigg May 05 '20 at 13:21
  • 1
    @DouglasLudlow If that helps, you can always use https://npmpeer.dev to find the correct versions of the required peer dependency. – faboulaws Aug 29 '20 at 18:13
43

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
MrLehiste
  • 1,018
  • 1
  • 8
  • 14
  • 25
    Perhaps an `npm cache clean`, `npm install` may be better than wiping node_modules? I always refrain from deleting node_modules, I like to find out what the issue is rather than blowing away the directory. – Bruno May 05 '16 at 13:40
  • 3
    I don't think this is a very safe idea for people to just get it to work. They have higher changes of worsening their conflicts: having code that doesn't match it's library. – zoomlar Jun 27 '18 at 13:22
  • Got the following error messag while trying to run npm clean cache: npm ERR! As of npm@5, the npm cache self-heals from corruption issues and data extracted from the cache is guaranteed to be valid. If you want to make sure everything is consistent, use 'npm cache verify' instead. On the other hand, if you're debugging an issue with the installer, you can use `npm install --cache /tmp/empty-cache` to use a temporary cache instead of nuking the actual one. npm ERR! npm ERR! If you're sure you want to delete the entire cache, rerun this command with --force. – Tarun Nov 02 '19 at 03:55
  • In which section of `package.json`? In which `package.json`? The one from the package or the one from the app? – Bernardo Ramos Jul 27 '21 at 04:44
23

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
wowkin2
  • 5,895
  • 5
  • 23
  • 66
zoomlar
  • 391
  • 4
  • 6
  • 9
    The option `--save` is evidently no longer required as of npm 5.0.0: https://stackoverflow.com/a/19578808/12484 – Jon Schneider Jan 25 '19 at 18:48
  • 4
    This is unhelpful, as it relies on shortcuts where it's not clear what you're actually doing. Shortcuts are different by platform and editor. – mbomb007 Jan 13 '22 at 14:42
  • Fair point. What he seems to be doing is taking the log output and zapping the complaints at the front of the lines so he can parse the rest into a single "npm install" command. – Mike K Apr 19 '22 at 17:50
13

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.

joshweir
  • 5,427
  • 3
  • 39
  • 59
11

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

deepelement
  • 2,457
  • 1
  • 25
  • 25
  • 40
    On running, It gave me this message. `This package doesn't seem to have any peerDependencies` – notnotundefined Feb 21 '17 at 07:42
  • 10
    As I understand it, `npm-install-peers` will install `peerDependencies` registered in `package.json`. It will not install `peerDependencies` of dependencies such as `angular2`. – drizzd May 26 '18 at 14:44
  • 1
    Yes, to second @drizzd comment: `npm-install-peers` is only intended for installing the `"peerDependencies"` listed in your project's package.json. You are trying to install the `"peerDependencies"` listed in each one of you `node_modules//package.json` files, not your direct peer dependencies. – Will Farley Dec 18 '18 at 20:17
5

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

Vinesh Goyal
  • 607
  • 5
  • 8
0

Here's a one liner for version 6.14.12 of npm:

npm install $(cut -d ' ' -f 6 <(npm ls 2>&1 | grep "npm ERR! peer dep missing:" | sed 's/\^.*|| //') | sed 's/,*$//' | sort | uniq | tr '\n' ' ')

It takes the error output from npm ls and installs everything listed there.

ADJenks
  • 2,973
  • 27
  • 38
-5

Install yarn and then run:

yarn global add install-peerdeps
Farhan
  • 1,445
  • 16
  • 24
Carlos
  • 3,480
  • 1
  • 16
  • 14
  • 3
    It is not necessary to install yarn in order to install npm packages. Also, the `install-peerdeps` package gives an error when there are no peer dependencies. – Jeff Apr 20 '21 at 21:46
  • [A code-only answer is not high quality](//meta.stackoverflow.com/questions/392712/explaining-entirely-code-based-answers). While this code may be useful, you can improve it by saying why it works, how it works, when it should be used, and what its limitations are. Please [edit] your answer to include explanation and link to relevant documentation. – ray Nov 06 '22 at 01:58