React中为何出现Module Not Found错误?连接Firebase数据库时遇此问题
React接入Firebase时出现Module Not Found错误
问题描述
尝试将Firebase接入React应用,触发「模块未找到」错误,无法定位firebase/app模块。
实现代码
import firebase from "firebase/app" import "firebase/auth"; const app = firebase.initializeApp({ apiKey: , authDomain: , projectId: , storageBucket: , messagingSenderId: , appId: }) export const auth = app.auth() export default app
错误说明
错误截图显示:无法找到firebase/app模块。
解决方法
- 检查并安装Firebase依赖
在项目根目录执行安装命令:npm install firebase # 或使用yarn yarn add firebase - 适配Firebase v9+模块化语法
若使用Firebase v9及以上版本,旧的命名空间导入方式已被弃用,需改用模块化导入:import { initializeApp } from "firebase/app"; import { getAuth } from "firebase/auth"; const app = initializeApp({ apiKey: "你的API密钥", authDomain: "你的Auth域名", projectId: "你的项目ID", storageBucket: "你的存储桶", messagingSenderId: "你的消息发送者ID", appId: "你的App ID" }); export const auth = getAuth(app); export default app; - 重置依赖(若安装后仍报错)
删除项目根目录下的node_modules文件夹和package-lock.json(或yarn.lock)文件,重新执行安装命令。
内容的提问来源于stack exchange,提问作者Shaheer Ahmed
相关产品推荐
相关产品推荐

