DEV Community

Cover image for What if I need to make a change in node_modules?
Thamara Andrade
Thamara Andrade

Posted on

What if I need to make a change in node_modules?

I have an application build on top of node/electron, and recently, because of an update in the electron, some dependencies have broken down, preventing me to upgrade to the newer versions while keeping all functionalities.

In such cases, the correct behavior would be to reach out to the dependency, and open an issue, or even submit a Pull Request, but what if your dependency is not maintained anymore, or the development process on such repository is stalled or slow, and you really need to move on with your development?

That was the scenario I was facing. The change I needed was very localized, but I could not, in a million tries, get to make it work on my fork of the dependency. To make things worst, the last commit on the repository was over 6 months ago, and no sign of a reply on the issue I opened in a week.

That's when I found patch-package, a package that lets app authors instantly make and keep fixes to npm dependencies.

GitHub logo ds300 / patch-package

Fix broken node modules instantly 🏃🏽‍♀️💨

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"
Enter fullscreen mode Exit fullscreen mode

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"
 }
Enter fullscreen mode Exit fullscreen mode

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.

The README has all the information you'll need, but I'll summarize here the idea:

  1. Install patch-package (using npm or yarn)
  2. Update the scripts rule on your package.json to include a call for patch-package:

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

3. Do the change you need on the dependency, directly on node_modules
4. Call patch-package specifying the package you modified:

    ```


    (npx | yarn) patch-package package-name


Enter fullscreen mode Exit fullscreen mode
The last command will create a patch file (a diff) in `patches/`, which is a diff between the public dependency and your changed version.
Enter fullscreen mode Exit fullscreen mode
  1. Commit the changes, including the new patch file and everything will be working as expected.

Now, whenever you (or someone) calls install on your repository, patch-package will be called, applying that change on it.

Win!

Problem solved!

What about you? Have you ever needed to take a similar action?

Top comments (3)

Collapse
 
pak11273 profile image
Isaac Pak

So patch-package is hardly maintained now. How to "patch" patch-package?

Collapse
 
meatnordrink profile image
AndyG

It has commits within the last month as of Dec 2022.

Collapse
 
irumidesu7 profile image
Ilmi

it's still maintained, last update was 7th aug 2023