+0
日增长
+0
周增长
+6
月增长
11,233
stars
328
forks
39
watchers
142
prs
474
issues
25
releases
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.locTop issues
#277Better support for monorepos22 comments · 2020-11-17#484Can't patch package- fails with status 1, no error20 comments · 2023-08-18#272patch-package 6.2.2 • Creating temporary folder SyntaxError: Unknown token: { line: 3, col: 2, type: 'INVALID', value: undefined } 3:2 in lockfile15 comments · 2020-10-13#337"**ERROR** Failed to apply patch for package react-native at path" After deleting node_modules14 comments · 2021-07-29#326vercel deployment14 comments · 2021-06-26#141Integrate with open source PR flow?14 comments · 2019-05-07#547I can't create a patch 13 comments · 2024-11-25#489Support for Bun12 comments · 2023-09-09
Latest release
v8.0.0 v8.0.0 2023-08-03- Add support for multiple patch files for a single package. (@ds300) 474 Breaking I removed some really old legacy filename handling stuff so it's technically a breaking change.