patch-package
When forking just won't work, patch it.
Last updated 7 years ago by ds300 .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install patch-package 
SYNC missed versions from official npm registry.
patch-package

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.

# 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

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

Then

npm

npm i patch-package --save-dev

yarn

yarn add --dev patch-package postinstall-postinstall

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

Usage

Making patches

First make changes to the files of a particular package in your node_modules folder, then run

yarn patch-package package-name

or use npx (included with npm > 5.2)

npx patch-package package-name

where package-name matches the name of the package you made changes to.

If this is the first time you've used patch-package, it will create a folder called patches in the root dir of your app. Inside will be a file called package-name+0.44.0.patch or something, which is a diff between normal old package-name and your fixed version. Commit this to share the fix with your team.

Options

  • --use-yarn

    By default, patch-package checks whether you use npm or yarn based on which lockfile you have. If you have both, it uses npm by default. Set this option to override that default and always use yarn.

  • --exclude <regexp>

    Ignore paths matching the regexp when creating patch files. Paths are relative to the root dir of the package to be patched.

    Default value: package\\.json$

  • --include <regexp>

    Only consider paths matching the regexp when creating patch files. Paths are relative to the root dir of the package to be patched.

    Default value: .*

  • --case-sensitive-path-filtering

    Make regexps used in --include or --exclude filters case-sensitive.

Updating patches

Use exactly the same process as for making patches in the first place, i.e. make more changes, run patch-package, commit the changes to the patch file.

Applying patches

Run patch-package without arguments to apply all patches in your project.

Options

  • --reverse

    Un-applies all patches.

    Note that this will fail if the patched files have changed since being patched. In that case, you'll probably need to re-install node_modules.

    This option was added to help people using CircleCI avoid an issue around caching and patch file updates but might be useful in other contexts too.

Notes

To apply patches individually, you may use git:

git apply --ignore-whitespace patches/package-name+0.44.2.patch

or patch in unixy environments:

patch -p1 -i patches/package-name+0.44.2.patch

Benefits of patching over forking

  • Sometimes forks need extra build steps, e.g. with react-native for Android. Forget that noise.
  • Get told in big red letters when the dependency changed and you need to check that your fix is still valid.
  • Keep your patches colocated with the code that depends on them.
  • Patches can be reviewed as part of your normal review process, forks probably can't

When to fork instead

  • The change is too consequential to be developed in situ.
  • The change would be useful to other people as-is.
  • You can afford to make a proper PR to upstream.

Isn't this dangerous?

Nope. The technique is quite robust. Here are some things to keep in mind though:

  • It's easy to forget to run yarn or npm when switching between branches that do and don't have patch files.
  • Long lived patches can be costly to maintain if they affect an area of code that is updated regularly and you want to update the package regularly too.
  • Big semantic changes can be hard to review. Keep them small and obvious or add plenty of comments.
  • Changes can also impact the behaviour of other untouched packages. It's normally obvious when this will happen, and often desired, but be careful nonetheless.

Why use postinstall-postinstall with Yarn?

Most times when you do a yarn, yarn add, yarn remove, or yarn install (which is the same as just yarn) Yarn will completely replace the contents of your node_modules with freshly unpackaged modules. patch-package uses the postinstall hook to modify these fresh modules, so that they behave well according to your will.

Yarn only runs the postinstall hook after yarn and yarn add, but not after yarn remove. The postinstall-postinstall package is used to make sure your postinstall hook gets executed even after a yarn remove.

License

MIT

Empowered by Futurice's open source sponsorship program

Current Tags

  • 6.1.0-0                                ...           beta (6 years ago)
  • 8.0.0-canary.5                                ...           canary (a year ago)
  • 8.0.0                                ...           latest (a year ago)

