pnpm patch: Usage & Common Errors
Patch a dependency and version-control the fix.
pnpm patch opens an editable copy of a package; pnpm patch-commit captures your edits as a patch file referenced from package.json, so the fix is reproducible in CI.
What it does
pnpm patch <pkg>@<version> extracts the package to a temp directory and prints its path. You edit the files there, then run pnpm patch-commit <path> to generate a patch under patches/ and add a patchedDependencies entry to package.json. Every subsequent install reapplies it.
Common usage
pnpm patch lodash@4.17.21 # prints a temp dir to edit
# edit files in that dir, then:
pnpm patch-commit /tmp/...path # writes patches/ + package.json entryCommon CI error: patch fails to apply
CI install fails because a patchedDependencies entry references a version that no longer matches the installed package (after an upgrade), so the patch will not apply cleanly. Pin the patched dependency to the exact version the patch targets, and regenerate the patch with pnpm patch when you bump it.
# package.json keeps the patch tied to an exact version:
"pnpm": { "patchedDependencies": { "lodash@4.17.21": "patches/lodash@4.17.21.patch" } }