DEV Community

Ajmal Hasan
Ajmal Hasan

Posted on • Updated on

Make changes to node_modules files with patch-package

Library Link

a) Install library

  • using yarn:
yarn add patch-package postinstall-postinstall --dev
Enter fullscreen mode Exit fullscreen mode
  • or using npm:
npm i patch-package --save-dev
Enter fullscreen mode Exit fullscreen mode

b) In package.json add

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

c) Usage
First make changes to the files of a particular package in your node_modules folder, then run

yarn patch-package package-name
Enter fullscreen mode Exit fullscreen mode

or

npx patch-package package-name
Enter fullscreen mode Exit fullscreen mode

(where package-name matches the name of the package you made changes to for example react-native, react-native-push-notification etc...)

  • Nested packages If you are trying to patch a package at, e.g. node_modules/package/node_modules/another-package you can just put a / between the package names:
npx patch-package package/another-package
Enter fullscreen mode Exit fullscreen mode

It works with scoped packages too

npx patch-package @my/package/@my/other-package
Enter fullscreen mode Exit fullscreen mode

d) Finally
A folder will be created in your project named as patches which will include all the library patches you made.
Then you can push the code to your git repository.
Whenever yarn install or npm install is run all the patches will stay.

Top comments (0)