npm-patch

将本地补丁应用到已安装的依赖

选择命令行版本:

概要

🌐 Synopsis

npm patch <pkg>[@<version>]
npm patch add <pkg>[@<version>] [--edit-dir <path>] [--ignore-existing]
npm patch commit <edit-dir> [--patches-dir <dir>] [--keep-edit-dir]
npm patch update <pkg>[@<old-version>] [--to <new-version>] [--patches-dir <dir>]
npm patch ls
npm patch rm <pkg>[@<version>]

注意:此命令不支持工作区。

🌐 Note: This command is unaware of workspaces.

描述

🌐 Description

npm patch 让你对已安装的依赖进行小范围的局部修改,并在每次安装时自动重新应用这些修改。补丁在你项目根目录的 package.json 文件的 patchedDependencies 字段中声明,以普通的统一差异(unified diff)格式存放在 patches/ 目录下,并在 package-lock.json 中用内容哈希记录。

因为补丁是在安装过程中应用的,所以它们无论 install-strategy 如何都有效,适用于传递依赖,并且不会被 --ignore-scripts 禁用。

🌐 Because patches are applied during the install itself, they work regardless of install-strategy, apply to transitive dependencies, and are not disabled by --ignore-scripts.

裸形式 npm patch <pkg>npm patch add <pkg> 的简写。一个字面上像子命令的包必须使用显式形式,例如 npm patch add add

🌐 The bare form npm patch <pkg> is shorthand for npm patch add <pkg>. A package literally named like a subcommand must use the explicit form, e.g. npm patch add add.

  • npm patch add <pkg>[@<version>]

    为编辑准备一个包。npm 会将已解析的包压缩文件提取到 node_modules 外的临时目录,并打印其路径。在那里编辑文件,然后运行 npm patch commit

    如果安装了多个版本的 <pkg>,请使用像 npm patch add lodash@4.17.21 这样的精确选择器重新运行。

  • npm patch commit <edit-dir>

    将编辑过的目录与原始 tar 包的干净副本进行比较,把统一差异写入 <patches-dir>/<name>@<version>.patch,将条目添加到 patchedDependencies,并更新 package-lock.json

  • npm patch ls

    列出已注册的补丁以及每个补丁匹配的已安装节点数量。

  • npm patch rm <pkg>[@<version>]

    patchedDependencies 中移除匹配的条目,当没有其他条目引用补丁文件时删除它,并更新 package-lock.json。如果省略 <version>,则会移除 <pkg> 的所有条目。

故障模式

🌐 Failure modes

默认情况下,任何补丁问题都是一个严重错误,会中止安装:补丁无法应用、已注册的补丁没有匹配的已安装包、缺少补丁文件,或者补丁的哈希值与锁文件不匹配。

🌐 By default any patch problem is a hard error that aborts the install: a patch that fails to apply, a registered patch that matches no installed package, a missing patch file, or a patch whose hash does not match the lockfile.

有两个仅限命令行的标志可以在一次性情况下放宽这个限制:--allow-unused-patches--ignore-patch-failures

🌐 Two CLI-only flags relax this for one-off cases: --allow-unused-patches and --ignore-patch-failures.

配置

🌐 Configuration

patches-dir

  • 默认:“补丁”
  • 类型:字符串

相对于项目根目录,npm patch commitpatchedDependencies 写补丁文件的目录。

🌐 The directory, relative to the project root, where npm patch commit writes patch files for patchedDependencies.

allow-unused-patches

  • 默认:否
  • 类型:布尔

即使在 patchedDependencies 中注册的补丁没有匹配到已安装的包时也要安装。不会屏蔽补丁应用失败。

🌐 Install even when a registered patch in patchedDependencies matches no installed package. Does not silence patch apply failures.

这个标志只有在命令行上传递时才生效;在 .npmrc 和环境变量中会被忽略,并且会被 npm ci 拒绝。

🌐 This flag is only honored when passed on the command line; it is ignored in .npmrc and environment variables, and rejected by npm ci.

ignore-patch-failures

  • 默认:否
  • 类型:布尔

即使已注册的补丁安装失败,也要安装,每次失败都会有警告。仅用于事件响应。

🌐 Install even when a registered patch fails to apply, with a warning per failure. Intended for incident response only.

这个标志只有在命令行上传递时才生效;在 .npmrc 和环境变量中会被忽略,并且会被 npm ci 拒绝。

🌐 This flag is only honored when passed on the command line; it is ignored in .npmrc and environment variables, and rejected by npm ci.

edit-dir

  • 默认值:空
  • 类型:空或路径

覆盖 npm patch add 用来准备编辑包的临时目录。

🌐 Override the temporary directory used by npm patch add to prepare a package for editing.

ignore-existing

  • 默认:否
  • 类型:布尔

使用 npm patch add,丢弃之前未完成的编辑目录并重新开始。

🌐 With npm patch add, discard a previous unfinished edit directory and start fresh.

keep-edit-dir

  • 默认:否
  • 类型:布尔

使用 npm patch commit 时,提交补丁后不要删除编辑目录。

🌐 With npm patch commit, do not remove the edit directory after committing the patch.

to

  • 默认值:空
  • 类型:空或字符串

当无法从 package-lock.json 读取时,npm patch update 用来设置要将补丁重新基准的版本——例如一个精确版本选择器,或者一个尚未安装的版本。

🌐 Used by npm patch update to set the version to rebase a patch onto when it cannot be read from package-lock.json — for example an exact-version selector, or a version that has not been installed yet.

registry

npm 注册表的基本 URL。

🌐 The base URL of the npm registry.

也可以看看

🌐 See Also