对等依赖设置
autoInstallPeers#
- 默认值:true
- 类型:Boolean
当值为 true 时,将自动安装任何缺少的非可选对等依赖。
Optional peer dependencies#
pnpm installs a missing optional peer dependency only if the workspace already has a version of it that satisfies the optional peer range. In that case, pnpm resolves the optional peer to that version. That version can come from any project in the workspace, including another project's devDependencies. For example, if one project depends on webpack and another has webpack-cli in its devDependencies, both projects get the webpack instance that has webpack-cli as its optional peer.
pnpm also resolves optional peers this way when dedupePeerDependents is true. To stop it, set both autoInstallPeers and dedupePeerDependents to false.
Version conflicts#
如果来自不同软件包的对等依赖项的需求版本存在冲突,那么 pnpm 将不会自动安装任何版本的冲突的对等依赖项。相反,会输出一条警告信息。比如,如果一个依赖项需要 react@^16.0.0,而另一个需要 react@^17.0.0,则会产生冲突,自动安装将不会进行。
Conflict resolution#
如果出现版本冲突,你需要评估自己安装哪个版本的对等依赖项,或更新依赖项以符合其对等依赖项要求。
dedupePeerDependents#
- 默认值:true
- 类型:Boolean
当此设置为 true 时,对等依赖在对等解析后会被做去重处理。
This setting also lets pnpm resolve missing required and optional peers using compatible versions available elsewhere in the workspace, including transitive dependencies of other projects. Required peers can be resolved this way even when autoInstallPeers is false. A required peer satisfied this way is not reported as missing.
To prevent this workspace-wide peer reuse, set both autoInstallPeers and dedupePeerDependents to false. Dependencies of the workspace root can still provide peers when resolvePeersFromWorkspaceRoot is enabled.
Resolving a package's peer does not add that peer to your project's declared dependencies. If your project's scripts run a peer package's executable, declare that package directly in your project's dependencies or devDependencies.
例如,假设我们有一个包含两个项目的工作区,并且它们的依赖项中都有 webpack。 esbuild 在 webpack 的可选对等依赖内,而其中一个项目的依赖包含 esbuild。这种情况下,pnpm 会将两个 webpack 实例链接到 node_modules/.pnpm 目录:一个包含 esbuild ,另一个不包含。
node_modules
.pnpm
webpack@1.0.0_esbuild@1.0.0
webpack@1.0.0
project1
node_modules
webpack -> ../../node_modules/.pnpm/webpack@1.0.0/node_modules/webpack
project2
node_modules
webpack -> ../../node_modules/.pnpm/webpack@1.0.0_esbuild@1.0.0/node_modules/webpack
esbuild
这是有道理的,因为 webpack 在两个项目中使用,而其中一个项目没有 esbuild,因此这两个项目无法共享同一个 webpack 实例。然而,这并不是大多数开发者所期望的,特别是在提升的 node_modules 中,将会只有一个 webpack 实例。因此,当没有冲突的对等依赖项时,你现在可以使用 dedupePeerDependents 设置对 webpack 进行重复数据删除(最后有解释)。在这种情况下,如果我们将 dedupePeerDependents 设置为true,则两个项目将使用相同的 webpack实例,即已解析 esbuild 的实例:
node_modules
.pnpm
webpack@1.0.0_esbuild@1.0.0
project1
node_modules
webpack -> ../../node_modules/.pnpm/webpack@1.0.0_esbuild@1.0.0/node_modules/webpack
project2
node_modules
webpack -> ../../node_modules/.pnpm/webpack@1.0.0_esbuild@1.0.0/node_modules/webpack
esbuild
什么是对等依赖冲突? 我们指的对等依赖冲突是如下场景:
node_modules
.pnpm
webpack@1.0.0_react@16.0.0_esbuild@1.0.0
webpack@1.0.0_react@17.0.0
project1
node_modules
webpack -> ../../node_modules/.pnpm/webpack@1.0.0_react@17.0.0/node_modules/webpack
react (v17)
project2
node_modules
webpack -> ../../node_modules/.pnpm/webpack@1.0.0_react@16.0.0_esbuild@1.0.0/node_modules/webpack
esbuild
react (v16)
在这种情况下,我们无法对 webpack 去重,因为 react 在 webpack 的对等依赖内,而在两个项目上下文环境中,react 版本不同。
dedupePeers#
添加于:v10.33.0
- 默认值: false
- 类型:Boolean
启用后,对等依赖项后缀将使用仅包含版本信息的标识符(name@version)而不是完整的依赖项路径,从而消除嵌套后缀,例如(foo@1.0.0(bar@2.0.0))。 This reduces the number of package instances in projects with many recursive peer dependencies.
这与 dedupePeerDependents 不同,后者会对在不同工作区项目中具有相同对等依赖项的包进行去重。 dedupePeers 简化了对等点依赖后缀格式。
strictPeerDependencies#
- 默认值: false
- 类型:Boolean
如果启用了此选项,那么在依赖树中存在缺失或无效的 peer 依赖关系时,命令将执行失败。
resolvePeersFromWorkspaceRoot#
- 默认值:true
- 类型:Boolean
启用后,将会使用根工作区项目的依赖解析工作区中任何项目的对等依赖。这是一个有用的功能,因为你可以只在工作区的根目录中安装对等依赖,并且确保工作区中的所有项目都使用相同版本的对等依赖。
peerDependencyRules#
peerDependencyRules.ignoreMissing#
pnpm 不会打印有关依赖列表中缺少对 peerDependency 的警告。
例如,使用以下配置,如果依赖项需要 react 但 react 未被安装,pnpm 不会打印相应警告。
peerDependencyRules:
ignoreMissing:
- react
包名也可以使用模式匹配
peerDependencyRules:
ignoreMissing:
- "@babel/*"
- "@eslint/*"
ignoreMissing matches the peer package name or a glob. It does not support
the parent>peer or parent@range>peer selectors accepted by
allowedVersions.
peerDependencyRules.allowedVersions#
对于指定版本范围的 peerDependency,将不会打印未满足版本范围的警告。
例如,如果你有一些依赖项需要 react@16 但你知道它们可以与 react@17 一起正常工作,那么你可以使用以下配置:
peerDependencyRules:
allowedVersions:
react: "17"
这将告诉 pnpm 任何在其对等依赖中含有 react 的依赖都应该允许安装 react v17。
这还可以用来抑制指定包的对等依赖项引发的警告。例如,以下配置下 react v17 仅在其位于 button v2 包的对等依赖项中或任何 card 包的依赖项中时才被允许:
peerDependencyRules:
allowedVersions:
"button@2>react": "17"
"card>react": "17"
peerDependencyRules.allowAny#
allowAny 是一个匹配包名的数组,任何匹配该模式的对等依赖将可被解析为任意版本,无论 peerDependencies 指定的范围如何。例如:
peerDependencyRules:
allowAny:
- "@babel/*"
- "eslint"
上述设置将禁用任何与 @babel/ 或 eslint 有关的对等依赖版本不匹配的警告。
allowAny matches the peer package name or a glob. It does not support
the parent>peer or parent@range>peer selectors accepted by
allowedVersions.