-
Notifications
You must be signed in to change notification settings - Fork 987
Closed
Labels
Description
Operating System
macOS
Environment (if applicable)
Node.JS 20
Firebase SDK Version
12.5.0
Firebase SDK Product(s)
Firestore, Functions
Project Tooling
Turborepo, PNPM
Detailed Problem Description
Since v12.5.0 I suddenly get the error Service firestore is not available (and analogous for other service Service functions is not available) without changing my firebase init
The error happens at either of these places
dbDev = getFirestore(app, "development");
dbDefault = getFirestore(app);
functions = getFunctions(app, firebaseFunctions.region);
// packages/config-firebase-client/src/init-client.ts
"use client";
import { type Auth, getAuth, GoogleAuthProvider } from "firebase/auth";
import firebase from "firebase/compat/app";
import "firebase/compat/auth";
import "firebase/compat/firestore";
import { type Firestore, getFirestore } from "firebase/firestore";
import { type Functions, connectFunctionsEmulator, getFunctions } from "firebase/functions";
import { getStorage, type FirebaseStorage } from "firebase/storage";
import { firebaseFunctions } from "@repo/config/site";
import { isDevOrLocalPrd } from "@repo/utils/development";
// Firebase Client Configuration
const firebaseClientConfig = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
measurementId: process.env.NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID,
};
// Vars to export
let app: firebase.app.App;
export let dbDev: Firestore; // db for dev purposes
export let dbDefault: Firestore; // db for dev purposes
export let functions: Functions;
export let storage: FirebaseStorage;
export let auth: Auth;
export let provider: GoogleAuthProvider;
// Init Firebase components
function initFirebaseVars(app: firebase.app.App) {
console.log("[firebase/config-client] init vars for app:", app.name);
dbDev = getFirestore(app, "development");
dbDefault = getFirestore(app);
functions = getFunctions(app, firebaseFunctions.region);
storage = getStorage(app);
auth = getAuth(app);
auth.languageCode = "de"; // For phone auth https://firebase.google.com/docs/auth/web/phone-auth?hl=en&authuser=0
provider = new GoogleAuthProvider();
// Connect to the local emulator if running in development mode
if (isDevOrLocalPrd) {
connectFunctionsEmulator(functions, "127.0.0.1", 5001);
}
}
// Initialize Firebase
try {
// Check if an app already exists
app = firebase.app();
console.log("[firebase/config-client] App already exists");
} catch (error: any) {
// If the app does not exist, initialize it
if (error.code === "app-compat/no-app") {
console.log("[firebase/config-client] No App exists");
app = firebase.initializeApp(firebaseClientConfig);
} else {
console.error("[firebase/config-client] Error initializing Firebase:", error.message);
throw error;
}
}
// Use the app instance for further operations
initFirebaseVars(app);I haven't seen any breaking changes, so I wonder what is suddenly wrong with my code.
A downgrade to v12.4.0 immediately solves this error.
Steps and code to reproduce issue
Update to v12.5.0 and try to init with my code above. It fails immediately in initFirebaseVars at
dbDev = getFirestore(app, "development");
dbDefault = getFirestore(app);
functions = getFunctions(app, firebaseFunctions.region);
...