Skip to content

Aquent | DEV6

Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Just patch it and get on with your day!

Written by: Rod Nolan

If you’ve ever been frustrated while waiting for small fixes to be applied to the npm modules that you depend on in your projects, this post is for you.

Here’s a familiar problem: a dependency that you can’t live without contains a bug. It might be small, it might be big; it might be at any stage of the resolution process, from “just discovered” to “fixed but not yet released”. In a pinch, you could fork the repo, fix it yourself, switch to your fork and wait for the original library to return to stability. Forking is overkill, though, when the fix is very small and/or you expect the update to be reseased in a reasonable amount of time.

For those situations where you need the fix NOW but don’t want to fork, you can use patch-package.

Setup is easy and usage is straight forward.


From the command line:

yarn add --dev patch-package

to add this library as a devDependency.

In package.json, add:

"scripts": {
 "prepare": "patch-package"

To ensure that your patches are applied every time you install/re-install dependencies.

In .gitattributes, add:

patches/*.patch eol=lf

To deal with a line endings issue on Windows

Just a heads up: patch-package likes specific version ranges for yarn/node/npm. If your versions don’t match, you’ll be prompted to update.


Making a patch

  1. Install the package that needs to be patched and fix it locally, right there in the node_modules\ folder
  2. from the command line
yarn patch-package <package-name>


npm prepare -- <package-name>

This will generate a patches\ folder and install a patch file whose name is tagged with the version of the library the fix was written for. When you update the target library, you’ll get a warning to let you know the fix can’t be applied to the current version. If the fix is still needed, you can regenerate the patch based on the current version just like you did before. In any case, you should probably check your patch files in to source control so other team members can benefit from your work.

Applying a patch

Delete your node_modules folder and

yarn install

You should see that the patch is applied after the dependencies are installed.

I reported the Windows line endings issue and I was very happy to discover that the fix had been released in less than 24 hours! That’s open source software development at its finest!

If you decide to add this tool to your project, I’d love to hear your opinions. Feel free to drop me a line at [email protected].