Patch Package

8.0.1 · active · verified Sun Apr 19

patch-package is a utility that enables developers to apply and manage local modifications to their `node_modules` dependencies without needing to fork or wait for upstream merges. It works by creating `.patch` files based on manual changes made directly in `node_modules`, which are then automatically applied during subsequent `npm install` or `yarn install` operations via a `postinstall` script. The current stable version is 8.0.1, with major releases occurring periodically to address Node.js version updates, security fixes, and compatibility with new package manager features. Key differentiators include its simplicity in generating and applying patches, avoiding the overhead of maintaining forks, and providing a quick workaround for urgent bugs in dependencies. It primarily serves as a band-aid solution, especially for projects on the bleeding edge of dependency versions, though newer package managers like Yarn 2+ and pnpm have integrated native patching capabilities that supersede patch-package for those ecosystems. Its release cadence is reactive to critical issues and ecosystem changes.

Common errors

Warnings

Install

Quickstart

Demonstrates the typical workflow for fixing a node module: manual edit, patch creation via CLI, automated application via postinstall script, and committing the patch file.

/*
  patch-package is primarily a CLI tool and typically integrated into package.json scripts.
  This quickstart demonstrates the general workflow.
*/

// 1. Manually identify and fix a bug in one of your node_modules dependencies.
//    Example: open 'node_modules/some-package/brokenFile.js' and apply your fix.
//    (This step involves direct file editing, not code here)

// 2. Generate a patch file for the modified package using npx.
//    Replace 'some-package' with the actual name of your modified dependency.
//    Run this in your terminal from the project root:
// npx patch-package some-package

// 3. Configure your package.json to automatically apply patches during installation.
//    Add this entry to your 'scripts' object:
/*
"scripts": {
  "postinstall": "patch-package"
},
*/
//    If using Yarn v1, you must also install 'postinstall-postinstall' and use:
//    "postinstall": "postinstall-postinstall || patch-package"

// 4. Commit the generated patch file to your version control system (e.g., Git).
//    The file will be located in a 'patches/' directory, named like 'patches/some-package+3.14.15.patch'.
// git add patches/some-package+3.14.15.patch
// git commit -m "Fix brokenFile.js in some-package using patch-package"

// 5. From now on, any 'npm install' or 'yarn install' will automatically apply your patch.

view raw JSON →