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.
npm install patch-packageNo compatibility data collected yet for this library.
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.
Re-generate existing patch files using `patch-package` on version 8 to ensure they adhere to current naming conventions.
Upgrade your Node.js environment to version 14 or higher. Projects running on older Node.js versions will fail to install `patch-package@7` and above.
For Yarn 2+ or pnpm projects, utilize their built-in patching mechanisms (`yarn patch` / `pnpm patch`) instead of `patch-package`.
Set the environment variable `NPM_CONFIG_PRODUCTION=false` or `YARN_PRODUCTION=false` in your Heroku application settings to ensure `postinstall` scripts run.
Ensure your cache key includes a checksum or hash of the `patches/` directory (e.g., `md5sum patches/*`) to invalidate the cache when patches are updated.
Adjust `.npmrc` settings, possibly adding `unsafe-perm = true`, or verify that your Dockerfile copies the project to `/app` (or similar) before running `npm install`, ensuring correct permissions and working directory setup.
Verify that the `patches/` directory and its contents are correctly committed to version control and copied into your build environment. Ensure the patch file name exactly matches the package name and version.
Manually re-apply your changes to the updated dependency in `node_modules`, then run `npx patch-package <package-name>` to generate a new patch file that matches the current version.