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

TypeScript使用Firebase报错Property 'auth'不存在如何解决?

问题解决方法

这个报错是由于你使用的Firebase版本为9.0及以上的模块化版本,旧版命名空间式的导入语法已不再被默认支持,对应类型定义也被移除,才会触发属性不存在的TS校验错误,可选择以下两种方案处理:

方案1:使用官方推荐的模块化写法(推荐)

该写法支持tree shaking减少打包体积,是Firebase v9+的标准写法,直接替换原有代码即可:

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

const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const database = getDatabase(app);

方案2:使用兼容包适配旧写法

如果不想修改现有业务代码,可以切换到Firebase提供的compat兼容导入路径,原有逻辑完全不需要改动:

import firebase from 'firebase/compat/app';
import 'firebase/compat/auth';
import 'firebase/compat/database';

firebase.initializeApp(firebaseConfig);
const auth = firebase.auth();
const database = firebase.database();

额外检查项

如果你使用的是Firebase v8及更早版本仍触发该报错,说明你安装了过时的第三方类型定义包导致冲突,执行以下命令移除即可:
npm uninstall @types/firebase

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 06:48:01