Say Goodbye to Old-Fashioned Forks Thanks to the Patch-Package

Have you ever been in a situation when you had to fork the library to apply some fix to your app? While working with JavaScript and all the environment around it I found this situation to occur quite often. Especially when it comes to <rte-code>react-native<rte-code> which changes rapidly and the dependencies are often broken due to framework updates (0.40 iOS headers or 0.47 <rte-code>@Override<rte-code> annotation did a lot of damage to non-maintained libs).

Let’s think about the other situations. You’re working with a team of few developers. One guy from your team has to make a change in one of your dependencies and forks it into his private account (yes, I’ve seen this quite many times). A month later he decides to change his job and deletes the forked library. Now you have to re-implement the missing stuff.

Of course you could keep the forks within your company’s or your client’s repositories, but there is no reason to do it anymore since now you can use an amazing library called <rte-code>patch-package<rte-code> created by David Sheldrick. Remember that throwing a PR that fixes the issue along with the patches for your library is still the best idea #powerofopensource.

How to use it?

Using it is very simple and straightforward. You can do this by following a few simple steps:

Let’s say I want to patch a dependency called <rte-code>example-library<rte-code>.

#1 Install the patch-package dependency:

yarn add --dev patch-package postinstall-prepare
view raw gistfile1.txt hosted with ❤ by GitHub

If you’re wondering why <rte-code>postinstall-prepare<rte-code> comes along with <rte-code>patch-package<rte-code> — please refer to the author’s explanation:

Yarn only runs the <rte-code>prepare<rte-code> hook after <rte-code>yarn<rte-code> and <rte-code>yarn add<rte-code>, but not after <rte-code>yarn remove<rte-code>. The <rte-code>postinstall-prepare<rte-code> package is used to make sure your <rte-code>prepare<rte-code> hook gets executed even after a <rte-code>yarn remove<rte-code>.

#2 Do the changes in your node_modules/example-library

yarn patch-package example-library
view raw gistfile1.txt hosted with ❤ by GitHub

…just like you would edit your regular project files. When all the fixes/enhancements are applied, go to the next step.

#3 Run the patch-package script on your library:

screenshot of patch-package script

Note that new folder called patches has been created in your root directory. It contains .patch files for your project.

one-line example of patches

#4 Add prepare hook to your package.json scripts:

"scripts": { "prepare": "patch-package"}
view raw package.json hosted with ❤ by GitHub

If you want to know more about <rte-code>prepare<rte-code> and other hooks used by yarn and npm— check out the scripts section in npm docs.

Now you can commit the changes and push them straight to the repo. Your co-workers, as well as continuous integrations systems, can use your dependency updates.

You just have to run <rte-code>yarn<rte-code>, <rte-code>yarn install<rte-code> or <rte-code>npm<rte-code> install and the changes will be applied automatically.

another screenshot of patch-package script

That’s it! Now your patch will be applied “auto-magically” when you use yarn and no one in your organization has to bother about doing fixes on their own anymore.

I hope it will make your life a bit easier. 😉

Discover services offered by our React Native development company.

Insights

Learn more about Developer Experience

cover

May 5Article

Codex Ambassador: Q1 Recap Across 5 Cities

A short recap of Codex events in Berlin, Wroclaw, Warsaw, Sopot, and Miami, plus what we learned from each format and where we want to go next.

cover

Feb 5Live Stream

Storybook Explained: From Basics to Advanced Workflows

Core Storybook concepts, stories and args, interaction testing, and how Storybook works in React Native projects.

cover

Jan 29Article

Announcing AI SDK Profiler for React Native

Announcing the AI SDK Profiler plugin for Rozenite DevTools. Capture OpenTelemetry spans from the Vercel AI SDK and inspect AI requests across on-device and cloud providers.

cover

Nov 28Talk

How I Hacked React Native DevTools (And Built a Plugin Framework)

Learn how React Native DevTools work under the hood, and how to inject custom plugins with clean hooks and no forks.

