Skip to content
Latchkey

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

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

Common 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
# package.json keeps the patch tied to an exact version:
"pnpm": { "patchedDependencies": { "lodash@4.17.21": "patches/lodash@4.17.21.patch" } }

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →