pnpm update

别名:up、upgrade

pnpm update 基于指定的范围更新包到它们的最新版本。

在不带参数的情况下使用时,将更新所有依赖关系。

摘要:#

命令 含义
pnpm up 更新所有依赖项,遵守 package.json 中指定的范围
pnpm up --latest 将所有依赖项更新到最新版本
pnpm up foo@2 将 foo 更新到 v2 上的最新版本
pnpm up "@babel/*" 更新 @babel 范围内的所有依赖项

自 v11.27.1 起,如果某个依赖的说明符来自 packageExtensions 条目、readPackage 钩子或 override,则不会写入 package.json。锁文件会保留该说明符。 pnpm update 和 pnpm audit --fix=update 仍会在范围内移动它。

来自这些来源之一的精确固定版本会保持原样。当该固定版本是易受攻击的版本时,pnpm audit --fix=update 会发出警告并指向 pnpm audit --fix,后者会写入一个 override。 override 在 packageExtensions 和 readPackage 之后应用,因此它会替换这些来源提供的说明符。

更新到指定版本#

自 v11.23.0 起,当该包不是任何所选项目的直接依赖时,pnpm update <name>@<version> 会以 ERR_PNPM_UPDATE_VERSION_ON_INDIRECT_DEP 失败。在这种情况下无处记录版本;请改为通过 overrides 固定传递依赖。范围和标签不受影响。

使用模式匹配选择依赖项#

你可以使用模式来更新特定的依赖项。

更新所有 babel 包:

pnpm update "@babel/*"

更新所有依赖项,除了 webpack:

pnpm update "\!webpack"

模式也可以组合,因此下一个命令将更新所有 babel 包,除了 core:

pnpm update "@babel/*" "\!@babel/core"

更新 GitHub Actions#

添加于:v11.16.0

pnpm outdated 可以检查仓库工作流文件所引用的 GitHub Actions 是否有更新,而 pnpm update 可以更新它们。每条命令都需要显式开启:传入 --include-github-actions,或在 pnpm-workspace.yaml 中把 update.githubActions 设为 true 以默认启用。

更新后的操作被锁定至具体的提交哈希,并且它们的发布标签保留在注释中:

- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0

本地 action 会被直接跟随,而不是查找:./ 相对引用,以及 自 v11.26.0 起 GitHub 的自仓库写法 (uses: $/.github/actions/setup),都在仓库内解析。

检查更新会对每个被引用的仓库执行 git ls-remote。无法读取 ref 的 action(例如私有仓库中的 action)会被跳过并给出警告。如果这些 action 托管在另一个 GitHub 服务器上(例如 GitHub Enterprise Server),请设置 update.githubActionsServer(添加于 v11.17.0)。

配置项#

--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

--latest, -L#

将依赖项更新至由 latest 标签决定的最新稳定版本(有可能会跨越主版本号),只要 package.json 中指定的版本范围低于 latest 标签的版本号(即不会对预发布版本降级)。

自 v11.27.1 起,--latest 不会将依赖移动到由 packageExtensions、readPackage 钩子或 override 提供的说明符之外。锁文件会保持在该说明符范围内。

--global, -g#

更新全局安装的依赖包。来自 pnpm 10 全局布局的包会被迁移到当前布局。成功迁移后,pnpm 会从 PNPM_HOME 中移除旧安装及其符号链接、shim 和硬链接可执行文件。

--workspace#

尝试从工作空间链接所有软件包。版本将更新至与工作空间内的包匹配的版本。

如果更新了特定的包,而在工作空间内也找不到任何可更新的依赖项,则命令将会失败。例如,如果 express 不是工作空间内的包,那么以下命令将失败:

pnpm up -r --workspace express

--prod, -P#

仅更新在 dependencies 和 optionalDependencies 中的依赖项。

--dev, -D#

仅更新在 devDependencies 中的依赖项。

--no-optional#

忽略在 optionalDependencies 中的依赖项。

--peer#

添加于:v11.28.0

同时更新 peerDependencies 中的范围。没有此标志,pnpm update 会保持 peerDependencies 不变。

pnpm update --peer react

--interactive, -i#

显示过时的依赖项并选择要更新的依赖项。

自 v11.21.0 起,与 --global 结合使用时,每个隔离安装组会作为一个可选项呈现:共享同一次全局安装的包会作为一个整体一起更新。

--no-save#

不更新 package.json 中的范围。

--changeset#

添加于:v11.16.0

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.

在 pnpm-workspace.yaml 中把 update.changeset 设为 true 可默认启用此行为,用 --no-changeset 可在单次更新中覆盖该设置。

--include-github-actions#

添加于:v11.16.0

同时更新被仓库中工作流文件引用的 GitHub Actions。参见更新 GitHub Actions。

供应链策略标志#

添加于:v11.26.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: v11.28.14

Set minimumReleaseAge and minimumReleaseAgeExclude for this run, as they do for pnpm install.

--filter <package_selector>#

阅读更多有关过滤的内容。