对等依赖设置

autoInstallPeers#

  • 默认值:true
  • 类型:Boolean

当值为 true 时,将自动安装任何缺少的非可选对等依赖。

选择的版本是符合对等范围的最高版本。因为自动安装的对等依赖不是项目声明的依赖,resolutionMode 将其视为间接依赖,而不是直接依赖,因此 lowest-direct 不会使其降级。

Optional peer dependencies#

pnpm installs a missing optional peer dependency only if a version of it that satisfies the optional peer range is already installed. In that case, pnpm resolves the optional peer to that version.

When dedupePeerDependents is true, 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.

When dedupePeerDependents is false, or projectScopedOptionalPeers is true, that version must come from the project's own dependencies. The workspace root's dependencies also count if resolvePeersFromWorkspaceRoot is true.

pnpm skips that version if installing it would leave the project with an unmet peer dependency. This covers the version's own peers, the required peers that pnpm would install with it, and their peers in turn. dedupePeerDependents does not give the skipped peer back to the project by sharing another project's instance of the package that declares it.

pnpm also resolves optional peers this way when autoInstallPeers is false and dedupePeerDependents is true. To stop it, set both settings to false.

When selecting a workspace provider for a new project, pnpm prefers compatible optional peer versions already pinned in the lockfile.

A locked version can guide optional peer selection while another dependency still provides it. When you remove the last provider, pnpm install drops the binding even if the dependency that declares the optional peer stays on the same version. To clean up a binding left by an earlier install, run pnpm dedupe or pnpm install --fix-lockfile.

Version conflicts#

如果来自不同软件包的对等依赖项的需求版本存在冲突,那么 pnpm 将不会自动安装任何版本的冲突的对等依赖项。相反,会输出一条警告信息。比如,如果一个依赖项需要 react@^16.0.0,而另一个需要 react@^17.0.0,则会产生冲突,自动安装将不会进行。

Conflict resolution#

如果出现版本冲突,你需要评估自己安装哪个版本的对等依赖项,或更新依赖项以符合其对等依赖项要求。 You can also set autoInstallPeersFromHighestMatch to true.

autoInstallPeersFromHighestMatch#

  • 默认值: false
  • 类型:Boolean

When set to true, pnpm joins conflicting peer dependency ranges with || and installs the highest version that satisfies any of them. For example, if one dependency requires react@^16.0.0 and another requires react@^17.0.0, pnpm installs the highest react@17.

Only semver ranges can be joined. Conflicting paths, aliases, or tags leave the peer dependency missing. Identical non-semver specifiers are preserved.

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. With autoInstallPeers: false, pnpm peers check reports a separate advisory when the project and its dependency ancestors do not declare a provider.

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 实例。 Therefore, with the default dedupePeerDependents: true, pnpm deduplicates webpack when it has no conflicting peer dependencies (explanation at the end). Both projects use the same webpack instance, which is the one that has esbuild resolved:

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 版本不同。

projectScopedOptionalPeers#

Added in: v12.19.0

  • 默认值: false
  • 类型:Boolean

When true, a project gets a missing optional peer dependency only if the project's own dependencies already install a version of it. The workspace root's dependencies also count if resolvePeersFromWorkspaceRoot is true. A version that only another workspace project installs does not count.

dedupePeerDependents keeps working for everything else. It still deduplicates packages across projects, unless deduplicating would give a project an optional peer its own dependencies don't install. Required peers are not affected.

In the dedupePeerDependents example, project1 keeps the webpack instance without esbuild.

This setting has no effect when dedupePeerDependents is false, because optional peers are already resolved only from the project's own dependencies then.

Changing this setting changes the lockfile. pnpm records it in the lockfile's settings while it takes effect, so the next install resolves the lockfile again.

dedupePeers#

Deprecated. Remove this setting. For lockfile references without peer suffixes, use lockfile.format: v13.

  • 默认值: 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 简化了对等点依赖后缀格式。

addMissingPeerTypes#

Added in: v12.12.0

  • 默认值: false
  • 类型:Boolean

When true, every peer dependency of a dependency also makes the matching @types package an optional peer of it. A package with a peer dependency on react gets an optional peer dependency on @types/react, and one on @babel/core gets @types/babel__core. A package that already declares the @types package keeps its own declaration. pnpm does not install an @types package that nothing in your workspace depends on.

Many packages peer-depend on a library without declaring its @types package. TypeScript then looks for the types next to the package's own location. With enableGlobalVirtualStore that location is outside your project, so the types your project installs are not found. With this setting, pnpm links the @types package next to every package that peer-depends on the library, as it does with any optional peer dependency.

Changing this setting changes the lockfile.

strictPeerDependencies#

  • 默认值: false
  • 类型:Boolean

如果启用了此选项,那么在依赖树中存在缺失或无效的 peer 依赖关系时,命令将执行失败。

resolvePeersFromWorkspaceRoot#

  • 默认值:true
  • 类型:Boolean

启用后,将会使用根工作区项目的依赖解析工作区中任何项目的对等依赖。这是一个有用的功能,因为你可以只在工作区的根目录中安装对等依赖,并且确保工作区中的所有项目都使用相同版本的对等依赖。

peerDependencyRules#

注意

Deprecated: peerDependencyRules only suppresses peer dependency warnings after resolution without changing peerDependencies. When you want to change or remove the declared peer dependency ranges so peer resolution itself is updated, use overrides with :peer:

  • peerDependencyRules.ignoreMissing: ["react", "@babel/*"] only silences missing-peer warnings. The overrides entries "react:peer": "-" and "@babel/*:peer": "-" remove the peer declarations and their peerDependenciesMeta entries, so packages no longer receive those peers from their parents.
  • peerDependencyRules.allowedVersions: { "button@2>react": "17" } only changes peer-mismatch reporting and has no behavior-preserving overrides replacement. Use "button@2>react:peer": "16 || 17" in overrides when you intend to widen the declared peer range and change peer resolution.
  • peerDependencyRules.allowAny: ["@babel/*"] only suppresses peer-version mismatch warnings and does not rewrite peerDependencies, so it has no direct overrides equivalent. Use "@babel/*:peer": "*" in overrides only when you intend to widen the declared peer range and change peer resolution.

peerDependencyRules continues to work for backward compatibility.

peerDependencyRules.ignoreMissing#

pnpm 不会打印有关依赖列表中缺少对 peerDependency 的警告。

例如,使用以下配置,如果依赖项需要 react 但 react 未被安装,pnpm 不会打印相应警告。

pnpm-workspace.yaml
peerDependencyRules:
  ignoreMissing:
  - react

包名也可以使用模式匹配

pnpm-workspace.yaml
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 一起正常工作,那么你可以使用以下配置:

pnpm-workspace.yaml
peerDependencyRules:
  allowedVersions:
    react: "17"

这将告诉 pnpm 任何在其对等依赖中含有 react 的依赖都应该允许安装 react v17。

这还可以用来抑制指定包的对等依赖项引发的警告。例如,以下配置下 react v17 仅在其位于 button v2 包的对等依赖项中或任何 card 包的依赖项中时才被允许:

pnpm-workspace.yaml
peerDependencyRules:
  allowedVersions:
    "button@2>react": "17"
    "card>react": "17"

peerDependencyRules.allowAny#

allowAny 是一个匹配包名的数组,任何匹配该模式的对等依赖将可被解析为任意版本,无论 peerDependencies 指定的范围如何。例如:

pnpm-workspace.yaml
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.