TypeError: Cannot read property ‘length’ of undefined – Firebase

I am trying to connect to Firebase.

Code from firebase.js:

import * as firebase from "firebase/compat/app";

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

const app = !firebase.apps.length
  ? firebase.initializeApp(firebaseConfig)
  : firebase.app();

const db = app.firestore();
const auth = app.auth();
const provider = new firebase.auth.GoogleAuthProvider();

export { db, auth, provider };

Error Message

Answer

You’re missing imports for Firestore (as the error mentions) and Authentication.

import * as firebase from "firebase/compat/app";
import 'firebase/compat/auth';
import 'firebase/compat/firestore';

Also see the documentation on updating imports to v9 compat.