ds300 /patch-package

Fix broken node modules instantly 🏃🏽‍♀️💨

★ 11,233⑂ 328
TypeScriptCreated 2017-05-08
11,233
stars
+0+-2+8
328
forks
–
watchers
–
prs
–
issues
–
releases
–
discussions

README

patch-package lets app authors instantly make and keep fixes to npm
dependencies. It's a vital band-aid for those of us living on the bleeding edge.

sh
 fix a bug in one of your dependencies
vim node_modules/some-package/brokenFile.js

 run patch-package to create a .patch file
npx patch-package some-package

 commit the patch file to share the fix with your team
git add patches/some-package+3.14.15.patch
git commit -m "fix brokenFile.js in some-package"


Patches created by patch-package are automatically and gracefully applied when
you use npm(=5) or yarn.

No more waiting around for pull requests to be merged and published. No more
forking repos just to fix that one tiny thing preventing your app from working.

 Set-up

In package.json

diff
 "scripts": {
+  "postinstall": "patch-package"
 }


Then

 npm

    npm i patch-package

You can use --save-dev if you don't need to run npm in production, e.g. if
you're making a web frontend.

 yarn v1

    yarn add patch-package postinstall-postinstall

You can use --dev if you don't need to run yarn in production, e.g. if you're
making a web frontend.

To understand why yarn needs the postinstall-postinstall package see:
Why use postinstall-postinstall

 yarn workspaces

Same as for yarn ☝️ Note that if you want to patch un-hoisted packages you'll
need to repeat the setup process for the child package. Also make sure you're in
the child package directory when you run patch-package to generate the patch
files.

 yarn v2+

yarn 2+ have native support for patching dependencies via
yarn patch. You do not need to use
patch-package on these projects.

 pnpm

pnpm has native support for patching dependencies via
pnpm patch. You do not need to use patch-package
on these projects.

 Heroku

For patch-package to work on Heroku applications, you must specify
NPM_CONFIG_PRODUCTION=false or YARN_PRODUCTION=false.
See this issue for more
details.

 Docker and CI

- If having errors about working directory ("cannot run in wd [...]") when
  building in Docker, you might need to adjust configuration in .npmrc. See
  185.
- In your Dockerfile, remember to copy over the patch files _before_ running
  [npm|yarn] install
- If you cache node_modules rather than running yarn install every time,
  make sure that the patches dir is included in your cache key somehow.
  Otherwise if you update a patch then the change may not be reflected on
  subsequent CI runs.

 CircleCI

Create a hash of your patches before loading/saving your cache. If using a Linux
machine, run md5sum patches/  patches.hash. If running on a macOS machine,
use md5 patches/  patches.hash

yaml
- run:
    name: patch-package hash
    command: md5sum patches/  patches.hash


Then, update your hash key to include a checksum of that file:

yaml
- restore_cache:
    key:
      app-node_modules-v1-{{ checksum "yarn.lock" }}-{{ checksum "patches.hash"
      }}


As well as the save_cache

yaml
- save_cache:
    key:
      app-node_modules-v1-{{ checksum "yarn.loc

Snapshot: 2026-11-10T09:35:25Z · Data license Apache-2.0 · JSON