执行npx react-native run-ios时Hermes引擎报错求助
解决React Native run-ios时AppRegistry未注册报错
报错信息
ERROR TypeError: undefined is not a function, js engine: hermes ERROR Invariant Violation: Failed to call into JavaScript module method AppRegistry.runApplication(). Module has not been registered as callable. Bridgeless Mode: false. Registered callable JavaScript modules (n = 9): Systrace, JSTimers, HeapCapture, SamplingProfiler, RCTLog, RCTDeviceEventEmitter, RCTNativeAppEventEmitter, GlobalPerformanceLogger, HMRClient. A frequent cause of the error is that the application entry file path is incorrect. This can also happen when the JS bundle is corrupt or there is an early initialization error when loading React Native., js engine: hermes
已尝试无效操作
- 重新安装Pod:
rm -rf Pods/ && rm -rf Podfile.lock && pod install - 更新Pod
- 清理npm缓存
- 卸载重装node_modules
- 改用Yarn安装依赖
可行排查方案
1. 验证入口文件配置
- 检查
ios/<项目名称>/AppDelegate.mm(或.m)中的入口路径是否正确,确保指向项目根目录的index.js/index.tsx:- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge { #ifdef DEBUG return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil]; #else return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"]; #endif } - 确认根目录入口文件中正确注册App:
import { AppRegistry } from 'react-native'; import App from './App'; import { name as appName } from './app.json'; AppRegistry.registerComponent(appName, () => App);
2. 排查Hermes引擎兼容性
- 临时禁用Hermes,修改
ios/Podfile:
执行use_react_native!( :path => config[:reactNativePath], # 注释此行以禁用Hermes # :hermes_enabled => true )pod install后重新运行项目,若恢复正常则说明Hermes存在配置或语法兼容问题,可尝试更新Hermes版本或排查代码中不兼容语法。
3. 清理iOS构建缓存
- 打开Xcode执行
Product > Clean Build Folder(快捷键Cmd+Shift+K) - 删除DerivedData目录:
rm -rf ~/Library/Developer/Xcode/DerivedData - 重新执行
npx react-native run-ios
4. 定位早期初始化错误
- 打开远程调试(
Cmd+D唤起开发者菜单,选择Debug JS Remotely),查看Chrome控制台的完整报错日志,定位AppRegistry注册前的模块导入、语法错误等问题。 - 临时替换
App.js为最简组件,排查是否为业务代码导致的初始化失败:import React from 'react'; import { View, Text } from 'react-native'; const App = () => { return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Text>Test</Text> </View> ); }; export default App;
5. 确认版本兼容性
- 检查
package.json中react与react-native版本是否匹配,无冲突可尝试升级到稳定版本:
重新安装依赖及Pod后运行项目。npm install react-native@latest react@latest
内容的提问来源于stack exchange,提问作者ImDarkk
相关产品推荐
相关产品推荐

