DEV Community

Cover image for NPM: How to patch a dependency 📦
Majuran SIVAKUMAR
Majuran SIVAKUMAR

Posted on • Updated on

NPM: How to patch a dependency 📦

Why?

As a Javascript developer, you might have come to a situation where you found a bug in a dependency you use.

As a good soul 😇, you have done a pull request with the fix. But what if the pull request is not yet accepted and you need this fix to deploy your project?! 🤔

Patch-Package 🚀

Patch-Package is a tool to patch a dependency, it will create a .patch file and it will patch the package after each npm install.

How?

  • Open the file you want to edit and add your fix.
vim node_modules/package-name/file-to-edit.js
Enter fullscreen mode Exit fullscreen mode
  • Run :
npx patch-package package-name
Enter fullscreen mode Exit fullscreen mode

This will create a .patch file under a new repository patches/

  • Commit this file to your git repository

  • Finally add following script to your package.json. This will run after each npm install and will patch the package.

 "scripts": {
   "postinstall": "patch-package"
 }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)