Expo托管React Native项目Firebase包main模块无法解析报错求助
解决方案
这个报错是因为你当前安装的Firebase为v9及以上版本,该版本采用模块化API设计,不再兼容你代码中使用的v8版本命名空间导入规则,直接按以下两种方案处理即可:
方案1:适配v9+模块化API(推荐)
直接替换你的firebase配置文件为以下代码,后续业务逻辑也同步使用模块化调用方式即可:
import { initializeApp } from "firebase/app"; import { getFirestore } from "firebase/firestore"; import { getAuth } from "firebase/auth"; import { getStorage } from "firebase/storage"; // 替换为你的实际配置 const firebaseConfig = { apiKey: "api-key", authDomain: "project-id.firebaseapp.com", databaseURL: "https://project-id.firebaseio.com", projectId: "project-id", storageBucket: "project-id.appspot.com", messagingSenderId: "sender-id", appId: "app-id", measurementId: "G-measurement-id", }; const app = initializeApp(firebaseConfig); const db = getFirestore(app); const auth = getAuth(app); const storage = getStorage(app); export { db, auth, storage };
方案2:降级到Firebase v8(无需修改现有业务代码)
如果不想调整现有业务逻辑,直接降级Firebase版本即可:
- 执行命令卸载现有Firebase:
npm uninstall firebase - 安装指定v8版本:
expo install firebase@8.10.0 - 删除项目根目录下的
.expo缓存文件夹,重启项目即可。
异常排查
如果完成上述操作后仍有报错,执行以下操作清除缓存重试:
- 删除依赖文件和缓存:
rm -rf node_modules package-lock.json yarn.lock rm -rf .expo
- 重新安装依赖:
npm install - 启动时带清除缓存参数:
npx expo start --clear
内容的提问来源于stack exchange,提问作者Joshua Gao
相关产品推荐
相关产品推荐

