Lottie React Native报错:未知模块"2236",求解决方法
解决lottie-react-native中"Requiring unknown module '2236'"错误
问题场景
使用lottie-react-native时触发如下错误,已尝试重启Metro、执行yarn安装依赖,但问题未解决:
Error: Requiring module "node_modules/lottie-react-native/lib/index.js", which threw an exception: Error: Requiring unknown module "2236". If you are sure the module exists, try restarting Metro. You may also want to run `yarn` or `npm install`.
相关代码:
import React from 'react'; import { StyleSheet, View } from 'react-native'; import LottieView from 'lottie-react-native'; export default function App() { return ( <View style={styles.animationContainer}> <LottieView autoPlay style={{ width: 200, height: 200, backgroundColor: '#eee', }} source={require('./assets/gradientBall.json')} /> </View> ); } const styles = StyleSheet.create({ animationContainer: { backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', flex: 1, }, buttonContainer: { paddingTop: 20, }, });
解决方法
彻底清除Metro缓存
执行命令重置Metro缓存后重启服务:npx react-native start --reset-cache核对版本兼容性
确认lottie-react-native版本与你的React Native版本匹配(例如RN 0.70+建议使用lottie-react-native 5.x系列)。若版本不兼容,卸载后重装适配版本:yarn remove lottie-react-native yarn add lottie-react-native@5.1.6 # 版本号根据RN实际情况调整检查动画文件完整性
验证./assets/gradientBall.json是否存在语法错误或文件损坏,可替换为官方测试用的Lottie JSON文件排查问题。如果替换后正常,说明原动画文件需重新下载或修复。重置所有依赖与项目缓存
执行以下步骤彻底清理并重建依赖:# 删除依赖目录与锁文件 rm -rf node_modules rm yarn.lock # 重装依赖 yarn install # iOS项目额外操作 cd ios && pod install --repo-update && cd .. # Android项目额外操作 cd android && ./gradlew clean && cd .. # 重置Metro缓存并启动 npx react-native start --reset-cache检查Metro配置
打开metro.config.js,确认blockList(或旧版的blacklistRE)中没有阻止lottie-react-native相关模块加载的规则,如有则移除后重启Metro。
内容的提问来源于stack exchange,提问作者Dagus Wijaya
相关产品推荐
相关产品推荐

