How to patch packages in node_modules

Digvijay Upadhyay
2 min readOct 13, 2020

Ever ran into a situation when there’s a bug in one of the node packages? or one of the package function/code you use was deprecated?

The ideal way to fix is to create a PR in the source repo and wait for it to get released. Very convenient right?

While waiting for the fix to get released what you can do is simply use patch-package and patch the node modules.

Example

Imagine a well known package underscore. They released a new version. In this version they removed and added some functions.

You updated it but there is one deprecated function named someDeprecatedFunction which you use in your code at a lot of places.

// index.jsimport _ from "underscore";const result = _.someDeprecatedFunction();console.log("patchFunction returned:", result);

We will patch the underscore module and add this deprecated function.

Install the library

Let’s install the package using

yarn add --dev patch-package

or if you use npm

npm install patch-package --save-dev

--

--