Expo React Native真机运行Firebase v9报firestore模块找不到错误
问题说明
- 运行环境:Expo CLI搭建的React Native项目,集成Firebase v9
- 异常场景:Expo Go安卓真机运行触发打包失败,Web端运行无异常;受硬件配置限制无法使用安卓模拟器,仅能通过真机调试
- 已尝试操作:切换多个Firebase版本,问题未解决
完整报错
Android Bundling failed 26102ms While resolving module `firebase/firestore`, the Haste package `firebase` was found. However the module `firestore` could not be found within the package. Indeed, none of these files exist: * `E:\Programming\React_Native\Firebase\firestore(.native|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx|.android.js|.native.js|.js|.android.jsx|.native.jsx|.jsx|.android.json|.native.json|.json)` * `E:\Programming\React_Native\Firebase\firestore\index(.native|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx|.android.js|.native.js|.js|.android.jsx|.native.jsx|.jsx|.android.json|.native.json|.json)`
修复步骤
这个是Metro打包器解析Firebase模块化路径时的常见兼容问题,按顺序操作就能解决:
- 全量清理缓存和重装依赖
先关闭当前运行的Expo服务,在项目根目录执行命令:
用yarn的话把rm -rf node_modules .expo npm installnpm install换成yarn install即可。 - 修正Firebase导入语法
全局检查项目里的Firebase导入语句,必须严格使用v9的标准模块化导入,不要用lite版、旧版兼容导入:❌ 错误示例:
import { getFirestore } from 'firebase/firestore/lite'import firestore from 'firebase/firestore'(v8及以前的命名空间写法)
✅ 正确示例:
import { initializeApp } from 'firebase/app'import { getFirestore } from 'firebase/firestore'
- 添加Metro配置兼容cjs模块
在项目根目录新建metro.config.js文件,写入以下配置:const { getDefaultConfig } = require('@expo/metro-config'); const config = getDefaultConfig(__dirname); config.resolver.assetExts.push('cjs'); module.exports = config; - 固定Firebase兼容版本
Expo Go对Firebase高版本兼容有问题,直接把Firebase版本固定到v9最终稳定版即可,执行命令:npm install firebase@9.23.0 - 清缓存启动服务
执行以下命令启动Expo,不要省略-c参数,必须强制清除Metro旧缓存:
启动后重新用安卓真机的Expo Go扫码连接,就能正常打包运行,不会再报找不到firestore模块的错误。npx expo start -c
内容的提问来源于stack exchange,提问作者Abhishek
相关产品推荐
相关产品推荐