95 Versions

  • 8.0.0                                ...           a year ago
  • 8.0.0-canary.5                                ...           a year ago
  • 8.0.0-canary.4                                ...           a year ago
  • 8.0.0-canary.3                                ...           a year ago
  • 7.0.2                                ...           a year ago
  • 7.0.1                                ...           a year ago
  • 8.0.0-canary.2                                ...           a year ago
  • 8.0.0-canary.1                                ...           a year ago
  • 8.0.0-canary.0                                ...           a year ago
  • 7.0.0                                ...           2 years ago
  • 6.5.1                                ...           2 years ago
  • 6.5.0                                ...           2 years ago
  • 6.4.7                                ...           4 years ago
  • 6.4.6                                ...           4 years ago
  • 6.4.5                                ...           4 years ago
  • 6.4.4                                ...           4 years ago
  • 6.4.3                                ...           4 years ago
  • 6.4.2                                ...           4 years ago
  • 6.4.1                                ...           4 years ago
  • 6.4.0                                ...           4 years ago
  • 6.3.1                                ...           4 years ago
  • 6.3.0                                ...           4 years ago
  • 6.2.2                                ...           5 years ago
  • 6.2.1                                ...           5 years ago
  • 6.2.0                                ...           5 years ago
  • 6.1.4                                ...           5 years ago
  • 6.1.3                                ...           5 years ago
  • 6.1.2                                ...           6 years ago
  • 6.1.1                                ...           6 years ago
  • 6.1.0                                ...           6 years ago
  • 6.1.0-0                                ...           6 years ago
  • 6.0.7                                ...           6 years ago
  • 6.0.6                                ...           6 years ago
  • 6.0.5                                ...           6 years ago
  • 6.0.4                                ...           6 years ago
  • 6.0.2                                ...           6 years ago
  • 6.0.1                                ...           6 years ago
  • 6.0.0                                ...           6 years ago
  • 6.0.0-18                                ...           6 years ago
  • 6.0.0-17                                ...           6 years ago
  • 6.0.0-16                                ...           6 years ago
  • 6.0.0-15                                ...           6 years ago
  • 5.1.2                                ...           6 years ago
  • 6.0.0-14                                ...           6 years ago
  • 6.0.0-13                                ...           6 years ago
  • 6.0.0-12                                ...           6 years ago
  • 6.0.0-11                                ...           6 years ago
  • 6.0.0-10                                ...           6 years ago
  • 6.0.0-9                                ...           6 years ago
  • 6.0.0-8                                ...           6 years ago
  • 6.0.0-7                                ...           6 years ago
  • 6.0.0-6                                ...           7 years ago
  • 6.0.0-5                                ...           7 years ago
  • 6.0.0-4                                ...           7 years ago
  • 6.0.0-3                                ...           7 years ago
  • 6.0.0-2                                ...           7 years ago
  • 6.0.0-1                                ...           7 years ago
  • 6.0.0-0                                ...           7 years ago
  • 5.1.1                                ...           7 years ago
  • 5.1.0                                ...           7 years ago
  • 5.0.0                                ...           7 years ago
  • 4.0.0                                ...           7 years ago
  • 3.6.1                                ...           7 years ago
  • 3.6.0                                ...           7 years ago
  • 3.5.3                                ...           7 years ago
  • 3.5.3-0                                ...           7 years ago
  • 3.5.2                                ...           7 years ago
  • 3.5.1                                ...           7 years ago
  • 3.5.0                                ...           7 years ago
  • 3.4.6                                ...           7 years ago
  • 3.4.5                                ...           7 years ago
  • 3.4.4                                ...           7 years ago
  • 3.4.3                                ...           7 years ago
  • 3.4.2                                ...           7 years ago
  • 3.4.1                                ...           7 years ago
  • 3.4.0                                ...           7 years ago
  • 3.3.6                                ...           7 years ago
  • 3.3.5                                ...           7 years ago
  • 3.3.4                                ...           7 years ago
  • 3.3.3                                ...           7 years ago
  • 3.3.2                                ...           7 years ago
  • 3.3.1                                ...           7 years ago
  • 3.3.0                                ...           7 years ago
  • 3.2.1                                ...           7 years ago
  • 3.2.0                                ...           7 years ago
  • 3.1.0                                ...           7 years ago
  • 3.0.0                                ...           7 years ago
  • 2.1.1                                ...           7 years ago
  • 2.1.0                                ...           7 years ago
  • 2.0.0                                ...           8 years ago
  • 1.2.1                                ...           8 years ago
  • 1.2.0                                ...           8 years ago
  • 1.1.1                                ...           8 years ago
  • 1.1.0                                ...           8 years ago
  • 1.0.0                                ...           8 years ago
Maintainers (1)
Downloads
Total 1
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (8)
Dev Dependencies (18)

© 2010 - cnpmjs.org x YWFE | Home | YWFE