Skip to content

In a React Native monorepo, Nx lets you share libraries across apps and run only the tasks a change affects. React Native brings the React declarative UI framework to iOS and Android. With React Native, you use native UI controls and have full access to the native platform.

The Nx Plugin for React Native contains generators for managing React Native applications and libraries within an Nx workspace. It provides:

  • Integration with libraries such as Jest, Detox, and Storybook.
  • Scaffolding for creating buildable libraries that can be published to npm.
  • Utilities for automatic workspace refactoring.

The @nx/react-native plugin supports the following package versions.

PackageSupported Versions
metro-config>= 0.82.0
react-native^0.83.0 || ^0.84.0

Nx generators install the latest supported versions automatically when scaffolding new projects.

To create a new workspace with React Native, run the following command:

Terminal window
npx create-nx-workspace@latest your-workspace-name --preset=react-native --appName=your-app-name
Terminal window
npx create-nx-workspace your-workspace-name

In any Nx workspace, you can install @nx/react-native by running the following command:

Terminal window
nx add @nx/react-native

This will install the correct version of @nx/react-native.

The @nx/react-native plugin will create a task for any project that has an app configuration file present. Any of the following files will be recognized as an app configuration file:

  • app.config.js
  • app.config.ts
  • app.json

The directory must contain both package.json and metro.config.js. React Native inference excludes projects whose app configuration has an expo key or whose package.json lists expo as a dependency or development dependency. The @nx/expo/plugin handles those projects.

To view inferred tasks for a project, open the project details view in Nx Console or run nx show project my-project --web in the command line.

The @nx/react-native/plugin is configured in the plugins array in nx.json.

nx.json
{
"plugins": [
{
"plugin": "@nx/react-native/plugin",
"options": {
"startTargetName": "start",
"podInstallTargetName": "pod-install",
"bundleTargetName": "bundle",
"runIosTargetName": "run-ios",
"runAndroidTargetName": "run-android",
"buildIosTargetName": "build-ios",
"buildAndroidTargetName": "build-android",
"syncDepsTargetName": "sync-deps",
"upgradeTargetName": "upgrade"
}
}
]
}
OptionDefaultInferred behavior
startTargetNamestartRuns react-native start continuously.
podInstallTargetNamepod-installRuns pod install after the dependency synchronization task.
bundleTargetNamebundleRuns react-native bundle after bundling project dependencies.
runIosTargetNamerun-iosRuns react-native run-ios continuously.
runAndroidTargetNamerun-androidRuns react-native run-android continuously.
buildIosTargetNamebuild-iosRuns and caches react-native build-ios with the iOS build outputs.
buildAndroidTargetNamebuild-androidRuns and caches react-native build-android with Android outputs.
syncDepsTargetNamesync-depsRuns the @nx/react-native:sync-deps executor.
upgradeTargetNameupgradeRuns react-native upgrade.

Use include and exclude glob patterns on the plugin entry to scope inference. A target defaults plugin filter must use the exact identifier @nx/react-native/plugin.

To create additional React Native apps run:

Terminal window
nx g @nx/react-native:app apps/<your-app-name>

To generate a new library run:

Terminal window
nx g @nx/react-native:lib libs/<your-lib-name>

To generate a new component inside library run:

Terminal window
nx g @nx/react-native:component <component-path> --export

Replace <component-directory> with the directory where you want to place the component. It must be a path to a directory relative to the workspace root and located inside the library project root.

The Nx CLI provides the migrate command to help you stay up to date with the latest version of Nx.

To upgrade native iOS and Android code to latest, you can use the upgrade-native generator:

Terminal window
nx generate @nx/react-native:upgrade-native apps/<your-app-name>

This is a command that will replace the iOS and Android native code folder entirely.

You can also upgrade React Native iOS and Android code using the rn-diff-purge project.

To start the server that communicates with connected devices:

Terminal window
nx start <your-app-name>

To build your app and start it on iOS simulator or device:

Terminal window
nx run-ios <your-app-name>

To build your app and start it on a connected Android emulator or device:

Terminal window
nx run-android <your-app-name>

To build an iOS app:

Terminal window
nx build-ios <your-app-name>

The build artifacts will be located under <your app folder>/ios/build.

You can specify the build folder by setting the buildFolder option:

Terminal window
nx build-ios <your-app-name> --buildFolder="./build"

To build an Android app, run:

Terminal window
nx build-android <your app name>

The build artifacts will be located under <your app folder>/android/app/build.

In CI, Nx runs nx affected to rebuild and retest only the projects a change touches, and caches results to skip repeated work.

For a complete pipeline, see Set up CI.