OMC's Blog

Patching npm packages for good

September 17, 2020

Introduction

In React Native development, there is a common scenario if you support react-native-web.

  • Find a package that fits your use case completely
  • Install and use it
  • Crashes for web
  • The fix is one-liner on library

There is two options in that case. You either create a PR with your fix or fork the library and mantain your local version. Both of them has their downsides. Your PR might not be reviewed for weeks and you would need that fix immediately. And it can be pretty overwhelming to fork repos and mantain your own version.

The solution is here: patch-package

patch-package

According to its documentation: 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.

How does it work

  1. Go find the library that you'd like to update in node_modules folder.
  2. Make your changes
  3. Run npx patch-package package-name
  4. Run yarn install patch-package -D
  5. Add "postinstall": "patch-package" to your scripts key in package.json file

Step 4 and Step5 guarantee that your node_modules will be overwritten by your patch each time you yarn.


Oguzhan Murat Cakmak, starting his software career in Istanbul and advancing through roles in San Francisco at Uber and Hims&Hers, now does remote work from Kaş, Antalya, balancing his passion for technology with interests in kitesurfing, diving, hiking, and exploring diverse content on podcasts. Follow him on Twitter and Instagram

Webmentions

00
    Loading Webmentions...