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

React Native项目报Firebase.auth() is not a function错误如何解决?

报错触发原因
  • 当前使用的是Firebase 9.0及以上版本,该版本重构为模块化API架构,完全移除了旧版本firebase.auth()这类命名空间调用方式
  • 仅导入了firebase/app核心包,未引入Authentication功能对应的扩展模块
解决方法

推荐使用官方主推的模块化API实现,改动如下:

  1. 首先确保已安装最新版Firebase依赖:
    npm install firebase

  2. 修正App.js初始化代码:

import { initializeApp } from 'firebase/app';
import { getAuth } from 'firebase/auth';

const firebaseConfig = {
  apiKey: "",
  authDomain: "",
  projectId: "",
  storageBucket: "",
  messagingSenderId: "",
  appId: ""
};

const app = initializeApp(firebaseConfig);
// 导出auth实例供全局使用
export const auth = getAuth(app);
  1. 修正登录逻辑代码:
import { signInWithEmailAndPassword } from 'firebase/auth';
// 路径需根据你的实际文件目录调整
import { auth } from './App';

export const loginRequest = (email, password) => 
  signInWithEmailAndPassword(auth, email, password);

如果你不想修改现有旧风格的代码,可以使用兼容版本的导入方式,原有逻辑无需调整:

// 将所有firebase/app的导入替换为compat版本
import * as firebase from "firebase/compat/app";
// 导入compat版本的auth模块
import "firebase/compat/auth";

内容的提问来源于stack exchange,提问作者Patrick89

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 16:06:10