React Native Expo项目报错:Invariant Violation: 'main'未注册求助
解决"Invariant Violation: 'main' has not been registered"错误
问题根源
你在src/app/chama.tsx中错误调用了registerRootComponent(HelloWorld),这会和Expo默认的根组件注册逻辑冲突,导致入口识别混乱。Expo项目中,根组件由app.tsx的默认导出自动注册,不需要额外在其他文件执行registerRootComponent。
修复步骤
修改
src/app/chama.tsx
删除文件末尾的registerRootComponent(HelloWorld);,修改后的代码如下:import { Text, View } from "react-native" export function HelloWorld() { return <View style={{ flex:1, justifyContent: "center", alignItems:"center"}}> <Text>Hello World</Text> </View> }清除缓存并重启项目
运行以下命令清除Expo缓存,避免旧编译代码干扰:expo start -c
额外检查
- 确认
package.json中的main字段保持为"node_modules/expo/AppEntry.js",这是Expo默认的入口配置,会自动关联到你的app.tsx。 - 检查
tsconfig.json的路径别名是否正确,当前配置"@/*": ["./src/*"]是有效的,@/app/chama能正确指向src/app/chama.tsx。
内容的提问来源于stack exchange,提问作者Ming
相关产品推荐
相关产品推荐

