Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invariant Violation: "main" has not been registered

New to React Native: I started a brand new project with Expo init and then I followed the instructions mentioned inhttps://reactnavigation.org/docs/hello-react-navigation I run the project with expo start and I get this error. 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.

invariant browser.js:38:14 runApplication AppRegistry.js:193:13 __callFunction MessageQueue.js:425:19 __guard$argument_0 MessageQueue.js:112:6 __guard MessageQueue.js:373:10 callFunctionReturnFlushedQueue MessageQueue.js:111:4 callFunctionReturnFlushedQueue [native code]:0 Any suggestions?

like image 279
Pankaj Sharma Avatar asked Jun 30 '20 02:06

Pankaj Sharma


People also ask

What is an invariant violation?

Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

Has not been registered this can happen if?

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.

What is AppRegistry Why is it required early in require sequence?

AppRegistry should be required early in the require sequence to make sure the JS execution environment is setup before other modules are required.

What is index JS in react native?

index. js is the starting point for React Native applications, and it is always required. It can be a small file that require s other file that are part of your React Native component or application, or it can contain all the code that is needed for it.


1 Answers

Open the index.js, the content of the file should be like this:

import { AppRegistry, Platform } from 'react-native'; import App from './App';  AppRegistry.registerComponent('X', () => App);  if (Platform.OS === 'web') {     const rootTag = document.getElementById('root') || document.getElementById('X');     AppRegistry.runApplication('X', { rootTag }); } 

If you have this error Invariant Violation: “main” has not been registered you have to replace the 'X' by 'main'.

Another example :

If you have this error Invariant Violation: “app” has not been registered you have to replace the 'X' by 'app'.

For android :

Open ./android/app/src/main/java/[multiple folders]/MainActivity.java

/**    * Returns the name of the main component registered from JavaScript.    * This is used to schedule rendering of the component.    */   @Override   protected String getMainComponentName() {     return "X";   } 

The 'X' of MainActivity.java and index.js must match.

like image 58
justcodin Avatar answered Sep 23 '22 15:09

justcodin