React Native APK构建后启动崩溃:require未知模块undefined求助
React Native EAS构建APK后崩溃问题(开发环境正常)
使用eas build -p android --profile production构建的React Native应用,在expo start开发模式下运行完全正常,但打包为APK安装到安卓设备后启动崩溃。预期流程是启动屏执行Fetching函数后跳转至下一页面,实际直接崩溃。
错误日志
com.facebook.react.common.JavascriptException: Error: Requiring unknown module "undefined"., js engine: hermes, stack: unknownModuleError@1:36453 loadModuleImplementation@1:36262 guardedLoadModule@1:35860 metroRequire@1:35531 ?anon_0_@1:1397940 asyncGeneratorStep@1:507933 _next@1:508205 anonymous@1:508157 tryCallTwo@61:8 doResolve@216:24 Promise@82:13 anonymous@1:508078 _getDataFromDB@1:1397896 getDataFromDB@1:1397844 ?anon_0_@1:1398217 asyncGeneratorStep@1:507933 _next@1:508205 tryCallOne@53:15 anonymous@139:26 anonymous@1:195409 _callTimer@1:194406 _callReactNativeMicrotasksPass@1:194570 callReactNativeMicrotasks@1:196484 __callReactNativeMicrotasks@1:62331 anonymous@1:61473 __guard@1:62208 flushedQueue@1:61384 invokeCallbackAndReturnFlushedQueue@1:61327 at com.facebook.react.modules.core.ExceptionsManagerModule.reportException(ExceptionsManagerModule.java:72) at java.lang.reflect.Method.invoke(Native Method) at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372) at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:188) at com.facebook.jni.NativeRunnable.run(Native Method) at android.os.Handler.handleCallback(Handler.java:914) at android.os.Handler.dispatchMessage(Handler.java:100) at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:27) at android.os.Looper.loop(Looper.java:224) at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:228) at java.lang.Thread.run(Thread.java:919)
Package.json
{ "name": "app-name", "version": "1.0.0", "author": "njbsyd", "main": "node_modules/expo/AppEntry.js", "scripts": { "start": "expo start", "android": "expo start --android", "ios": "expo start --ios", "web": "expo start --web" }, "dependencies": { "expo": "~48.0.18", "expo-status-bar": "~1.4.4", "react": "18.2.0", "react-native": "0.71.8", "@expo/vector-icons": "^13.0.0", "@react-native-async-storage/async-storage": "1.17.11", "@react-navigation/material-bottom-tabs": "^6.2.15", "@react-navigation/native": "^6.1.6", "@react-navigation/stack": "^6.3.16", "axios": "^1.4.0", "expo-sqlite": "~11.1.1", "lottie-ios": "^3.4.0", "lottie-react-native": "5.1.4", "react-native-action-button": "^2.8.5", "react-native-autocomplete-input": "^5.3.2", "react-native-element-dropdown": "^2.9.0", "react-native-gesture-handler": "~2.9.0", "react-native-paper": "^5.8.0", "react-native-safe-area-context": "4.5.0", "react-native-screens": "~3.20.0", "react-native-vector-icons": "^9.2.0" }, "devDependencies": { "@babel/core": "^7.20.0" }, "private": true }
eas.json
{ "cli": { "version": ">= 3.13.3" }, "build": { "development": { "developmentClient": true, "distribution": "internal" }, "preview": { "distribution": "internal" }, "production": { "android": { "buildType": "apk" } } }, "submit": { "production": {} } }
排查与解决步骤
1. 定位_getDataFromDB函数中的模块导入问题
错误栈明确指向_getDataFromDB函数,优先检查该函数内的模块导入逻辑:
- 确认是否存在动态require/导入(如
require(modulePath)),若modulePath变量未定义或路径错误,会触发"Requiring unknown module 'undefined'"错误。 - 检查是否在异步流程中导入了未正确声明的模块,建议将静态依赖改为顶部导入,避免运行时动态导入出错。
2. 清理构建缓存
旧缓存可能导致生产构建与开发环境不一致,执行以下命令重新构建:
eas build -p android --profile production --clear-cache
3. 修复依赖冲突与兼容性问题
- 移除
react-native-vector-icons,保留@expo/vector-icons:Expo项目官方推荐使用@expo/vector-icons,两者共存可能导致打包冲突。 - 对齐
lottie-react-native版本:Expo 48对应的lottie-react-native兼容版本为~5.1.1,当前5.1.4可能存在兼容性问题,调整后重新安装依赖:npm install lottie-react-native@~5.1.1
4. 启用Hermes Source Map定位具体错误
修改eas.json的production配置,生成source map便于精准定位错误代码行:
"production": { "android": { "buildType": "apk", "hermesEnabled": true, "sourceMap": true } }
构建后通过Hermes工具将错误栈中的行号映射到源码,快速定位问题代码。
5. 检查Expo SQLite配置
虽然expo-sqlite是Expo预配置模块,但需确认:
- 初始化数据库的逻辑是否在APP启动后执行,避免在启动阶段过早调用导致模块未加载完成。
- 数据库文件路径是否正确,生产环境中路径规则与开发环境可能存在差异。
内容的提问来源于stack exchange,提问作者Najeeb Said
相关产品推荐
相关产品推荐

