使用Expo安装react-native-chart-kit后运行报错求助
解决Expo安装react-native-chart-kit后的启动报错问题
问题根源
报错Invariant Violation: Tried to register two views with the same name RNSVGRect和"main" has not been registered,本质是项目中同时存在两个版本的react-native-svg:
- react-native-chart-kit@6.12.0依赖react-native-svg@15.2.0
- react-native-gradient-texts@0.1.0依赖react-native-svg@13.14.1
不同版本的svg组件重复注册原生视图,导致Metro加载失败,进而触发main组件未注册的连锁错误。
解决方案
1. 强制统一react-native-svg版本
根据项目已有的最高版本(15.2.0),强制所有依赖使用同一版本:
- 若使用npm,在
package.json中添加overrides字段:"overrides": { "react-native-svg": "15.2.0" } - 若使用yarn,在
package.json中添加resolutions字段:"resolutions": { "react-native-svg": "15.2.0" }
添加后执行npm install或yarn install重新安装依赖。
2. 清理缓存并重启服务
完全停止当前运行的Metro服务,执行以下命令清除缓存并重启:
npx expo start --clear
3. 验证修复结果
执行npm ls react-native-svg或yarn list react-native-svg,确认输出中所有依赖的react-native-svg都统一为15.2.0,无多版本共存即可。
内容的提问来源于stack exchange,提问作者RoAJ
相关产品推荐
相关产品推荐

