Skip to content
Latchkey

Node.js "patch-package failed to apply a patch" in CI

patch-package replays a saved diff against an installed dependency during postinstall. When the dependency version changes, the patch context no longer matches and the apply fails, breaking the install.

What this error means

A postinstall step fails with patch-package reporting it could not apply a patch, usually after a dependency was upgraded so the patched files differ from the saved diff.

node
**ERROR** Failed to apply patch for package some-lib at path
    node_modules/some-lib

  This error was caused because some-lib has changed since you
  made the patch file for it.

Common causes

The patched package version changed

The patch was created for one version; an upgrade (even a patch bump) altered the files so the diff context no longer matches.

The patch filename version is mismatched

patch-package keys patches by version in the filename. A version that does not match the installed one is rejected.

How to fix it

Regenerate the patch for the new version

Recreate the patch against the currently installed version of the package.

Terminal
# edit node_modules/some-lib as needed, then:
npx patch-package some-lib

Pin the dependency to the patched version

If you cannot re-patch now, pin the dependency to the exact version the patch targets so the context matches.

package.json
{
  "dependencies": { "some-lib": "1.2.3" }
}

How to prevent it

  • Pin patched dependencies to exact versions.
  • Regenerate patches whenever the patched package is upgraded.
  • Prefer upstreaming the fix so the patch can be dropped.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →