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?
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.
peerDependencies
object in package.json
- I noticed that you update the question and my answer does not fulfill the context of the updated question.
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.
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,
npm i @angular/core
This will add a property in the dependencies object.
"dependencies": {
"@angular/core": "^7.0.0"
}
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"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With