存储与锁文件设置
存储设置#
storeDir#
- 默认值:
- 如果设置了 $PNPM_HOME 环境变量,则为 $PNPM_HOME/pnpm/rc
- 如果设置了 $XDG_DATA_HOME 环境变量,则为 $XDG_DATA_HOME/pnpm/store
- 在 Windows 上: ~/AppData/Local/pnpm/store
- 在 macOS 上:~/Library/pnpm/global
- 在 Linux 上:~/.local/share/pnpm/store
- 类型:路径
所有包被保存在磁盘上的位置。
该存储应始终位于进行安装的同一磁盘上,因此每个磁盘将有一个存储。如果在使用磁盘中具有主目录,存储目录就会创建在这里。如果磁盘上没有主目录,那么将在文件系统的根目录中创建该存储。例如,如果安装发生在挂载在 /mnt 的文件系统上,那么存储将在 /mnt/.pnpm-store 处创建。 Windows 系统上也是如此。
可以从不同的磁盘设置同一个存储,但在这种情况下,pnpm 将复制包而不是硬链接它们,因为硬链接只能发生在同一文件系统上。
If no directory above the project accepts a hard link at all (an agent sandbox
that grants write access only to the project, or a container with just the
project bind-mounted writable), the store is created inside the project instead:
at node_modules/.pnpm-store. In a workspace, that is the workspace root's node_modules/.pnpm-store.
Putting it in the home directory there would either fail on a read-only home or
land on another volume, which copies every package instead of hard-linking it
(#13525).
pnpm keeps the locks that coordinate processes sharing a store outside the
store, in a per-user directory. On Linux and macOS that directory is
$XDG_RUNTIME_DIR when it is an absolute path to a directory the user owns
and can create files in (write and search permission), and other users cannot
write to. Otherwise the directory is /tmp. On Windows it is ~/AppData/Local. A sandbox that blocks writes to
/tmp can set XDG_RUNTIME_DIR to a directory it allows. Processes that use
the same store should see the same XDG_RUNTIME_DIR, since pnpm processes only
wait for each other when they share the lock directory.
重要
pnpm 存储旨在仅供相互信任的用户、作业和进程共享。如果配置了共享的 storeDir,请通过文件系统权限对其进行保护,以防止不受信任的用户向其写入数据。该存储属于 pnpm 的信任域:其中的包可以通过硬链接方式引用,且存储索引文件(index.db)记录了用于验证缓存文件的哈希值。
verifyStoreIntegrity#
- 默认值:true
- 类型:Boolean
默认情况下,如果存储中的文件已被修改,则在将其链接到项目的 node_modules 之前会检查该文件的内容。如果 verifyStoreIntegrity 设置为 false,则在安装过程中不会检查内容可寻址存储中的文件。
此设置有助于检测意外的存储损坏。这并不能使一个可由不受信任用户写入的存储变得安全,因为能够向该存储写入数据的攻击者既可以篡改缓存的软件包内容,也可以篡改用于验证这些内容的元数据。
strictStorePkgContentCheck#
- 默认值:true
- 类型:Boolean
一些注册源允许以不同的包名或版本,发布完全相同的内容。这破坏了存储中包的有效性检查。为了避免在存储中验证此类软件包的名称和版本时出现错误,你可以将 strictStorePkgContentCheck 设置设为 false。
frozenStore#
- 默认值: false
- 类型:Boolean
Lets pnpm install run against a package store that lives on a read-only filesystem, for example a Nix store, a read-only bind mount, or an OCI image layer. 启用该功能后,pnpm 会以不可变模式打开存储目录中的 SQLite index.db 文件(从而绕过 WAL/-shm 辅助文件,因为这些文件无法在只读目录下创建),并禁用所有会向存储目录执行写入操作的代码路径。
Ordinary installs can run in parallel and populate the same store on a local filesystem on the same machine. Leave frozenStore disabled for that use case.
With frozenStore enabled, no other process may modify the store during the install. Immutable reads bypass SQLite locking and can report "database disk image is malformed" if another job changes the database. This restriction does not apply to --frozen-lockfile, which still allows writes to the store.
将其与 --offline 和 --frozen-lockfile 结合使用,并针对已完全填充的存储进行操作:
pnpm install --frozen-store --offline --frozen-lockfile
该存储必须已包含安装过程所需的一切,包括任何生命周期脚本已获批准(或已应用补丁)的包的构建产物。 Under the global virtual store, those package directories live inside the store, so if a required build is missing the install fails up front with ERR_PNPM_FROZEN_STORE_NEEDS_BUILD. Seed the store with those builds first.
frozenStore 与 --force 以及已配置的 pnpr 服务器不兼容,因为它们都会向存储中写入数据。 pnpm store add and pnpm store prune fail while it is enabled. 副作用缓存 也不会被写入。
storeIndexFormat#
新增于:v12.13.0
- Default: sqlite
- Type: sqlite, files
How the store records which files each package has.
sqlite: one SQLite database,index.db. SQLite's locking only works between processes on one machine, so this format is for a store on a local disk.files: one file per package in theindexdirectory of the store. Use it for a store on a network filesystem that several machines use at once, such as a store shared by CI jobs. Each entry is written to a temporary file and renamed into place, so concurrent jobs need no locks.
The format belongs to the store. pnpm creates the index directory the first time it writes to a store with storeIndexFormat: files, and from then on every pnpm process uses that directory for the store, whatever its own storeIndexFormat says. The packages recorded in an existing index.db are not carried over, so the first installs after switching download them again. To switch a store back to sqlite, delete its index directory.
storeDir: /mnt/shared/pnpm-store
storeIndexFormat: files
pnpm v11 reads only index.db, so it does not see the packages of a store that uses files.
锁文件设置#
锁文件#
- 默认值:true
- 类型:Boolean
当设置为 false 时,pnpm 不会读取或生成 pnpm-lock.yaml 文件。
lockfileDir#
- Default: the workspace root with a shared lockfile, otherwise the project directory
- 类型:路径
The directory in which pnpm-lock.yaml is created. Several projects may share a
single lockfile.
Setting lockfileDir to the workspace root of a workspace with a shared
lockfile, or to the project directory outside a workspace, has no effect. In a
workspace with sharedWorkspaceLockfile: false, setting lockfileDir makes the
projects share one lockfile in that directory.
The --lockfile-dir CLI option sets it for a single command. It cannot be used
with --global.
frozenLockfile#
- 默认值:
- 对于非 CI 环境:false
- 对于 CI 环境:如果存在 lock 文件,则为 true
- 类型:Boolean
When set to true, pnpm fails the installation if the lockfile needs updating
or is missing.
In CI environments, this setting defaults
to true when a lockfile is present.
preferFrozenLockfile#
- 默认值:true
- 类型:Boolean
当设置为 true 并且存在 pnpm-lock.yaml 满足
package.json 中的依赖关系时,执行无头安装。无头安装会跳过所有依赖项解析,因为它不需要修改锁文件。
excludeLinksFromLockfile#
- 默认值: false
- 类型:Boolean
When set to true, dependencies declared with the link: protocol are left
out of pnpm-lock.yaml. Dependencies that use the workspace: protocol are
still recorded.
lockfileIncludeTarballUrl#
- 默认值: false
- 类型:Boolean
将包的 tarball 的完整 URL 添加到 pnpm-lock.yaml 中的每个条目。
symlinkedLockfileTargetDirs#
Added in: v12.16.0
- 默认值:[]
- 类型:string[]
When pnpm-lock.yaml is a symlink, pnpm reads it through the link but refuses to write changes to it. List the directories that the lockfile may link into, and pnpm writes the updated lockfile to the file the symlink points to if that file is inside one of them. pnpm follows the whole chain of links, keeps every link in place, and keeps the target file's permissions. A lockfile that does not change is never written.
symlinkedLockfileTargetDirs:
- ~/build/src
Each entry must be an absolute path or start with ~/. pnpm ignores any other entry.
Set it in the global configuration file or with PNPM_CONFIG_SYMLINKED_LOCKFILE_TARGET_DIRS='["/path/to/src"]'. pnpm ignores the setting in pnpm-workspace.yaml, because the repository that commits the symlink must not be the one that allows writing through it.
lockfile.includeResolutionSettings#
Added in: v12.10.0
- 默认值: false
- 类型:Boolean
When set to true, pnpm records these settings in the settings section of pnpm-lock.yaml:
lockfile:
includeResolutionSettings: true
A lockfile that records other values than the current settings is outdated. pnpm install resolves it again, and pnpm install --frozen-lockfile fails with ERR_PNPM_LOCKFILE_CONFIG_MISMATCH. Turning this setting on makes the next install resolve the lockfile once.
With autoDedupe, an install reuses a lockfile that records autoDedupe: true when nothing else changed. This also holds on a machine that did not write the lockfile, such as a server that runs pnpm install --frozen-lockfile and then pnpm run.
The v13 format always records these settings.
pnpm v11 and earlier v12 versions do not keep these settings and remove them when they rewrite the lockfile. pnpm v12 versions before v12.10.0 fail to read a pnpm-workspace.yaml that sets lockfile to an object. Turn this setting on when everyone working on the project uses v12.10.0 or later.
This setting needs local dependency resolution, so an install that is not frozen fails when pnprServer is set.
lockfile.integrityTarget#
新增于:v12.15.0
- Default: resource
- Type: resource, tar
Which hash the integrity of each package tarball in pnpm-lock.yaml pins.
resourcepins the file at the tarball URL. That is the.tgzon most registries.tarpins the uncompressed tar inside it, for registry packages,tarball:URLs, andfile:tarballs alike.
lockfile:
integrityTarget: tar
With tar, the lockfile no longer changes when a registry recompresses its tarballs, starts serving tar registries, or is replaced by a mirror that does either. pnpm checks every download by decoding it and hashing the tar. The hash it records comes from a .tgz that matched the registry's published integrity, so it is as trustworthy as that integrity. Git dependencies and registry revisions are unaffected: git dependencies pin a commit, and revisions always pin the tar.
The lockfile records the setting as integrityTarget: tar in its settings. A lockfile resolved under the other value is outdated. pnpm install resolves it again and rewrites every hash, and pnpm install --frozen-lockfile fails with ERR_PNPM_LOCKFILE_CONFIG_MISMATCH.
The store remembers the tar hash of every .tgz it extracts, whatever this setting says. A version already in the store therefore needs no download to learn its tar hash, and a frozen install finds its files under either hash. A version the store has not extracted is downloaded once to learn the hash, even by pnpm install --lockfile-only, and so is an optional dependency for another platform, which the lockfile records but no install on this machine extracts. Packages extracted by pnpm versions without this setting are downloaded once too.
pnpm audit signatures checks the registry signature of the .tgz and ties it to the tar hash in the lockfile, through the store or by downloading the package.
pnpm versions before v12.15.0 cannot install from a lockfile that pins tars. Turn this setting on when everyone working on the project uses v12.15.0 or later. A resolution by pnprServer cannot pin tars, so an install that is not frozen fails when pnprServer is set.
lockfile.format#
- Default: v9
- Type: v9, v13
The format pnpm writes pnpm-lock.yaml in. v13 is the format planned for pnpm 13. It is experimental and may change before pnpm 13 is released.
lockfile:
format: v13
A lockfile in the v13 format is JSON, indented with tabs, with a trailing comma after every entry. That is valid YAML, so pnpm-lock.yaml keeps its name and tools that read it as YAML still can. Its lockfileVersion is 13.0, the pnpm major planned to write the format by default.
The format differs from today's in these ways:
- The workspace projects are listed under
projects. - There is one
packagesentry per package version and nosnapshotssection. The entry holds the package's metadata, its resolved dependencies, and underpeerDependencieseach peer's declared range beside the version it resolved to. - An ordinary dependency on a peer's provider under another name stays in the dependency map for that name.
- A peer that a dependency below the package needs, rather than the package itself, is recorded as
{version: 1.2.3, below: true}. A peer the package marks optional is written as{specifier: '*', optional: true}. There is nopeerDependenciesMetafield. - The tarball's
integrityis a field of the entry. A registry package and an injected workspace package have noresolutionfield. - A local directory package records the dependencies its manifest declares, after hooks, in its
dependenciesandoptionalDependencies. A dependency is its specifier alone when the specifier is also the version. Otherwise it is{specifier: ^1.0.0, version: 1.2.3}, withoutversionwhen the package does not link the dependency in every snapshot. Frozen installs compare these specifiers with the current manifest. Changed ranges invalidate the lockfile even when the locked versions still satisfy them. A lockfile converted from thev9format records no specifiers and keeps the existing freshness checks. - A project or local directory package dependency marked
injectedindependenciesMetahasinjected: true. The format records no otherdependenciesMetafield. - A package installed with different peer dependencies lists what differs under
snapshotsin its entry. - Transitive peers without an external provider are normally inferred from dependencies. When necessary,
unboundPeerDependenciesrecords their names explicitly, including an empty list. - A reference between packages is a version, with no peer or patch suffix. Where that version alone does not say which copy of a package is meant, the reference names the copy by the peer dependencies it was resolved with, under
with, one per line. An emptywithmap keeps optional peers unbound when another copy binds them. A peer reference can carry its ownwithmap when its provider has different peer bindings. - A project's dependency is its specifier alone when the version follows from it: an exact version, a
catalog:entry, or aworkspace:dependency on another workspace project. Otherwise it is{specifier: ^1.0.0, version: 1.2.3}on one line. A dependency with awithmap leaves out a version that follows from its specifier too. - A project that a
workspace:dependency links to records itsname. - The hash of a patched package is recorded once, under
patchedDependencies. A package's deprecation message is not recorded. - The
settingssection holds everything the lockfile records frompnpm-workspace.yaml: the resolution settings,catalogs,overrides,patchedDependencies,ignoredOptionalDependenciesand the two checksums. A catalog entry is its specifier alone when the version follows from it. - The resolution settings include the ones
lockfile.includeResolutionSettingslists, whatever that setting says.
When a peer dependency is updated, only the lines that record it change.
Reading and writing the format#
pnpm reads lockfiles in both formats whatever this setting is. A regular install converts the lockfile when you change this setting. pnpm install --lockfile-only also writes the selected format. Versions of pnpm without this setting cannot read a lockfile in the v13 format. The format is still changing, so a lockfile written with this setting may need regenerating with a later pnpm.
The v13 format records the resolution settings. The next install resolves again a lockfile that does not record them yet, such as a v9 lockfile written without lockfile.includeResolutionSettings, or a v13 lockfile written by an earlier pnpm. pnpm install --frozen-lockfile fails on such a lockfile until a regular install records them. An install that is not frozen fails when pnprServer is set, because pnpr does not receive these settings.
The boot document that records config dependencies and the pinned pnpm version follows the pinned pnpm, not this setting. While the project pins pnpm 13 or earlier, it keeps today's format, so that a pnpm from before this format can read the pinned version and switch to it. A project that pins pnpm 14 or later gets a boot document in the v13 format.
gitBranchLockfile#
- 默认值: false
- 类型:Boolean
When set to true, the generated lockfile name after installation will be named
based on the current branch name to completely avoid merge conflicts. 例如,如果当前分支名称为 feature-foo,则
对应的锁文件名称将为
pnpm-lock.feature-foo.yaml 而不是 pnpm-lock.yaml。 It is typically used
in conjunction with the command line argument --merge-git-branch-lockfiles or by
setting mergeGitBranchLockfilesBranchPattern in the pnpm-workspace.yaml file.
mergeGitBranchLockfilesBranchPattern#
- 默认值: null
- 类型:Array 或 null
This configuration matches the current branch name to determine whether to merge
all git branch lockfile files. By default, you need to manually pass the
--merge-git-branch-lockfiles command line parameter. This configuration allows
this process to be automatically completed.
例如:
mergeGitBranchLockfilesBranchPattern:
- main
- release*
你还可以使用 ! 来排除模式。
peersSuffixMaxLength#
- 默认值:1000
- 类型:number
添加到锁文件中的依赖项键的 peer ID 后缀的最大长度。如果后缀较长,则用井号替换。