Catalogs
“Catalogs” 是一个 工作空间功能,可将依赖项版本定义为可复用常量。目录中定义的常量稍后可以在 package.json 文件中引用。
The catalog protocol (catalog:)#
在 pnpm-workspace.yaml 中定义目录,
packages:
- packages/*
# 定义目录和依赖版本号
catalog:
react: ^18.3.1
redux: ^5.0.1
可以使用 catalog: 协议来代替依赖项版本。
{
"name": "@example/app",
"dependencies": {
"react": "catalog:",
"redux": "catalog:"
}
}
这等于直接写入一个版本范围(例如 ^18.3.1)。
{
"name": "@example/app",
"dependencies": {
"react": "^18.3.1",
"redux": "^5.0.1"
}
}
你可以在以下字段中使用 catalog: 协议:
package.json:dependenciesdevDependenciespeerDependenciesoptionalDependencies
pnpm-workspace.yamloverrides
pnxspecifiers, such aspnx shx@catalog:
The dependencies of a local directory installed as a copy, such as a file: dependency or an injected workspace package, can use the catalog: protocol too. They resolve against the catalogs of the workspace that installs the directory.
When a local directory package uses a catalog entry for a peer dependency, pnpm records that entry in the lockfile. Run pnpm install after changing the entry so frozen installs can use the updated lockfile.
Catalog 协议允许在冒号后使用可选名称 (例如:catalog:name) 来指定应使用哪个目录。当省略名称时,将使用默认目录。
根据不同的场景,catalog: 协议相比于直接编写版本范围提供了一些优势 ,将在下文中详细介绍。
优势#
在工作空间(即 monorepo 或多包 repo)中,许多包使用相同的依赖项是很常见的。在编写 package.json 文件时,目录可以减少重复,并在此过程中提供一些好处:
- Maintain unique versions: It's usually desirable to have only one version of a dependency in a workspace. Catalog 让工作区内共同依赖项的版本更容易维护。重复的依赖关系可能会在运行时冲突并导致错误。当使用打包器时,不同版本的重复依赖项也会增大项目体积。
- Easier upgrades: When upgrading a dependency, only the catalog entry in
pnpm-workspace.yamlneeds to be edited rather than allpackage.jsonfiles using that dependency. This saves time. Only one line needs to be changed instead of many. - Fewer merge conflicts: Since
package.jsonfiles do not need to be edited when upgrading a dependency, git merge conflicts no longer happen in these files.
Defining catalogs#
目录在 pnpm-workspace.yaml 文件中定义。有两种方法来定义目录。
- 使用 (单数)
catalog字段创建名为default的目录。 - 使用 (复数)
catalogs字段创建任意命名的目录。
提示
If you have an existing workspace that you want to migrate to using catalogs, run pnpm catalog migrate:
pnpm catalog migrate
Default catalog#
顶层 catalog 字段允许用户定义一个名为 default 的目录。
catalog:
react: ^18.2.0
react-dom: ^18.2.0
这些版本范围可以通过 catalog:default 引用。仅有默认目录时,也可以使用特殊的 catalog: 简写。将 catalog: 视为可扩展为 catalog:default 的简写。
The default catalog can also be written as catalogs.default. Defining both catalog and catalogs.default fails with "The 'default' catalog was defined multiple times".
Named catalogs#
可以在 catalogs 键下配置具有名称任意选择的多个catalog。
catalogs:
# 可以通过 "catalog:react17" 引用
react17:
react: ^17.0.2
react-dom: ^17.0.2
# 可以通过 "catalog:react18" 引用
react18:
react: ^18.2.0
react-dom: ^18.2.0
默认目录可以与多个命名目录一起定义。这对正在更新依赖项版本的大型多包存储库可能会很有用。
catalog:
react: ^16.14.0
react-dom: ^16.14.0
catalogs:
# 可以通过 "catalog:react17" 引用
react17:
react: ^17.0.2
react-dom: ^17.0.2
# 可以通过 "catalog:react18" 引用
react18:
react: ^18.2.0
react-dom: ^18.2.0
Workspace dependencies in a catalog#
Added in: v12.2.0
A catalog entry may hold a workspace: range, so the version a workspace dependency is linked by is written once too:
catalog:
'@example/utils': workspace:^
{
"name": "@example/app",
"dependencies": {
"@example/utils": "catalog:"
}
}
pnpm expands the catalog: reference to workspace:^ and then links the workspace project, exactly as if the package.json had declared workspace:^ itself. On publish both protocols are replaced, so the example above ships as "^1.4.0" when @example/utils is at 1.4.0.
Local dependencies in a catalog#
添加于:v12.6.0
Catalog entries can use the file: and link: protocols (#8642). A relative path in an entry, such as file:./tarballs/foo.tgz, workspace:./packages/foo, or a bare ./tarballs/foo.tgz, is measured from the directory holding pnpm-workspace.yaml:
catalog:
my-lib: link:./packages/my-lib
my-tarball: file:./tarballs/foo.tgz
{
"name": "@example/app",
"dependencies": {
"my-lib": "catalog:",
"my-tarball": "catalog:"
}
}
Adding dependencies to a catalog#
pnpm add --save-catalog writes the new dependency's range to the default catalog and catalog: to package.json. --save-catalog-name picks a named catalog. catalog.mode controls whether pnpm add adds to the default catalog without the flag.
Errors#
ERR_PNPM_CATALOG_ENTRY_NOT_FOUND_FOR_SPEC: the catalog has no entry for the dependency.ERR_PNPM_CATALOG_ENTRY_INVALID_RECURSIVE_DEFINITION: a catalog entry is itself acatalog:reference.
发布#
运行 pnpm publish 或者 pnpm pack 时,catalog: 协议将被移除。这与 workspace: 协议 类似,该协议在发布时也会被替换。
例如,
{
"name": "@example/components",
"dependencies": {
"react": "catalog:react18"
}
}
在发布时将变为以下内容。
{
"name": "@example/components",
"dependencies": {
"react": "^18.3.1"
}
}
catalog: 的替换过程允许其他工作区或包管理器使用 @example/components 包。
设置#
catalogMode#
Set catalog.mode alongside catalog.entries to choose how pnpm add uses the default catalog. The top-level catalogMode setting accepts the same values. When a file sets both, catalog.mode wins.
- 默认值: manual
- 类型:manual、strict、prefer
控制运行 pnpm add 时是否以及如何将依赖项添加到默认目录中。有三种模式:
- strict - 仅允许来自目录的依赖版本。在目录版本范围之外添加依赖关系会导致错误。
- prefer - 优先使用目录版本,但如果未找到兼容版本,则会退回到直接依赖项。
- manual(默认)——不会自动将依赖项添加到目录中。
自 v12.3.0 起,指定位置而非版本的说明符永远不会被移入目录:本地路径、本地 tarball、tarball URL 和 workspace:\<path> 范围会保留在声明它们的 package.json 中。此类说明符相对于其自身项目解析,因此一个目录条目无法对引用它的每个项目都表示相同的目录。
catalog.defaultSaveTarget#
Set catalog.defaultSaveTarget alongside catalog.entries to choose where pnpm add saves a dependency. Use default for the default catalog or supply a named catalog. The command-line flags --save-catalog and --save-catalog-name take precedence.
catalog:
entries: {}
mode: prefer
defaultSaveTarget: tools
The top-level saveCatalogName setting continues to work. When a file sets both, catalog.defaultSaveTarget wins. If you define the default catalog under catalogs.default, use the top-level settings or move its entries to catalog.entries before using the nested settings.
catalog.entries and catalog.unused#
Put the default catalog's packages under catalog.entries to configure cleanup alongside them. Use entries: {} for an empty map. Without entries, a key named unused refers to a package.
catalog:
unused: prune
entries:
example-tool: ^2.0.0
catalog.unused accepts ignore (the default) or prune. Pruning covers the default catalog and all named catalogs in catalogs. When a file sets both catalog.unused and catalogPrune, catalog.unused wins.
catalogPrune#
- 默认值: false
- 类型:Boolean
当设置为 true 时,pnpm 将在安装期间删除未使用的目录条目。
cleanupUnusedCatalogs 是该设置已弃用的名称,但仍可继续使用;若两者同时设置,则以 catalogPrune 为准。