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

Viewed 56

Consider:

Enter image description here

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

I did install Firebase as a dependency and also saw someone successfully writing const auth=app.auth(), but that does not work either, because type FirebaseApp doesn't have any attribute auth.

I am using TypeScript.

1 Answers

It seems you have the new Firebase Modular SDK installed (V9.0.0+) which has a new syntax unlike the older name-spaced one. If you want to keep using existing syntax you can switch to compat version:

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

I would recommend upgrading to Modular SDK because the compat libraries are a temporary solution that will be removed completely in a future major SDK version.

Try refactoring your code as shown below:

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

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

export const auth = getAuth(app);
Related