Settings (pnpm-workspace.yaml)
pnpm gets its configuration from the command line, environment variables, pnpm-workspace.yaml
, and
.npmrc
files.
The pnpm config
command can be used to read and edit the contents of the project and global configuration files.
The relevant configuration files are:
- Per-project configuration file:
/path/to/my/project/pnpm-workspace.yaml
- Global configuration file:
~/.config/pnpm/rc
(an INI-formatted list ofkey = value
parameters)
Authorization-related settings are handled by npm's configuration system. So, pnpm config set registry=<value>
will actually save the setting to npm's global configuration file.
Values in the configuration files may contain env variables using the ${NAME}
syntax. 也可以使用默认 值指定环境变量。 使用 ${NAME-fallback}
将在未设置 NAME
时返回 fallback
。 ${NAME:-fallback}
,会在 NAME
不存在或为空字符串时返回 fallback
。
Dependency Resolution
overrides
此字段允许您指示 pnpm 覆盖依赖关系图中的任何依赖项。 这对于强制所有软件包使用一个依赖项的单个版本、反向移植一个修复、用分叉替换依赖项或删除未使用的依赖项很有用。
请注意,overrides 字段只能在项目的根目录下设置。
An example of the overrides
field:
overrides:
"foo": "^1.0.0"
"quux": "npm:@myorg/quux@^1.0.0"
"bar@^2.1.0": "3.0.0"
"qar@1>zoo": "2"
你可以用 ">" 来覆盖某个包下的子依赖的版本,比如 qar@1>zoo
只会覆盖 qar@1
依赖的 zoo
的版本,而不会影响其他依赖。
一个overide可以被定义为直接依赖项的规则的引用。
这通过依赖名称前缀一个 $
实现:
{
"dependencies": {
"foo": "^1.0.0"
}
}
overrides:
foo: "$foo"
被引用的包不必匹配需要覆盖的包:
overrides:
bar: "$foo"
If you find that your use of a certain package doesn't require one of its dependencies, you may use -
to remove it. For example, if package foo@1.0.0
requires a large package named bar
for a function that you don't use, removing it could reduce install time:
overrides:
"foo@1.0.0>bar": "-"
此功能对于 optionalDependencies
特别有用,其中大多数可选包可以被安全地跳过。
packageExtensions
packageExtension
字段提供了一种用额外信息扩展现有软件包定义的方法。 例如,如果 react-redux
本应该在它的 peerDependencies
中包含 react-dom
但它没有,则可以用 packageExtensions
来填补上 react-redux
。
packageExtensions:
react-redux:
peerDependencies:
react-dom: "*"
packageExtensions
中的键是包名或包名和 semver 范围,因此可以只修补包的某些版本:
packageExtensions:
react-redux@1:
peerDependencies:
react-dom: "*"
以下字段可以使用 packageExtensions
进行扩展: dependencies
、optionalDependencies
、peerDependencies
和 peerDependenciesMeta
。
一个更大的例子:
packageExtensions:
express@1:
optionalDependencies:
typescript: "2"
fork-ts-checker-webpack-plugin:
dependencies:
"@babel/core": "1"
peerDependencies:
eslint: ">= 6"
peerDependenciesMeta:
eslint: {
optional: true
我们与 Yarn 一起维护一个 packageExtensions
的数据库,以便修补在生态系统中损坏的包。
如果你使用 packageExtensions
, 考虑发送一个 PR 上游并将你的扩展贡献给 [@yarnpkg/extension
] 数据库。
allowedDeprecatedVersions
此项设置允许忽略特定依赖包的 deprecation 警告。
示例:
allowedDeprecatedVersions:
express: "1"
request: "*"
使用上述配置,pnpm 将不会打印 request
任何版本和 express
v1 版本的弃用警告。
updateConfig
updateConfig.ignoreDependencies
有时您无法更新依赖项。 例如,最新版本的依赖项开始使用 ESM,但您的项目尚未采用 ESM。 恼人的是,这样的包将始终被 pnpm outdated
命令打印出来,并在运行 pnpm update --latest
时更新。 但是,你可以在 ignoreDependencies
字段中列出你不想更新的包:
updateConfig:
ignoreDependencies:
- load-json-file
模式匹配也是支持的,因此你可以忽略在特定范围内的任何包: @babel/*
。
支持的架构
你可以指定要安装的可选依赖项的架构,即使它们与运行安装的系统的架构不匹配。
例如,以下配置指示安装 Windows x64 的可选依赖项:
supportedArchitectures:
os:
- win32
cpu:
- x64
而此配置将为 Windows、macOS 以及当前正在运行安装的系统架构安装可选依赖项。 它包括 x64 和 arm64 CPU 的工件:
supportedArchitectures:
os:
- win32
- darwin
- current
cpu:
- x64
- arm64
另外, supportedArchitectures
还支持指定系统的 libc
。