pnpm update
别名:up、upgrade
pnpm update 基于指定的范围更新包到它们的最新版本。
在不带参数的情况下使用时,将更新所有依赖关系。
摘要:#
| 命令 | 含义 |
|---|---|
pnpm up |
更新所有依赖项,遵守 package.json 中指定的范围 |
pnpm up --latest |
将所有依赖项更新到最新版本 |
pnpm up --tag next |
Updates all dependencies to the versions behind their next tags |
pnpm up foo@2 |
将 foo 更新到 v2 上的最新版本 |
pnpm up "@babel/*" |
更新 @babel 范围内的所有依赖项 |
更新内容写入什么#
Besides moving pnpm-lock.yaml to the newly resolved versions, pnpm update writes the new range back to the place the dependency is declared:
- In
package.json, the range is moved onto the resolved version while the operator the dependency already declared is kept, so^1.1.0stays a caret range. - A dependency declared through the
catalog:protocol is not rewritten inpackage.json. The catalog entry it points at is updated instead, inpnpm-workspace.yaml. - A dependency declared through the
jsr:protocol keeps itsjsr:prefix, and its range moves like an npm range. - A dependency declared through a dist-tag, such as
"foo": "latest", keeps tracking the tag. The tag stays inpackage.jsonand only the lockfile moves to the version behind it, with--latestas well. - Since v12.12.0, a config dependency is updated too, and its new range is written to
pnpm-workspace.yaml. - A runtime (
node,deno,bundeclared indevEngines.runtime,engines.runtime, or through theruntime:/tool:protocol) and a package manager (pnpm,npm,yarn,bundeclared indevEngines.packageManagerorpackageManager) are updated too, and their new versions or ranges are written back topackage.json(andpnpm-lock.yamlwhen the pin is recorded there). A Node.js runtime range such as>=20 <23keeps its bounds when the updated stable version satisfies it. Anengines.pnpmrange moves with the pnpm pin, the waypnpm self-updatemoves it. - When
cargo.enabledistrue, Cargo dependencies inCargo.tomlandCargo.lockare updated too. Passcrate:<name>(orcrate:<name>@<req>) to target specific crates. - Since v12.25.0, when
cargo.enabledistrue,--latestalso moves the Rust release that a toolchain file pins. Passrustto update only the toolchain, or!rustto leave it out. The toolchain counts as a dev dependency, andupdateConfig.ignoreDependenciescan hold it back. - When
python.enabledistrue, Python dependencies inpyproject.tomlandpylock.tomlare updated too. Passpypi:<name>(orpypi:<requirement>) to target specific Python distributions.
Recursive and filtered updates include standalone Python and Cargo workspace members. Python name patterns ignore case and treat runs of dots, underscores, and hyphens as one hyphen. Cargo selectors match both the dependency alias and the crate name.
- Since v12.6.0, a dependency whose specifier comes from a
packageExtensionsentry, areadPackagehook, or anoverrideis not written topackage.json. 锁文件会保留该说明符。pnpm update和pnpm audit --fix=update仍会在范围内移动它。
来自这些来源之一的精确固定版本会保持原样。当该固定版本是易受攻击的版本时,pnpm audit --fix=update 会发出警告并指向 pnpm audit --fix,后者会写入一个 override。 override 在 packageExtensions 和 readPackage 之后应用,因此它会替换这些来源提供的说明符。
Pass --no-save to update the lockfile only and leave the declared ranges alone.
Since v12.0.0, --patches does something different
from all of this: it changes no version and no declared range, refreshing only
which registry artifact each locked version resolves to. That still rewrites
lockfile metadata (the integrity, the revision, and the package snapshot
around them), because artifact revisions may declare different dependencies. See
Registry revisions.
更新到指定版本#
In manual catalog mode, pnpm update foo@2.0.0 moves a shared catalog entry from ^1.0.0 to ^2.0.0. If you request a range, pnpm writes that range to the catalog entry. A version or range update fails if the entry points to a local directory.
pnpm update <name>@<version> fails with ERR_PNPM_UPDATE_VERSION_ON_INDIRECT_DEP when the package is not a direct dependency of any selected project. 在这种情况下无处记录版本;请改为通过 overrides 固定传递依赖。范围和标签不受影响。
使用模式匹配选择依赖项#
你可以使用模式来更新特定的依赖项。
更新所有 babel 包:
pnpm update "@babel/*"
更新所有依赖项,除了 webpack:
pnpm update "\!webpack"
模式也可以组合,因此下一个命令将更新所有 babel 包,除了 core:
pnpm update "@babel/*" "\!@babel/core"
更新 GitHub Actions#
pnpm outdated 可以检查仓库工作流文件所引用的 GitHub Actions 是否有更新,而 pnpm update 可以更新它们。每条命令都需要显式开启:传入 --include-github-actions,或在 pnpm-workspace.yaml 中把 update.githubActions 设为 true 以默认启用。
更新后的操作被锁定至具体的提交哈希,并且它们的发布标签保留在注释中:
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
Local actions are followed rather than looked up: a ./-relative reference, and
since v12.3.0 GitHub's self-repository spelling
(uses: $/.github/actions/setup), resolve inside the repository.
检查更新会对每个被引用的仓库执行 git ls-remote。无法读取 ref 的 action(例如私有仓库中的 action)会被跳过并给出警告。 If the actions are hosted on a different GitHub server (such as a GitHub Enterprise Server), set update.githubActionsServer.
配置项#
--recursive, -r#
同时在所有子目录中使用 package.json (不包括
node_modules) 运行更新。
用法示例:
pnpm --recursive update
# updates direct dependencies only
pnpm --recursive update --depth 0
# update typescript to the latest version in every package
pnpm --recursive update typescript@latest
--depth <number>#
- Default: Infinity
- 类型:Number
要更新到依赖树的多少层深。 0 updates direct dependencies only.
--latest, -L#
将依赖项更新至由 latest 标签决定的最新稳定版本(有可能会跨越主版本号),只要 package.json 中指定的版本范围低于 latest 标签的版本号(即不会对预发布版本降级)。
Since v12.6.0, --latest does not move a dependency past a specifier supplied by packageExtensions, a readPackage hook, or an override. 锁文件会保持在该说明符范围内。
Since v12.23.0, pnpm update --latest <name> moves the override keyed by the bare name of a dependency it selects. The override keeps its range style, so 1.0.0 becomes the new exact version and ^1.0.0 becomes ^<new version>. The package.json entry stays as it is. pnpm update --interactive --latest does the same for the dependencies you select. pnpm prints a warning when it cannot move the override, for example an npm: alias, a $ reference, or a catalog: value. --latest without selectors leaves overrides alone.
A Cargo requirement keeps its operator when updated, so ~1.0 becomes ~1.1
and =1.0.0 stays exact. A requirement with only an upper bound, such as <2,
is kept while the new version matches it, and becomes a caret requirement, such
as ^2.0.0, when --latest moves past it. A requirement in the selector, as in
pnpm update crate:foo@^2, replaces it. savePrefix and saveExact apply only
to new Cargo dependencies. 保存的 Cargo 需求会省略构建元数据。
A pinned Rust release keeps its form. 1.95.0 becomes the newest stable
version, 1.95 the newest stable minor line, and nightly-2026-01-01 the
newest nightly. A channel that moves by itself, such as stable, stays as it is.
A Python requirement keeps its operator and precision too, so ~=1.0 becomes
~=2.0 and ==1.* becomes ==2.*. A requirement with only an upper bound,
such as <2, or with several clauses, such as >=1.0,<2, becomes >=2.0 when
--latest moves past it. A selected pre-release that the requirement does not
name, such as 1.4rc1 for ==1.*, adds a clause that names it, as in
==1.*,>=1.4rc1. savePrefix and saveExact do not apply to Python
requirements.
--tag <tag>#
Added in: v12.23.0
Update the dependencies to the version behind the given dist-tag, and write
that version to package.json with the range operator the dependency already
declares. The tag's version is used even when it is lower than the installed
version. pnpm up --tag next foo does the same as pnpm up foo@next.
A dependency that tracks a dist-tag, such as "foo": "latest", is switched to
the given tag. A dependency declared through the catalog:, workspace:, or
npm: protocol, through a named registry, or as a path or git URL, is not
rewritten. Config dependencies are not updated. A package that does not publish
the tag fails the update.
With --global, a package that does not publish the tag keeps its range, and
the other packages are updated. A global package also keeps its installed
version when the tag's version is lower.
The value must be a dist-tag name. A version or a range, such as 1.2.3 or
*, is rejected (ERR_PNPM_UPDATE_INVALID_TAG). --tag cannot be combined
with --latest, --workspace, --patches, or a selector that names a
version, such as foo@2 (ERR_PNPM_TAG_WITH_SPEC).
--patches#
添加于:v12.0.0
Refresh registry revisions without changing any
package version: for every locked registry package, pnpm resolves current
metadata for the same name@version, and adopts the artifact the registry
selects now if that changed. Versions and declared ranges are left alone, but the
lockfile entry is rewritten as a whole (integrity, revision, and the package
snapshot around them), since revisions may declare different dependencies.
Cannot be combined with package selectors, --latest, --tag, --interactive,
or --global (ERR_PNPM_PATCHES_WITH_SELECTOR).
--pnpr-server <url>#
添加于:v12.0.0
Offload the resolution of a --patches refresh to a pnpr server, the
way pnprServer does for an install.
--global, -g#
更新全局安装的依赖包。来自 pnpm 10 全局布局的包会被迁移到当前布局。成功迁移后,pnpm 会从 PNPM_HOME 中移除旧安装及其符号链接、shim 和硬链接可执行文件。
--workspace#
尝试从工作空间链接所有软件包。版本将更新至与工作空间内的包匹配的版本。
如果更新了特定的包,而在工作空间内也找不到任何可更新的依赖项,则命令将会失败。例如,如果 express 不是工作空间内的包,那么以下命令将失败:
pnpm up -r --workspace express
--prod, -P, --production#
仅更新在 dependencies 和 optionalDependencies 中的依赖项。
--dev, -D#
仅更新在 devDependencies 中的依赖项。
--no-optional#
忽略在 optionalDependencies 中的依赖项。
--optional#
Only update packages in optionalDependencies.
--peer#
Also update packages in peerDependencies. It cannot be combined with --interactive (ERR_PNPM_INTERACTIVE_PEER_UNSUPPORTED).
--save-exact, -E#
With --latest or --tag, write the resolved version to package.json without a range operator. A dependency that already declares a ^, ~, or exact range keeps that operator.
--lockfile-only#
Update pnpm-lock.yaml without downloading dependencies or changing node_modules.
--lockfile-dir <dir>#
The directory in which pnpm-lock.yaml is created. It cannot be combined with --global.
--interactive, -i#
显示过时的依赖项并选择要更新的依赖项。
For a package from a registry declared with serverType: pnpr, the release notes between the installed and the offered version are shown below the list while the package is under the cursor.
With trustPolicy: no-downgrade, the list of dependencies offers only versions the trust policy accepts, and it has a Trust column. The --global list of package groups has no such column. The column names the strongest trust evidence of the offered version: staged publish, trusted publisher, provenance, or none. If a package in trustPolicyExclude is offered a version with weaker evidence than the installed one, the cell is yellow and also names the installed version's evidence.
Combined with --global, each isolated install group is presented as one selectable item: packages that share a global installation update together as a unit.
--long#
Show package homepage or repository URLs in the interactive update list:
pnpm update --interactive --long
The URL column is hidden by default. This option fetches full package metadata
and only affects --interactive. It cannot be combined with --global, which
selects package groups.
--no-save#
不更新 package.json 中的范围。
--changeset#
After the update completes, write a change intent (a changesets-compatible .changeset/*.md file) for affected workspace packages. Changes to dependencies or optionalDependencies get a patch bump, and changes to peerDependencies get a major bump. 通过 catalog: 协议使用了已更新 catalog 条目的包也会被包含在内。 Private packages, packages without a name or a valid semver version, and packages listed in versioning.ignore or in the ignore array of .changeset/config.json are skipped. If neither the .changeset directory nor versioning in pnpm-workspace.yaml exists, a warning is printed and no changeset is generated.
Set update.changeset to true in pnpm-workspace.yaml to enable this behavior by default.
--no-changeset#
Don't write a change intent, even when update.changeset is true.
--include-github-actions#
同时更新被仓库中工作流文件引用的 GitHub Actions。参见更新 GitHub Actions。
供应链策略标志#
新增于:v12.3.0
pnpm update 接受与 pnpm install 和 pnpm add 相同的策略覆盖项:--trust-lockfile、--no-trust-lockfile、--trust-policy、--trust-policy-exclude 和 --trust-policy-ignore-after,因此可以在单次运行中放宽或收紧策略,而无需编辑 pnpm-workspace.yaml。
--minimum-release-age <minutes>, --minimum-release-age-exclude <package-spec>#
Added in: v12.23.0
Set minimumReleaseAge and minimumReleaseAgeExclude for this run, as they do for pnpm install.
--cpu, --os, --libc, --ignore-scripts, --ignore-pnpmfile#
These work as they do for pnpm install.