Reanimated JS与原生版本不匹配及Hermes引擎相关报错求助
Reanimated版本不兼容报错的解决办法
你碰到的TypeError: Cannot read property 'create' of undefined和Invariant Violation: "main" has not been registered这两个报错,核心原因是Reanimated的JavaScript端(3.0.2)与原生端(2.14.4)版本不匹配,版本不一致会导致原生模块无法被正确识别加载,进而触发后续的组件注册失败问题。
解决步骤:
统一Reanimated版本
- 卸载当前版本:
npm uninstall react-native-reanimated # 或者用yarn yarn remove react-native-reanimated - 安装与原生端匹配的2.14.4版本:
npm install react-native-reanimated@2.14.4 # 或者用yarn yarn add react-native-reanimated@2.14.4 - 同步原生依赖:
- iOS:进入项目的
ios目录,执行pod install - Android:无需额外操作,Gradle会自动同步依赖
- iOS:进入项目的
- 卸载当前版本:
清理缓存并重启服务
- 终止当前运行的Metro服务器(按下
Ctrl+C) - 清除Metro缓存:
npx react-native start --reset-cache - 重新启动项目:
npx react-native run-android # 或者iOS npx react-native run-ios
- 终止当前运行的Metro服务器(按下
额外排查点
- 确认Metro服务器是在项目根目录启动的,不要在子目录运行
- 用以下命令确认实际安装的Reanimated版本是否一致:
npm list react-native-reanimated # 或者yarn yarn list react-native-reanimated
内容的提问来源于stack exchange,提问作者Engr.Aftab Ufaq
相关产品推荐
相关产品推荐

