package.json 文件中的 peerDependencies

栏目: 后端 · 前端 · 发布时间: 5年前

内容简介:在假设我们现在有一个项目的的而

npm init 执行完成之后,会在当前目录下面生成 package.json 文件,这时,我们经常会使用 npm install package-name 或者 npm install development-required-package-name --save-dev 来安装一些依赖的第三方包,它们分别是运行时依赖和开发时依赖,但是还有一个不太常见的依赖叫 对等依赖 ,即 peerDependencies ,使用 npm install --save-peer 即可添加。

dependencies 依赖

假设我们现在有一个项目的的 package.json 文件如下:

{
  "name": "peer-dependencies-demo",
  "version": "0.0.0",
  "description": "Peer Dependencies Demo",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "pantao <ofcrab@gmail.com>",
  "license": "MIT",
  "dependencies": {
    "peer-dependencies-plugin": "^1.0.0"
  }
}

peer-dependencies-plugin 这个模块的 package.json 中有下面这样的依赖:

{
  ...
  "dependencies": {
    "peer-dependencies-plugin-core": "^1.0.0"
  }
  ...
}

安装完成之后,我们的项目目录结构将是下面这样的:

.
├──package.json
├──src
│  └──index.js
└──node_modules
   └──peer-dependencies-plugin
      └──node_modules
         └──peer-dependencies-plugin-core

此时,在 index.js 中,我们可以像下面这样引入 peer-dependencies-plugin

import PeerDependenciesPlugin from 'peer-dependencies-plugin'

但是,却不能像下面这样引用 peer-dependencies-plugin-core

import PeerDependenciesPluginCore from 'peer-dependencies-plugin-core'

这是因为,即使 peer-dependencies-plugin-core 已经安装进了 node_modules 里面的,但是却不在 node_modules 下,而是在他的子目录下的一个模块里面, import 的时候,只会在当前项目根目录的 node_modules 下查找,并不会去查找它的子目录。

所以,如果你在项目里面还要使用 peer-dependencies-plugin-core 的话,你还需要手工的安装对 peer-dependencies-plugin-core 依赖,这个时候,你的项目安装完成之后,目录结构就是下面这样的了:

.
├──package.json
├──src
│  └──index.js
└──node_modules
   └──peer-dependencies-plugin-core
   └──peer-dependencies-plugin
      └──node_modules
         └──peer-dependencies-plugin-core

这势必会靠成很多不必要的麻烦,首当齐冲的就是,你的项目依赖的是 1.0.0 ,而你依赖的另一个插件却只能支持到 0.0.8 ,这个时候,导致一个项目里面依赖了两次 peer-dependencies-plugin-core ,而且还不是同一个版本。

peerDependencies 的引入

为了解决上面这种问题, peer-dependencies-plugin 在声明对 peer-dependencies-plugin-core 的依赖的时候,设置为 peerDependencies

{
  ...
  "peerDependencies": {
    "peer-dependencies-plugin-core": "^1.0.0"
  }
  ...
}

它会告诉 npm :**如果某个 package 依赖我,那么这个 package 也应该对 peer-dependencies-plugin-core 依赖,这个时候,你 npm install peer-dependencies-plugin 的时候,将得到下面这样的目录:

.
├──package.json
├──src
│  └──index.js
└──node_modules
   └──peer-dependencies-plugin-core
   └──peer-dependencies-plugin

npm2 中,就算当前项目的 package.json 中没有对 peer-dependencies-plugin-core 的依赖,它也会被直接安装进 node_modules 目录下,但是如果你现在使用的是 npm3 ,那么安装完成之后, npm 并不会主动的帮你安装模块的 peerDependencies ,但是会发出一个警告,告诉你本次安装是否正确,可能是下面这样的:

peer-dependencies-plugin-core 是一个需要的依赖,但是还没有被安装。

此时,你需要手动的在 package.json 中指定对 peer-dependencies-plugin-core 的依赖。

什么时候使用 peerDependencies

npm 文档中对 peerDependencies 的介绍是:

In some cases, you want to express the compatibility of your package with a host tool or  library, while not necessarily doing a require of this host. This is usually referred to as a plugin. Notably, your module may be exposing a specific interface, expected and  specified by the host documentation.

大概的意思就是: 通常是在插件开发的场景下,你的插件需要某些依赖的支持,但是你又没必要去安装,因为插件的宿主会去安装这些依赖,你就可以用peerDependencies去声明一下需要依赖的插件和版本,如果出问题npm就会有警告来提醒使用者去解决版本冲突问题。


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

查看所有标签

猜你喜欢:

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

最优化导论

最优化导论

桑达拉姆 / 人民邮电出版社 / 2008-4 / 59.00元

最优化导论(英文版),ISBN:9787115176073,作者:(美国)(Sundaram、R、K)桑达拉姆一起来看看 《最优化导论》 这本书的介绍吧!

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

在线图片转Base64编码工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

SHA 加密
SHA 加密

SHA 加密工具