React Native Codegen Tools
react-native-codegen is an essential internal tool within the React Native ecosystem, responsible for generating native code (Java, Objective-C++, C++) from JavaScript type definitions (Flow or TypeScript). These generated definitions form the basis of "TurboModules" and "Fabric Components" in React Native's New Architecture, enabling type-safe, performant communication between JavaScript and native platforms. It primarily operates at build time, invoked by the React Native CLI, to automatically create the necessary bridging code for native modules and UI components. The package versioning is tightly coupled with the React Native monorepo, with the latest stable versions generally mirroring the `react-native` package's minor version (e.g., `react-native-codegen@0.85.x` for `react-native@0.85.x`). Its release cadence follows the React Native release schedule, typically bi-monthly or quarterly for minor versions. It differentiates itself from older bridging mechanisms by providing a standardized, strongly-typed, and automatically generated interface, significantly reducing boilerplate and improving developer experience and performance for native module authors.
Common errors
-
error: Type 'Int32' is not assignable to type 'number'.
cause Using `Int32` or other codegen-specific types directly in TypeScript/Flow code that isn't intended for the spec file, or incorrect environment setup.fixEnsure `Int32` and similar types are only used within the `TurboModule` or `FabricComponent` spec files. For regular JavaScript/TypeScript, use standard number types. Also, verify your TypeScript `tsconfig.json` or Flow config properly recognizes the codegen type definitions. -
Command failed with exit code 1
cause A generic error indicating that the `codegen-native-modules` command encountered an issue, often due to malformed spec files, missing dependencies, or incorrect paths.fixInspect the console output preceding the error for more specific messages (e.g., parsing errors, unsupported types, file access issues). Double-check your spec files for syntax errors and ensure all required tools (e.g., `clang`, Java SDK) are correctly installed and configured in your environment. -
Cannot find module 'react-native-codegen/lib/cli/index' or similar path.
cause Mismatched `react-native-codegen` and `react-native` versions, incorrect import paths in custom scripts, or `node_modules` corruption.fixRun `npm install` or `yarn install` to ensure all dependencies are correctly linked. Verify your `react-native-codegen` version is compatible with your `react-native` version. If using custom scripts, ensure the import path precisely matches the package's internal structure for that version. -
Expected a call signature, but got a Flow type annotation. Did you mean to use 'function' instead of 'interface'?
cause This error typically occurs when a TypeScript or Flow spec file intended for codegen has syntax that the codegen parser doesn't understand or misinterprets.fixReview the official React Native documentation for the exact syntax required for TurboModule and Fabric Component spec files. Ensure you are using the correct type annotations and interface definitions as expected by `react-native-codegen`.
Warnings
- breaking Projects upgrading to React Native versions 0.68 and above (which include `react-native-codegen`) must adopt the "New Architecture" (TurboModules/Fabric) for native modules and components. Legacy native modules will typically require significant refactoring or the use of compatibility layers. This is a fundamental shift in how native modules are defined and integrated.
- gotcha The `react-native-codegen` package is tightly coupled with the `react-native` package. Using a `react-native-codegen` version incompatible with your `react-native` version (e.g., from a different major series like `0.70.x` with `0.85.x`) will almost certainly lead to build failures due to API mismatches or schema inconsistencies.
- gotcha The Flow/TypeScript spec file format used for defining TurboModules and Fabric Components can evolve between React Native minor and major versions. Changes to type definitions, allowed syntax, or structural requirements in these spec files can break code generation.
- gotcha Ensure the `npx react-native codegen-native-modules` command (or `yarn react-native codegen-native-modules`) is executed in the correct project root, with all necessary `--platform` flags (e.g., `ios`, `android`), for the generated native code to be correctly created and linked into your native builds.
Install
-
npm install react-native-codegen -
yarn add react-native-codegen -
pnpm add react-native-codegen
Imports
- generateFromSchema
import { generateFromSchema } from 'react-native-codegen/lib/generators/GenerateFromSchema'; - CodegenConfig
import type { CodegenConfig } from 'react-native-codegen/lib/cli/types'; - cli
import * as cli from 'react-native-codegen/lib/cli';
Quickstart
import type { TurboModule } from 'react-native';
import type { Int32 } from 'react-native/Libraries/Types/CodegenTypes';
// native_modules/MyNativeModule/NativeMyNativeModule.ts
/**
* This is the interface for your native module, defining methods and constants.
* The codegen tool reads this TypeScript (or Flow) spec file.
*/
export interface Spec extends TurboModule {
/**
* Say hello to the native side.
* @returns A greeting string from native.
*/
sayHello(): Promise<string>;
/**
* Adds two integers on the native side.
* @param a The first integer.
* @param b The second integer.
* @returns The sum of a and b.
*/
add(a: Int32, b: Int32): Int32;
/**
* Returns a constant object.
*/
getConstants(): {
DEFAULT_VALUE: string;
};
}
// Declare the global TurboModule proxy for type safety
declare global {
var __turboModuleProxy: (moduleName: string) => Spec | null;
}
// --- To generate native code, run this command in your project root ---
// npx react-native codegen-native-modules \
// --platform ios \
// --platform android \
// --project-root . \
// --output-dir ./node_modules/my-native-module/generated
// (Note: The --output-dir is illustrative; in a real project, this might be managed by the CLI)