[译] Node 模块中的 peer dependencies 是什么?

栏目: IT技术 · 发布时间: 5年前

内容简介:原文:https://flaviocopes.com/npm-peer-dependencies/在某些 package.json 文件中,你可能见到过一些这样的配置行:

原文:https://flaviocopes.com/npm-peer-dependencies/

[译] Node 模块中的 peer dependencies 是什么?

在某些 package.json 文件中,你可能见到过一些这样的配置行:

{
  //...
  "peerDependencies": {
    "libraryName": "1.x"
  }
}

dependencies    devDependencies   常见,而   peerDependencies   并不是。

dependencies   是你的项目所依赖的包。

devDependencies   是开发阶段所需要的包。比如说像 Jest 这样的测试框架或 Babel、ESLint 等其他库。

在以上两种情况中,当你安装一个包时,其 dependencies 和 devDependencies 会被 npm 自动安装。

peerDependencies   则有所不同,它们不会被自动安装。

当一个依赖项 c 被列在某个包 b 的 peerDependency 中时, 它就不会被自动安装 。取而代之的是,包含了 b 包的代码库 a 则必须将对应的依赖项 c 包含为其依赖。

如果运行 npm install   时找不到该依赖, npm   会作出警告,正如例子中所示:

a/package.json

{
  //...
  "dependencies": {
    "b": "1.x"
  }
}

b/package.json

{
  //...
  "peerDependencies": {
    "c": "1.x"
  }
}

因此,在包 a 中,必须添加 c 作为一个依赖项,这样当你安装 b 包时,npm 就不会告警了(代码运行时也不会失败):

a/package.json

{
  //...
  "dependencies": {
    "b": "1.x",
    "c": "1.x"
  }
}

要注意依赖项的版本必须是兼容的,如果一个 peerDependency 被标为 2.x ,你就不能安装   1.x   或其他不兼容的版本。该规定遵循 https://flaviocopes.com/npm-semantic-versioning/ 标准。

--End--

[译] Node 模块中的 peer dependencies 是什么?

查看更多前端好文

请搜索 fewelife 关注公众号

转载请注明出处


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Python for Everyone

Python for Everyone

Cay S. Horstmann、Rance D. Necaise / John Wiley & Sons / 2013-4-26 / GBP 181.99

Cay Horstmann's" Python for Everyone "provides readers with step-by-step guidance, a feature that is immensely helpful for building confidence and providing an outline for the task at hand. "Problem S......一起来看看 《Python for Everyone》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具