package.json 文件中的 peerDependencies

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

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

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就会有警告来提醒使用者去解决版本冲突问题。


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

查看所有标签

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

A Philosophy of Software Design

A Philosophy of Software Design

John Ousterhout / Yaknyam Press / 2018-4-6 / GBP 14.21

This book addresses the topic of software design: how to decompose complex software systems into modules (such as classes and methods) that can be implemented relatively independently. The book first ......一起来看看 《A Philosophy of Software Design》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具

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

HSV CMYK互换工具