In this lesson, you will learn how to use patch-package to fix bugs in third-party native libraries without waiting for the original maintainers to release a fix.
Why things break in Native Land
When you install a library, you are pulling in code that interacts directly with iOS or Android internals. Sometimes, a library might have a small configuration error, a missing header file, or a broken build script that prevents your app from compiling. While you could technically edit the code inside your node_modules folder to fix it, those changes would disappear the moment you run npm install again.
Using patch-package
The tool patch-package solves this by letting you record your manual fix into a patch file that is automatically applied every time you install dependencies.
Step-by-Step Workflow
- Locate the bug: Find the file inside
node_modules/your-problematic-libraryand apply the necessary fix (e.g., adding a missing import or correcting a syntax error). - Install patch-package: Run
npm install patch-package --save-dev. - Create the patch: Run
npx patch-package your-problematic-library.
This command looks at the difference between the original version of the library and your modified version, then creates a new folder in your project root called patches/.
The Result
Inside that folder, you’ll find a .patch file. It’s essentially a record of your changes. Now, every time you or a teammate runs npm install, patch-package automatically detects that file and applies your custom fix on top of the library.
This approach is highly valuable because it keeps your codebase resilient to upstream errors. Instead of being blocked by a faulty release, you can proactively resolve the issue locally, maintain a clean audit trail via version control, and keep your build pipeline moving. Just remember to periodically check if the library maintainer has released an official update, at which point you can safely delete your patch file.
Key Takeaway: patch-package allows you to treat library bugs as temporary local configuration issues, ensuring that your native builds remain stable even when your dependencies aren't perfect.