我在网络上多次发现的这个 firebase 片段有什么问题?

What am I doing wrong with this firebase snippet I found multiple times around the web?

Property 'auth' does not exist on type 'typeof import("/home/nuhutuh25/Desktop/ignition/registry/node_modules/firebase/app/dist/app/index")'.ts(2339)

我确实安装了 firebase 作为依赖项,也看到有人成功编写了 const auth=app.auth() 但这也不起作用,因为类型 FirebaseApp 没有属性 auth.

我正在使用打字稿。

您似乎安装了新的 Firebase Modular SDK (V9.0.0+),它的语法与旧的命名空间语法不同。如果你想继续使用现有的语法,你可以切换到 compat 版本:

import firebase from "firebase/compat/app"
import "firebase/compat/auth"

我会推荐 upgrading to Modular SDK 因为兼容库是一个临时解决方案,将在未来的主要 SDK 版本中完全删除。

尝试重构您的代码,如下所示:

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

const app = initializeApp({...firebaseConfig});

export const auth = getAuth(app);