Subchapter 6.14
references/features-patches.mdMarkdown4 KBView on GitHub
pnpm’s patching feature lets you modify third-party packages directly. Useful for applying fixes before upstream releases or customizing package behavior.
pnpm patch <pkg>@<version>
# Example
pnpm patch express@4.18.2This creates a temporary directory with the package source and outputs the path:
You can now edit the following folder: /tmp/abc123...Navigate to the temporary directory and make your changes:
cd /tmp/abc123...
# Edit files as neededpnpm patch-commit <path-from-step-1>
# Example
pnpm patch-commit /tmp/abc123...This creates a .patch file in patches/ and records it in pnpm-workspace.yaml:
patches/
└── express@4.18.2.patchpatchedDependencies:
express@4.18.2: patches/express@4.18.2.patch
patchedDependencies(like all pnpm settings) now lives inpnpm-workspace.yaml, not thepackage.json#pnpmfield.
Patches use standard unified diff format:
diff --git a/lib/router/index.js b/lib/router/index.js
index abc123..def456 100644
--- a/lib/router/index.js
+++ b/lib/router/index.js
@@ -100,6 +100,7 @@ function createRouter() {
// Original code
- const timeout = 30000;
+ const timeout = 60000; // Extended timeout
return router;
}pnpm list --depth=0
# Shows (patched) marker for patched packages# Edit existing patch
pnpm patch express@4.18.2
# After editing
pnpm patch-commit <path>pnpm patch-remove <pkg>@<version>
# Example
pnpm patch-remove express@4.18.2Or manually:
patches/patchedDependencies in pnpm-workspace.yamlpnpm installPatches are shared across the whole workspace from the root pnpm-workspace.yaml:
patchedDependencies:
express@4.18.2: patches/express@4.18.2.patch
lodash@4.17.21: patches/lodash@4.17.21.patch
'@types/node@20.10.0': patches/@types__node@20.10.0.patchA version-less key (express:) patches every installed version. All workspace packages using a matching version get the patch.
Patch files can live inside a shared config dependency and be referenced by path:
configDependencies:
my-patches: '1.0.0'
patchedDependencies:
react: node_modules/.pnpm-config/my-patches/react.patchallowUnusedPatches: true # don't fail when a listed patch wasn't applied
ignorePatchFailureswas removed in v11. A patch that fails to apply now always throws. When several patches are grouped, all errors are reported together at the end.
Version specificity: Patches are tied to exact versions. Update patches when upgrading dependencies.
Document patches: Add comments explaining why the patch exists:
# In patches/README.md
## express@4.18.2.patch
Fixes timeout issue. PR pending: https://github.com/expressjs/express/pull/1234Minimize patches: Keep patches small and focused. Large patches are hard to maintain.
Track upstream: Note upstream issues/PRs so you can remove patches when fixed.
Test patches: Ensure patched code works correctly in your use case.
ERR_PNPM_PATCH_FAILED Cannot apply patchThe package version changed. Recreate the patch:
pnpm patch-remove express@4.18.2
pnpm patch express@4.18.2
# Reapply changes
pnpm patch-commit <path>Ensure:
patchedDependencies matches installed version exactlypnpm install after adding patch configuration