什么是peerDependencies

什么是peerDependencies

https://www.dazhuanlan.com/2020/02/01/5e354f97cac63/

本文主要讲述了:

  1. 背景
  2. 示例

正文

背景

考虑这样一种场景:

  1. 开发者针对`example@2.0.0开发了一个名为example-plugin-a@1.0.0`的插件
  2. `example-plugin-a@1.0.0不需要引用example@2.0.0,两者不存在显式的依赖关系。但从逻辑上讲,example@2.0.0example-plugin-a@1.0.0`的宿主
  3. `example-plugin-a@1.0.0不兼容example@1.0.0`

为了避免用户在`example@1.0.0的环境上安装example-plugin-a@1.0.0,显然此时开发者需要声明example-plugin-a@1.0.0example@2.0.0`的宿主关系。

于是peerDependencies应运而生。

peer的中文意思为同辈的、同龄的。peerDependencies可以理解为同伴依赖,它表示包和包之间的宿主关系。

示例

grunt-babelgrunt的插件。

grunt-babelpeerDependencies为:

1
2
3
4
5
6
{
"peerDependencies": {
"@babel/core": "^7.0.0",
"grunt": ">=0.4.0"
}
}

我们可以新建一个项目来体验一下peerDependencies的作用,就像下文这样:

1
2
3
mkdir learn_npm && cd learn_npm
npm init -y
npm install --save-dev grunt-babel

依赖安装完成之后,npm会发出同伴依赖缺失的警告:

1
2
npm WARN grunt-babel@8.0.0 requires a peer of @babel/core@^7.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN grunt-babel@8.0.0 requires a peer of grunt@>=0.4.0 but none is installed. You must install peer dependencies yourself.

参考资料

posted @ 2021-02-07 07:31  py2020  阅读(1809)  评论(0)    收藏  举报