You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 07:07:04