Native Modules & Bridging — Mobile

Write a native module only when Expo packages, community libraries and config plugins aren't enough, for instance for a vendor's printer SDK. The old bridge…

The Bluetooth thermal printer at Mrs. Rini's counter only understands its maker's own language. Its maker (call it "MyPrinter", an example) only ships SDKs for Android and iOS, and JavaScript can't call those SDKs directly. Mrs. Rini (JavaScript) needs an interpreter sitting next to the printer: a native module, Swift (iOS) and Kotlin (Android) code inside your app that JavaScript can call. You write one when what you need isn't in an Expo package or a community library yet, and a cashier tablet that has to print receipts on this printer is such a case.

When you need native code

Check from the top, and stop at the first answer that's enough:

  1. Is there an Expo package? Camera, location, notifications, secure storage: they exist (sensors and push lessons).
  2. Is there a community library? Search React Native Directory, and make sure it supports the New Architecture.
  3. Do you only need native configuration? Permission texts, entitlements, intent filters: a config plugin (Expo lesson), no native code.
  4. Do you need code that calls a native API or a vendor SDK? Only then write a native module. Mrs. Rini's printer lands here.

JSI and the New Architecture

In the old architecture, JavaScript and native talked through the bridge: every call became a JSON message, was queued, then processed asynchronously on the other side. Since React Native 0.82 there's only the New Architecture (first lesson), and its base is JSI (JavaScript Interface): JavaScript holds references to C++ objects and calls their methods directly, without turning them into JSON, even synchronously. On top of JSI sit TurboModules (native modules loaded when used, with a typed contract from Codegen) and Fabric (the new renderer). The Expo Modules API is built on JSI too, and its docs say its performance is similar to Turbo Modules: both handle hundreds of thousands of native calls per second.

Expo Modules API: a local module

Run in Mrs. Rini's Laundry project (create-expo-module 57.0.1, 11 October 2026):

$ npx create-expo-module@latest --local thermal-printer --name ThermalPrinter --package com.rinislaundry.thermalprinter --platform apple android --features Constant Function AsyncFunction Event
The local module will be created in modules/thermal-printer directory.
✔ Downloaded module template from npm registry.
✔ Created the module from template files
Remember to re-build your native app (for example, with npx expo run) when you make changes to the module. Native code changes are not reloaded with Fast Refresh.

