模块AppRegistry未注册为可调用模块。

5

我正在开始学习React Native开发,但是遇到了一个问题。当我尝试运行我的应用程序时,出现了错误:

Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication)
Unhandled JS Exception: Invariant Violation: Native module cannot be null.
Unhandled JS Exception: Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication)

我的App.js:

import React, { Component } from 'react';
import { SafeAreaView } from 'react-native';
import DefaultRouter from './src/navigation/DefaultRouter'

export default class App extends Component {
  render() {
    return (
      <SafeAreaView>
        <DefaultRouter />
      </SafeAreaView>
      );
    }
  };

index.js:

import { AppRegistry } from 'react-native';
import App from './App';
import {name as appName} from './app.json';

AppRegistry.registerComponent(appName, () => App);

DefaultRouter.js:

import { createSwitchNavigator, createAppContainer } from 'react-navigation';
import LoginScreen from '../screen/LoginScreen';
import DefaultTabBar from '../navigation/TabBar';

const DefaultRouter = createSwitchNavigator({
    LOGIN_SCREEN: {
        screen: LoginScreen
    },
    TAB_NAVIGATION: {
        screen: DefaultTabBar
    }
}, {
    initialRouteName: 'LOGIN_SCREEN',
    headerMode: 'none'
})

export default createAppContainer(DefaultRouter)

其他的文件都是简单的Component子类。
这个问题无论我是从Visual Studio Code运行应用还是从终端用react-native run-ios运行应用,都会出现。
我查看了已有的答案,但没有找到任何能指引我正确方向的答案:
React-Native: Module AppRegistry is not a registered callable module
React Native: Module AppRegistry is not a registered callable module (calling runApplication)
module appregistry is not a registered callable module (calling runApplication) Module AppRegistry is not a registered callable module and Cant find variable: Constants
React Native Module AppRegistry is not a registered callable module
Module AppRegistry is not a registered callable module only in Release configuration 我卡在这里,不知道该往哪里去。
3个回答

5

执行npm cache verify或者cd ios && pod install,然后执行npm run ios


为什么?请在此添加原因和解释。 - Jeff Padgett
有时候一些库没有被很好地清理干净,那么开发者可以通过重新安装或手动清理来修复它。 - NMI
这个答案帮助我解决了这个错误,我更新了一个NPM包,必须将其重新链接! - Tim4497

1
在我的情况下,提交 git 后导致错误,然后我发现提交中的错误代码是:
static emitter = Platform.OS === 'ios' ? new NativeEventEmitter(NativeModule) : DeviceEventEmitter;

NativeModule 为 null,因为我还没有在 iOS 上实现它!

当我编辑它时

static emitter = Platform.OS === 'ios' ? {} : DeviceEventEmitter;

然后错误 Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication) 就消失了!


-1

别担心...只需关闭所有服务器进程和节点,然后再次运行您的应用程序,使用以下命令:

react-native run-ios/android

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接