Patch Package

KPITENG
2 min readDec 7, 2021

--

patch-package allow developers to update code in node_modules with still keeping your changes even after upgrading/downgrading your node_modules.

patch-package — kpiteng

Consider you have install one package,

yarn add react-native-video-helper

Now, you want to update that package for fix some issues / customise it. So,

First Install patch-package dependency,

yarn add -D patch-package postinstall-postinstall

Turn back to node_module, Make necessary changes in your node_modules/react-native-video-helper/index.js,

Let’s say I have added console.log(“patch-package-testing”); Somewhere in file.

Now execute,

npx patch-package react-native-video-helper

Which create patches directory in your project’s root folder and create one file, patches/react-native-video-helper+1.5.0

If you open it you can see, whatever changes you did in node_modules. In our case we can see,

+console.log(“Patch Package TEST”);

Now, go to your package.json,

“scripts”: {
“postinstall”: “patch-package”
}

Now, if you delete node_modules, and install again then it will first download react-native-video-helper plugin, and then apply your patch changes. Because you have added patch-package in post install scripts.

Commands -

rm -rf node_modules/ (to delete node_modules)

yarn (to install node_modules packages)

Now, Let’s downgrade your package version, So execute,

yarn add react-native-video-helper@1.4.3

If you go to node_modules/react-native-video-helper, then you can see your console.log(“patch-package-testing”) which you have added, Because after upgrading/downgrading, it will execute post install script execute patch-package and your changes will be updated in your node_modules/react-native-video-helper.

If you think, you no more need of your changes then, simply remove patch files from patches/react-native-video-helper+1.5.0 and execute yarn install, so after yarn install postinstall will execute which check any patches are there to apply, if not found, and that’s it. Now, You are working with latest package.

Thanks for reading full article!

Read More Technical Articles

KPITENG || hello@kpiteng.com ||DIGITAL TRANSFORMATION

Connect Us — Linkedin | Facebook | Instagram

--

--

KPITENG
KPITENG

Responses (1)