The result, modules/thermal-printer/, holds expo-module.config.json, ios/ThermalPrinterModule.swift, ios/ThermalPrinter.podspec, android/.../ThermalPrinterModule.kt and src/ThermalPrinterModule.ts. Modules in the modules/ folder are found by autolinking without being listed in package.json. The template was changed into a printer module (the MyPrinter SDK is only an example, so this native code isn't compiled here):

// modules/thermal-printer/ios/ThermalPrinterModule.swift
import ExpoModulesCore

struct Receipt: Record {
  @Field var id: String = ""
  @Field var name: String = ""
  @Field var kg: Double = 0
  @Field var total: Int = 0
}

public class ThermalPrinterModule: Module {
  public func definition() -> ModuleDefinition {
    Name("ThermalPrinter")

    Function("isConnected") { () -> Bool in
      MyPrinter.shared.isConnected
    }

    AsyncFunction("printReceipt") { (receipt: Receipt) -> Bool in
      guard MyPrinter.shared.isConnected else {
        throw Exception(name: "PrinterOffline", description: "Printer not connected", code: "ERR_PRINTER_OFFLINE")
      }
      try MyPrinter.shared.print(lines: ["MRS. RINI'S LAUNDRY", "#\(receipt.id) \(receipt.name)", "\(receipt.kg) kg  $\(receipt.total)"])
      return true
    }
  }
}

The Kotlin version has the same shape: class Receipt : Record with @Field val, Function("isConnected") { MyPrinter.isConnected() }, and an AsyncFunction("printReceipt") { receipt: Receipt -> ... } that throws CodedException("ERR_PRINTER_OFFLINE", "Printer not connected", null).

// modules/thermal-printer/src/ThermalPrinterModule.ts
import { NativeModule, requireNativeModule } from 'expo';

export type Receipt = { id: string; name: string; kg: number; total: number };

declare class ThermalPrinterModule extends NativeModule {
  isConnected(): boolean;
  printReceipt(receipt: Receipt): Promise<boolean>;
}

export default requireNativeModule<ThermalPrinterModule>('ThermalPrinter');

Guess first: the local module thermal-printer is done, and you open the cashier screen that imports it in Expo Go, or in a development build made before the module existed. What happens?

Native code only exists in the binary that was built. Expo Go and an old development build have no ThermalPrinter, so requireNativeModule fails as soon as the file is imported: Error: Cannot find native module 'ThermalPrinter'. Make a new build: npx expo run:android, npx expo run:ios, or eas build --profile development. Use requireOptionalNativeModule (which returns null) when the screen must keep working without the module.

A Turbo Module with Codegen

React Native's built-in way. The contract is written as a TypeScript spec, and Codegen generates C++ and native code from it at build time (the Gradle task generateCodegenArtifactsFromSchema on Android, during pod install on iOS):

// specs/NativeThermalPrinter.ts (the file name must start with "Native")
import type { TurboModule } from 'react-native';
import { TurboModuleRegistry } from 'react-native';

export interface Spec extends TurboModule {
  isConnected(): boolean;
  printReceipt(receipt: { id: string; name: string; kg: number; total: number }): Promise<boolean>;
}

export default TurboModuleRegistry.getEnforcing<Spec>('NativeThermalPrinter');

Plus a codegenConfig in package.json (name, type: "modules", jsSrcsDir: "specs", android.javaPackageName), then an implementation in Kotlin and Objective-C++. If the module isn't in the binary yet: TurboModuleRegistry.getEnforcing(...): 'NativeThermalPrinter' could not be found. Verify that a module by this name is registered in the native binary. Choose a Turbo Module when you need shared C++ for both platforms, or a library that shouldn't depend on the expo package. For an Expo app, the Expo Modules API is more compact.

A config plugin for Bluetooth permission

The printer module needs Bluetooth permission: NSBluetoothAlwaysUsageDescription in Info.plist, and BLUETOOTH_CONNECT on Android 12 and up. Don't write it by hand in the ios/ folder (lost on prebuild, Expo lesson). Write a config plugin:

// plugins/withPrinterBluetooth.js, then "./plugins/withPrinterBluetooth" in app.json's plugins
const { AndroidConfig, withInfoPlist } = require('expo/config-plugins');

module.exports = function withPrinterBluetooth(config, { reason } = {}) {
  config = withInfoPlist(config, (cfg) => {
    cfg.modResults.NSBluetoothAlwaysUsageDescription = reason ?? "Allow Mrs. Rini's Laundry to connect to the receipt printer.";
    return cfg;
  });
  return AndroidConfig.Permissions.withPermissions(config, ['android.permission.BLUETOOTH_CONNECT']);
};

npx expo config --type introspect in this project shows NSBluetoothAlwaysUsageDescription: "Allow Mrs. Rini's Laundry to connect to the receipt printer." and 'android.permission.BLUETOOTH_CONNECT' in the permission list. Without that plugin, iOS stops the app when Bluetooth is used: This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSBluetoothAlwaysUsageDescription key with a string value explaining to the user how the app uses this data. On Android 12 and up, BLUETOOTH_CONNECT is also requested at runtime, for example with PermissionsAndroid.request.

⚠️ Why is the native module missing, or freezing the app?

TL;DR: Write a native module only when Expo packages, community libraries and config plugins aren't enough, for instance for a vendor's printer SDK. The old bridge was an asynchronous link passing JSON messages between JavaScript and native; the New Architecture (TurboModules and Fabric, the only one since React Native 0.82) uses JSI, so JavaScript calls native directly, synchronously if needed, with no JSON serialisation. Expo Modules API: npx create-expo-module@latest --local makes modules/thermal-printer with Swift, Kotlin and TypeScript; Name, Function (synchronous, on the JavaScript thread), AsyncFunction (a Promise, another thread), Record for objects, and requireNativeModule('ThermalPrinter') with a typed interface. A Turbo Module uses a NativeThermalPrinter.ts spec, TurboModuleRegistry.getEnforcing and Codegen. Native code needs a new build: in Expo Go the result is Cannot find native module 'ThermalPrinter'. Bluetooth permission goes in through a config plugin (withInfoPlist, AndroidConfig.Permissions.withPermissions), not by hand.

⚡ From the bridge to JSI

Meaning: The old bridge was an asynchronous link: every call from JavaScript to native became a JSON message, was queued, then processed on the other side. JSI replaces it: JavaScript calls native directly, synchronously if needed, with no JSON serialisation. TurboModules and Fabric are built on top of JSI.

⚡ Function or AsyncFunction

Write:

Function("isConnected") { () -> Bool in
  MyPrinter.shared.isConnected
}

Meaning: A Function is synchronous: its native code runs on the JavaScript thread and holds it until it's done, which fits quick checks. Printing over Bluetooth is slow, so printReceipt is written as an AsyncFunction: it always returns a Promise and runs on another thread.

⚡ A new module needs a new build

Guess first: the local module thermal-printer is done, and you open the cashier screen that imports it in Expo Go. What happens?

An error as soon as the file is imported: Cannot find native module 'ThermalPrinter'. Native code only exists in the binary that was built, and Expo Go doesn't have that module.

Meaning: Make a new build with npx expo run:android or eas build --profile development. When the screen must keep working without the module, use requireOptionalNativeModule, which returns null.

What you will learn