카탈로그
"Catalogs"는 종속성 버전 범위를 재사용 가능한 상수로 정의하기 위한 워크스페이스 기능입니다. 카탈로그에 정의된 상수는 나중에 package.json 파일에서 참조할 수 있습니다.
The catalog protocol (catalog:)#
pnpm-workspace.yaml에 카탈로그가 정의되면,
packages:
- packages/*
# Define a catalog of version ranges.
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: 프로토콜은 버전 범위를 직접 작성하는 것에 비해 몇 가지 장점이 있으며, 이에 대해서는 다음에 자세히 설명합니다.
장점#
워크스페이스(즉, 모노레포 또는 다중 패키지 저장소)에서는 동일한 종속성을 여러 패키지가 사용하는 것이 일반적입니다. 카탈로그는 package.json 파일을 작성할 때 중복을 줄여 주며, 그 과정에서 다음과 같은 이점을 제공합니다:
- Maintain unique versions: It's usually desirable to have only one version of a dependency in a workspace. 카탈로그를 사용하면 이를 더 쉽게 유지 관리할 수 있습니다. 중복된 종속성은 런타임에 충돌하여 버그를 일으킬 수 있습니다. 또한 번들러를 사용할 때 중복은 크기를 증가시킵니다.
- 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 키 아래에 구성할 수 있습니다.
catalogs:
# Can be referenced through "catalog:react17"
react17:
react: ^17.0.2
react-dom: ^17.0.2
# Can be referenced through "catalog:react18"
react18:
react: ^18.2.0
react-dom: ^18.2.0
기본 카탈로그는 여러 개의 이름이 지정된 카탈로그와 함께 정의할 수 있습니다. 이는 종속성을 점진적으로 새 버전으로 마이그레이션하는 대규모 다중 패키지 저장소에서 유용할 수 있습니다.
catalog:
react: ^16.14.0
react-dom: ^16.14.0
catalogs:
# Can be referenced through "catalog:react17"
react17:
react: ^17.0.2
react-dom: ^17.0.2
# Can be referenced through "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#
Added in: 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 (기본값) - 종속성을 카탈로그에 자동으로 추가하지 않습니다.
Since v12.3.0, a specifier that names a location rather than a version is never moved into a catalog: local paths, local tarballs, tarball URLs, and workspace:\<path> ranges stay in the package.json that declares them. Such a specifier is resolved relative to its own project, so one catalog entry could not mean the same directory for every project referencing it.
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이 우선합니다.