如何将 npm 依赖添加为对等依赖 [英] How to add npm dependency as peer dependency

查看:45
本文介绍了如何将 npm 依赖添加为对等依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

npm 是否可以选择像 yarn option --yarn 一样将依赖项安装为对等依赖项,而不是手动添加,例如:

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"
}


更新,对问题进行更多的澄清,感谢@Broncha


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 将依赖添加到dependencies";在 package.json 中,
  • npm i -D dep 将依赖添加到devDependencies"在 package.json 中.
  • 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.

如何安装将其添加到peerDependencies"的依赖项?在 package.json 中?

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

推荐答案

至于目前,没有办法,您可以将依赖项安装为对等依赖项.您必须安装然后手动将它们移动到 package.json

中的 peerDependencies 对象

- 我注意到您更新了问题,而我的回答不符合更新问题的上下文.

在 npm v3 中删除了对等依赖项的自动安装,此功能在 npm v7 中已老化.

因此,将您的 npm 更新到版本 7 或更高版本将解决大部分问题.

要安装对等依赖,您实际上需要手动修改您的 package.json 文件.

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

比如你想安装angular的核心组件库作为peer依赖,

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

  1. npm i @angular/core

这将在依赖项对象中添加一个属性.

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

  1. 将安装的包名移动到 peerDependencies 键.

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

额外:如果你需要同一个包的两个版本,那么你可以像这样修改 packge.json 文件,

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"
 }

这篇关于如何将 npm 依赖添加为对等依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