# Hotfixing Dependencies (patch-package & pnpm patch)

> Fix bugs inside node_modules immediately using patch-package or native pnpm patch without waiting for upstream releases or maintaining forks.

Subject: Package Managers | Lesson 18 of 24 | 5 min read
Interactive lesson: https://codewithanimation.in/package-managers/patch-package-and-pnpm-patch
Previous: [Package Overrides (overrides & resolutions)](https://codewithanimation.in/package-managers/overrides-and-resolutions.md) | Next: [Monorepos & Workspaces (npm, pnpm & Turborepo)](https://codewithanimation.in/package-managers/npm-workspaces-monorepos.md)

## What is Dependency Patching?

Dependency patching records a source-code change as a version-controlled diff. patch-package commonly starts with an installed-file edit; native pnpm patch instead provides an isolated editable copy.

Instead of waiting weeks for upstream maintainers to merge a bug fix Pull Request or maintaining a permanent fork of a repository, tools like patch-package or native pnpm patch generate unified patch files in a patches directory. These patches automatically apply whenever dependencies are installed.

## Why Patch Dependencies Instead of Forking or Overriding?

A fork is another option, distributed through a suitable package or source reference and maintained as upstream evolves. A version override chooses replacement resolutions; a patch records a line-level source fix for an existing target.

Patch files are lightweight git-tracked unified diffs stored inside your repository. They provide immediate unblocking for critical production bugs while keeping your project tied to official registry package versions.

## How patch-package and pnpm patch Work under the Hood

With patch-package in npm or Yarn v1, edit code inside node_modules/package-name and run npx patch-package package-name. This creates patches/package-name+1.2.0.patch. Add patch-package to your package.json postinstall script to automatically apply patches after npm install.

With pnpm, run pnpm patch package-name@1.2.0, edit the isolated copy and run pnpm patch-commit on that directory. The command saves a patch and patchedDependencies registration in the applicable pnpm configuration; current projects use pnpm-workspace.yaml. This native mechanism does not depend on a postinstall command.

## Common mistake: Version Upgrades Breaking Patches and --ignore-scripts Bypassing postinstall

When upgrading a patched dependency, review whether the fix remains necessary and whether the selected patch still applies. A version-bound patch can be skipped or fail against a different release; do not assume it was applied successfully.

Another critical issue with patch-package is running npm install with --ignore-scripts. Skipping lifecycle scripts prevents postinstall from executing patch-package, leaving node_modules unpatched in CI pipelines. Native pnpm patch avoids this risk by applying patches during installation linking.

## Tip: Submit Upstream PRs Alongside Patches and Normalize Line Endings

Treat patches as temporary hotfixes rather than permanent solutions. Always open an issue or Pull Request on the upstream repository with your fix, and link the PR URL inside your commit message or patch file header.

Ensure line endings are consistent across team environments. Mixing Windows CRLF and Unix LF line endings can cause patch-package to record entire files as changed lines. Configure gitattributes to force LF line endings for JavaScript and patch files.

## Try it: Interactive Dependency Patching Simulator

Step through the full 5-step hotfix lifecycle: edit a buggy file in node_modules, generate a unified diff patch file, inspect package manifests, and verify automated re-application on clean install.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/package-managers/patch-package-and-pnpm-patch

## Check yourself

### Question 1

What makes a dependency hotfix reproducible with patch-package?

- A. An edit left only in one developer's node_modules
- B. A terminal screenshot of the modified line
- C. Removing the dependency from the manifest
- D. A tracked patch file applied during installation

Correct answer: D

A saved diff plus the configured application step lets clean installs recreate the fix. An untracked edit in node_modules alone will not do that.

- Why not A: A clean install can overwrite that edit.
- Why not B: A screenshot does not apply code changes.
- Why not C: Removing a requirement does not preserve a source hotfix.

### Question 2

patch-package is called only by postinstall. What happens if that install skips lifecycle scripts?

- A. That postinstall application step does not run
- B. The patch is guaranteed to run through the skipped hook anyway
- C. The patch is automatically published upstream
- D. npm converts the diff into an override automatically

Correct answer: A

A workflow relying on postinstall must account for script policy. Skipping the hook can leave the installed package unpatched.

- Why not B: A skipped hook cannot perform its configured command.
- Why not C: Local installation does not publish upstream changes.
- Why not D: A patch and a version override are distinct mechanisms.

### Question 3

What is the normal native pnpm patch sequence?

- A. Edit the global store, then delete the manifest
- B. pnpm patch, edit the isolated copy, then pnpm patch-commit
- C. Run npm publish before editing any source
- D. Only add a package version to devDependencies

Correct answer: B

patch-commit records the diff and patch registration. Review whether the fix is still needed and applies correctly when the target version changes.

- Why not A: This does not record an isolated, reproducible patch.
- Why not C: Publishing is not required to create a local patch.
- Why not D: A version declaration does not record a line-level code change.

## Remember this

- Dependency patching creates git-tracked unified diff files in patches/ to hotfix node_modules without forking.
- patch-package requires adding patch-package to your package.json postinstall script hook.
- Native pnpm patch records a patchedDependencies entry and applies the patch through its installation mechanism, independently of a postinstall command.
- Review and test patches when upgrading the target version; remove fixes already supplied upstream.
