MD. NAZMUL ISLAM
HomeServicesPortfolioCase StudiesBlogAboutContact
ResumeLoginSign Up
Building Custom Native Modules in Expo With Native Modules API
  1. Home
  2. Blog
  3. Building Custom Native Modules in Expo With Native Modules API
Back to Blog
React NativeExpoMobileiOSAndroidJavaScript

Building Custom Native Modules in Expo With Native Modules API

November 6, 20265 min read30 views0 comments

Learn how to write custom native modules in Expo using the new Native Modules API for seamless iOS and Android integration.

Building Custom Native Modules in Expo With Native Modules API

When building mobile applications with React Native and Expo, you can usually rely on the rich ecosystem of existing libraries. Whether you need camera access, secure storage, or push notifications, there is almost always an Expo module ready to install. But every now and then, you run into a very specific hardware integration, a legacy SDK, or a complex performance requirement that forces you to drop down to the native layer.

In the past, writing custom native modules meant ejecting from Expo or dealing with complex bridging code that often broke during upgrades. Today, thanks to the modern Expo Modules API, writing Swift and Kotlin code and exposing it safely to JavaScript is cleaner, faster, and fully compatible with Expo EAS Build. Let me walk you through how I approach building custom native modules in my client projects.

When Do You Actually Need a Native Module?

Before writing any native code, it is worth asking if you really need to. Writing native modules means maintaining Swift code for iOS and Kotlin code for Android, which adds overhead to your codebase.

Common valid use cases include:

  • Integrating proprietary SDKs provided by hardware vendors or payment terminals.
  • Performing heavy background processing or cryptography that chokes the JavaScript thread.
  • Accessing niche device sensors or system settings not covered by standard libraries.

If your requirement falls into one of these buckets, writing a custom native module using the Expo Modules API is the cleanest path forward.

Setting Up the Module Structure

Expo encourages developing local modules directly inside your project workspace before publishing them to npm. You can create a local module folder within your project directory, structure it correctly, and configure your app.json to link it.

A typical local module folder looks like this:

modules/
  └── my-native-scanner/
      ├── android/
      ├── ios/
      ├── index.ts
      └── expo-module.config.json

The expo-module.config.json tells the Expo autolinking system how to discover your native files for both iOS and Android during prebuilding.

{
  "platforms": ["ios", "android"],
  "ios": {
    "modules": ["MyNativeScannerModule"]
  },
  "android": {
    "modules": ["com.mynativescanner.MyNativeScannerModule"]
  }
}

Writing the iOS Implementation with Swift

On iOS, Expo modules are written in Swift using the ExpoModulesCore framework. This framework provides clean macros that make exporting functions, constants, and event listeners straightforward.

Here is a simple example of an iOS module that retrieves device battery status or performs a native task:

import ExpoModulesCore

public class MyNativeScannerModule: Module {
  public func definition() -> ModuleDefinition {
    // Sets the name of the module in JavaScript
    Name("MyNativeScanner")

    // Defines synchronous functions callable from JS
    Function("getSystemInfo") { () -> String in
      return "iOS Native Bridge Active"
    }

    // Defines asynchronous functions using promises
    AsyncFunction("scanCode") { (options: [String: Any], promise: Promise) in
      DispatchQueue.main.async {
        // Perform native scanning logic here
        let success = true
        if success {
          promise.resolve("SCAN_RESULT_12345")
        } else {
          promise.reject("SCAN_ERROR", "Failed to scan code", nil)
        }
      }
    }
  }
}

Writing the Android Implementation with Kotlin

On Android, you implement the corresponding module in Kotlin using the same architectural patterns. Kotlin makes interacting with Android system APIs concise and readable.

Here is how the Android counterpart looks:

package com.mynativescanner

import expo.modules.kotlin.modules.Module
import expo.modules.kotlin.modules.ModuleDefinition

public class MyNativeScannerModule : Module {
  override fun definition() = ModuleDefinition {
    Name("MyNativeScanner")

    Function("getSystemInfo") {
      return@Function "Android Native Bridge Active"
    }

    AsyncFunction("scanCode") { options: Map<String, Any>, promise: expo.modules.kotlin.promise.Promise ->
      // Perform native Android scanning logic here
      val success = true
      if (success) {
        promise.resolve("SCAN_RESULT_12345")
      } else {
        promise.reject("SCAN_ERROR", "Failed to scan code", null)
      }
    }
  }
}

Exposing the Module to TypeScript

Once your Swift and Kotlin code are in place, you expose them through your TypeScript entry file using requireNativeModule provided by expo-modules-core.

import { requireNativeModule } from 'expo-modules-core';

// Import the native module by the name defined in Swift/Kotlin
const MyNativeScanner = requireNativeModule('MyNativeScanner');

export function getSystemInfo(): string {
  return MyNativeScanner.getSystemInfo();
}

export async function scanCode(options: { timeout: number }): Promise<string> {
  return await MyNativeScanner.scanCode(options);
}

Now, you can import this function anywhere in your React Native app just like any standard TypeScript module.

Testing and Building with EAS

Because custom native modules contain compiled Swift and Kotlin code, standard Expo Go will not be able to execute your native changes directly. You will need to use development builds (npx expo run:ios and npx expo run:release or npx expo run:android) to test your code locally.

When you are ready for production, EAS Build handles compiling your custom Swift and Kotlin code automatically in the cloud, exactly as it does for any other native dependency.

Wrapping Up

Writing custom native modules used to be a major headache in cross-platform mobile development. Today, the Expo Modules API makes it remarkably clean and maintainable. By keeping your native code encapsulated and well-tested, you can extend your React Native apps to do almost anything a fully native app can do.

Need help building a custom mobile app or integrating complex native SDKs into your Expo project? Get in touch and let's talk about your requirements.

Tagged with

React NativeExpoMobileiOSAndroidJavaScript

Comments

Leave a comment

You must be signed in to comment.

MD. NAZMUL ISLAM

Full-Stack & React Native Developer

Send a project enquiry

Navigation

  • Home
  • About
  • Services
  • Case Studies
  • Blog
  • Contact
  • 한국어 (Korean)

Services

  • Web Development
  • Mobile App Development
  • Custom Software & SaaS
  • SEO & Marketing
  • Domain & Hosting

© 2026 MD. Nazmul Islam. All rights reserved.

Privacy Policy·Terms of Service