34

Does npm have the option to install dependency as peer-dependency like yarn option --yarn, instead of adding it manually for example:

"peerDependencies": {
  "@angular/core": "^7.0.0"
}

Update with more clarification of the question, thanks to @Broncha

The question is how to add a peer dependency to a project. That is

  • npm i dep adds the dependency to the "dependencies" in package.json,
  • npm i -D dep adds the dependency to the "devDependencies" in package.json.

How do I install a dependency that adds it to the "peerDependencies" in package.json?

Amr Salama
  • 802
  • 1
  • 7
  • 19
  • Does this answer your question? [How to install npm peer dependencies automatically?](https://stackoverflow.com/questions/35207380/how-to-install-npm-peer-dependencies-automatically) – MwamiTovi Apr 09 '20 at 05:49
  • 1
    @MwamiTovi Unfortunately no, I need to add a peer-dependency to my project, So should I add it manually to peerDependencies? – Amr Salama Apr 09 '20 at 06:03
  • 1
    Yes, as explained in that answer you'll have to handle `peer-dependencies` manually. – MwamiTovi Apr 09 '20 at 10:09
  • 7
    I like how everyone is on the same bandwagon to answer how to install peer-dependencies while the question is how to add a peer dependency to a project. That is, npm i dep, adds the dependency to "dependencies" key in package.json, npm i -D dep adds the dependency to "devDependencies" in package.json. How do I install a dependency that adds it to "peerDependencies" key in package.json? I also searched for this, but I installed it with npm i and moved it to the key manually – Broncha Jan 24 '21 at 10:09
  • @Broncha Thanks for describing the question in a better way, I updated the question with your description. – Amr Salama Jan 24 '21 at 12:17
  • @AmrSalama I noticed that you update the question and my answer does not fulfill the context of the updated question. that why i update my answer i know it's kind of "now answer" but this is what it is!. – Rohit Nishad Aug 26 '21 at 09:32

3 Answers3

27

As for now, there is NO WAY, you can install dependencies as peer-dependencies. You have to install then and manually move them to peerDependencies object in package.json

OLD ANSWER


The automatic install of peer dependencies was removed with npm v3, this feature is aging added in npm v7.

So update your npm to version 7 or higher will solve most of the problems.

If You need to install dependency as a peer dependency.

To install peer dependency, you actually need to manually modify your package.json file.

For example, if you want to install angular's core component library as a peer dependency,

  1. npm i @angular/core

This will add a property in the dependencies object.

"dependencies": {
    "@angular/core": "^7.0.0"
}
  1. Move the installed package name to peerDependencies key.
"peerDependencies": {
    "@angular/core": "^7.0.0"
}

Extra: if you need two versions of the same package then you modify the packge.json file like this,

"peerDependencies": {
   "@angular/core": "^6.0.0"
   "@angular/core": "^7.0.0"
 }
Rohit Nishad
  • 2,570
  • 2
  • 22
  • 32
  • 3
    So this used to exist in `npm` and now it no longer does? Why? – Hashim Aziz Jan 22 '22 at 18:30
  • 2
    sure about the “2 versions of same package”? It’s a JSON, twice the same key means a) first is overridden, last wins.. and b) eslint warning. If this is possible they violated the JSON format? ^^ @HashimAziz if it really was possible I think my comment here is the answer – faebster Mar 28 '23 at 02:56
  • I believe the proper version should be: `"@angular/core": ">= 6.0.0 < 8"` (if we want to support versions from 6.0.0, but below 8.0.0.) – Lucas Matuszewski Jul 12 '23 at 22:45
8

All the other answers are talking about How NPM command can handle installing the 'peerDeps' of the current 'deps' and 'devDeps' in package.json of current project, installing them automatically.

But the question is ask how to use NPM command with specific flag to install a deps as 'peerDeps' and write into the package.json of current project.

The ANSWER is, unfortunately, there is no such flag even till NPM@7

I guess NPM doesn't treat that a command to install deps, since adding a 'peerDeps' to package.json doesn't really need NPM to install a package to /node_modules/. It is just a file configuration change to package.json. But I understand people don't want to manually add/remove 'deps' in package.json file and want NPM to do that, it may because NPM will handle the order of the 'deps'. Another reason is, 'peerDeps' always use a range of semver, and that has to be edit manually not via a npm install command. like react-redux:

"peerDependencies": {
  "react": "^16.8.3 || ^17"
},

I think NPM@7 should provide a way to support that, since now it is officially able to process the 'peerDeps' and this feature is part of it.

Wayne Mao
  • 419
  • 5
  • 9
1

You can use the pkg command:

npm pkg set peerDependencies.@angular/core="^7.0.0"
Kevin Heye
  • 11
  • 1
  • 2
    How is that different than updating `package.json` manually? – Amr Salama Aug 15 '23 at 05:05
  • This is handy when you need to edit multiple values, for example, or if you don't want to force the user to manually edit the file. Just copy-paste and press enter. – mcmimik Aug 30 '23 at 13:38