使用React-Native Reanimated遇错误:relativeSourceLocation不是有效插件属性
解决React Native Reanimated报错:Error: relativeSourceLocation is not a valid Plugin property
这个错误通常由babel配置错误或版本不兼容导致,按以下步骤排查修复:
1. 修正Babel配置
打开项目根目录的babel.config.js,检查react-native-reanimated/plugin的配置,不要添加relativeSourceLocation属性,正确配置示例:
module.exports = { presets: ['module:metro-react-native-babel-preset'], plugins: ['react-native-reanimated/plugin'], };
如果之前在插件里写了类似['react-native-reanimated/plugin', { relativeSourceLocation: true }]的代码,直接删除括号里的配置对象即可。
2. 检查版本兼容性
React Native Reanimated的版本需要和你的RN版本匹配:
- Reanimated 3.x 要求 React Native 0.71 及以上
- Reanimated 2.x 适配 React Native 0.64~0.70 版本
如果版本不匹配,执行对应命令调整:
# 降级到Reanimated 2.x(适配RN 0.70及以下) npm install react-native-reanimated@2.17.0 # 或者升级React Native到兼容版本(如果需要用Reanimated 3.x) npx react-native upgrade
3. 清除缓存并重新构建
修改配置或版本后,必须清除缓存才能生效:
# 重置Metro缓存 npx react-native start --reset-cache # 清理依赖并重新安装 rm -rf node_modules package-lock.json npm install # iOS项目需重新安装Pods cd ios && pod install && cd .. # 重新运行项目 npx react-native run-ios # 或 run-android
内容的提问来源于stack exchange,提问作者Atakan Paşalı
相关产品推荐
相关产品推荐