cover

Sep 16Talk

How I Hacked React Native DevTools (And Built a Plugin Framework)

Learn how React Native DevTools work under the hood, and how to inject custom plugins with clean hooks and no forks.

cover

Jul 28Article

Introducing Rozenite: A Plugin Framework for React Native DevTools

Build custom debugging panels inside React Native DevTools with Rozenite: a modular, TypeScript-ready plugin framework with full device-scoped support.

cover

Feb 12Article

How to Easily Create Native Libraries With Nitro and Turbo Modules

Learn how to optimize React Native performance with Turbo and Nitro Modules. Ditch the legacy bridge and master modern native module development.

cover

Dec 4Podcast

Radon IDE: Better Developer Experience for React Native Teams

Find out how Radon IDE can help increase team velocity and rid you of the common React Native app development pain points.

cover

Sep 19Podcast

Exploring Bun: Jarred Sumner on Hyperfast JavaScript Toolkit

Is Bun the future of React Native package management? Join us as we talk with Jarred Sumner, the creator of this lightning-fast JavaScript toolkit.

cover

Sep 11Podcast

Vim: from Zero to Hero

Discover the power of Vim, the legendary text editor, as we explore its origins, standout features, and practical tips to boost your coding efficiency.

cover

May 15Article

Add-Ons for Web Browsers With React

At times you may need to create an add-on to use a web browser more effectively. Find out how to build one with React.

cover

Jan 27Article

Why Is It Worth to Automate Your Dependency Management with `Autolinking`?

When you automate your dependency management, you can fully focus on developing your app. Find out how you can do it with autolinking.

cover

Mar 19Tutorial

The Hard Way vs. The React Native AI SDK Way: Stop Writing Custom Modules for Every Model

Learn how React Native AI uses a unified provider model to switch between cloud and on-device LLMs without rewriting application logic, enabling reliable AI experiences online and offline.

cover

Mar 12Tutorial

Build Smarter Apps: Tool Calling & AI Orchestration Explained

Learn how on-device models in React Native can call tools, interact with external APIs, and return structured outputs you can use directly in application logic.

cover

Mar 5Tutorial

MLC LLM + React Native: On-Device AI Without the Pain

Learn how to run third-party on-device LLMs in React Native using MLC LLM. Choose your models, run them efficiently across platforms, and keep a unified JavaScript API with React Native AI.

cover

Feb 24Tutorial

Intelligent Fallbacks with Apple Intelligence in React Native

Learn how to use Apple’s built-in Foundation Models in React Native apps, reduce memory usage, and enable fast, fully on-device AI with React Native AI Apple.

cover

Feb 10Tutorial

What Is the React Native AI SDK? A Complete Intro & Quickstart

Learn why on-device AI matters for React Native apps, how local LLMs behave offline, and what problems React Native AI is designed to solve from day one.

cover

Jan 29Tutorial

Implementing an Android TurboModule with Kotlin

Learn how to implement a React Native TurboModule on Android, from the TypeScript spec and Codegen to the Kotlin implementation and package registration.

cover

Jan 19Tutorial

Working with Different Threads in Swift TurboModules

Learn how JavaScript, native module, background, and main threads interact in React Native TurboModules, and how to use GCD queues to run async work safely in Swift.

cover

Jan 9Tutorial

How to Add Type-Safe Constants to Swift TurboModules

Learn how to expose typed, immutable constants from a Swift TurboModule using Codegen, and get full end-to-end type safety between native and JavaScript.

cover

Dec 30Tutorial

Adding Event Emitters to Your TurboModule in Swift

Learn how to emit native events from a Swift TurboModule and subscribe to them in JavaScript, including proper memory management and Codegen integration.

cover

Dec 22Tutorial

Writing Your First TurboModule in Swift

Learn how to build a fully working TurboModule in Swift, integrate it with Codegen, and bridge it through Objective‑C inside an Expo app.