Fix "Invariant Violation: "main" has not been registered" (React Native / Expo)
Invariant Violation: "main" has not been registered. This can happen if: * Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project. * A module failed to load due to an error and `AppRegistry.registerComponent` wasn't called.
- ERROR Invariant Violation: "MyApp" has not been registered. This can happen if:
- Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication)
- Application MyApp has not been registered. This is either due to a require() error during initialization or failure to call AppRegistry.registerComponent.
- Invariant Violation: "main" has not been registered. This can happen if: * Metro (the local dev server) is run from the wrong folder.
The native app asked for a root component named main (or your app name), but the JavaScript bundle never registered it. First look for the error that appears right before (a failing import), check that the name passed to AppRegistry.registerComponent matches the one in native code, then restart a single Metro with --reset-cache.
What this error means
At startup, the native side (MainActivity on Android, AppDelegate on iOS) loads the JavaScript bundle and asks AppRegistry to run the component with a given name: main for Expo, the name from app.json for bare React Native. That component must have been registered by the entry file through AppRegistry.registerComponent or Expo registerRootComponent. If the entry file crashed before that call, if the name does not match, or if the app received another project bundle served by another Metro, no component exists under that name and React Native throws this Invariant Violation. It is almost always the symptom of another error.
Common causes
- Error while loading a module: a missing import, a top-level exception or a missing native module ("could not be found") stops the entry file before
registerComponent. - Different names:
app.jsonsaysMyApp, butgetMainComponentName()(Android) or the AppDelegatemoduleName(iOS) expects another name, often after renaming the project. - Wrong Expo entry point: the
mainfield inpackage.jsonpoints to a file that does not callregisterRootComponent, or is notexpo-router/entrywith Expo Router. - Metro from another project: a Metro started in another folder already holds port 8081 and serves its own bundle to your app.
- Stale Metro cache after an upgrade, an entry point change or a Babel configuration change.
Step-by-step fix
-
Find the first error
Scroll up in the Metro terminal and in LogBox: the real error (module not found,
undefined is not a function, missing native module) is printed before "has not been registered". Fix it first. -
Check the entry file (React Native)
The
index.jsfile must register the root component with the name defined inapp.json.import { AppRegistry } from 'react-native'; import App from './App'; import { name as appName } from './app.json'; AppRegistry.registerComponent(appName, () => App); -
Check the entry point (Expo)
With Expo,
registerRootComponentregisters the component under the namemain. Themainfield inpackage.jsonmust point to that file, or toexpo-router/entryif you use Expo Router.// index.js import { registerRootComponent } from 'expo'; import App from './App'; registerRootComponent(App); // package.json: "main": "index.js" // with Expo Router: "main": "expo-router/entry" -
Compare with the native name
Bare React Native project: the name returned by
getMainComponentName()and the one in the AppDelegate (moduleNameorwithModuleName) must match thenameinapp.json. Fix one side or the other, then rebuild the native app.grep '"name"' app.json grep -rn "getMainComponentName" android/app/src/main grep -rn "moduleName" ios/*/AppDelegate* -
Stop other Metro instances
Check what holds port 8081 and stop that process if it belongs to another project.
# macOS / Linux lsof -i :8081 kill <PID> # Windows (PowerShell) netstat -ano | findstr :8081 taskkill /PID <PID> /F -
Restart Metro without cache and relaunch the app
Start Metro from the project root with its cache cleared, then reload the app (
rkey) or rebuild it if you changed native code.# React Native npx react-native start --reset-cache npx react-native run-android # Expo npx expo start -c
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- To rename a React Native app, change the name in
app.json, inMainActivityand in the AppDelegate together, then rebuild; with Expo, letregisterRootComponenthandle themainname. - Stop Metro when you switch projects, or run each project on its own port with
--port. - Keep the entry file minimal: avoid code that can crash at module level before
registerComponent.
Frequently asked questions
Why "main" when my app has another name?
Expo (and projects generated with expo prebuild) always use the component name main. In a bare React Native project, the message shows the name set on the native side, for example "MyApp" has not been registered.
Why does the error only appear in release?
In release, the bundle is embedded in the app: an error while loading (missing environment variable, unlinked native module) prevents registration. Read the logs with adb logcat or the Xcode console to find the original error.
Should I run expo prebuild --clean?
Only if the generated native code is out of sync with your configuration. This command regenerates android/ and ios/ and erases any manual native changes: commit first.
Useful tools for this error
More Mobile (Android, iOS) errors
- Fix "SDK location not found" (ANDROID_HOME / sdk.dir) on Android
- Fix "CocoaPods could not find compatible versions for pod" on iOS
- Fix "Unable to load script" (index.android.bundle) in React Native
- Fix "Signing for "App" requires a development team" in Xcode
- Fix "INSTALL_FAILED_UPDATE_INCOMPATIBLE: signatures do not match" on Android
- Fix "Duplicate class ... found in modules" in an Android Gradle build
- Fix "cmdline-tools component is missing" and "Android license status unknown" (flutter doctor)