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

Expo React Native中Firebase Auth初始化报错:getReactNativePersistence未定义

解决Firebase Auth初始化时getReactNativePersistence is not a function报错

1. 修正导入路径与初始化代码

Firebase 9.x中getReactNativePersistence的正确导入路径是firebase/auth,需配合Expo的AsyncStorage使用。按以下步骤调整:

  • 先安装AsyncStorage:
    expo install @react-native-async-storage/async-storage
    
  • 修改Firebase初始化代码:
    import { initializeApp } from 'firebase/app';
    import { initializeAuth, getReactNativePersistence } from 'firebase/auth';
    import AsyncStorage from '@react-native-async-storage/async-storage';
    
    const firebaseConfig = {
      // 你的Firebase配置信息
    };
    
    const app = initializeApp(firebaseConfig);
    
    // 用AsyncStorage作为持久化层初始化Auth
    initializeAuth(app, {
      persistence: getReactNativePersistence(AsyncStorage)
    });
    

2. 统一Firebase版本

Expo文档推荐Firebase 9.1,需保证firebase核心包与子模块版本完全一致。检查package.json,若版本不一致,执行:

npm install firebase@9.1.0
# 或使用yarn
yarn add firebase@9.1.0

3. 清除Expo缓存

缓存可能导致模块解析异常,执行以下命令清除缓存后重启项目:

expo r -c

报错原因说明

Firebase 9.x早期版本对getReactNativePersistence的导出路径做过调整,导入错误、版本不匹配或未正确安装AsyncStorage,都会触发这个TypeError。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 00:45:49