Next.js Discord

Discord Forum

Firebase Admin Initialization Issue in Next.js 14.0.3 API Route Handler Only in Production

Unanswered
Brown bear posted this in #help-forum
Open in Discord
Brown bearOP
Hello Next.js community,

I'm facing a perplexing issue with firebase-admin in a Next.js API route handler that only occurs in the production environment. My setup functions correctly in development, but the problem arises when I deploy my project.

Here's a summary of the issue:

- Environment: The issue only occurs in production. In development, Firebase Admin initializes correctly.
- Code Snippet:
  import { initAdmin } from '_/lib/firebaseAdmin';

  export async function POST(request: NextRequest, response: NextResponse) {
    await initAdmin();
    // other logic
    return NextResponse.json({}, {status: 200});
  }
  
j
- Behavior: Despite initializing Firebase Admin at the start of my function, I get an error in production stating that the Firebase app is not initialized. However, logging firebase-admin's apps.length shows that there is an instance.
- Error Message: "FirebaseAppError: The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services."
- Attempted Solutions: I've tried initializing with a name [DEFAULT], but it didn't resolve the issue.
- Environment Variables: I've also verified that all my environment variables, including Firebase credentials, are correctly set up in production.
I'm puzzled why this works in development but fails in production. Has anyone experienced a similar issue or can offer insights into what might be going wrong? Any tips on properly initializing firebase-admin in a Next.js API route handler for production use would be greatly appreciated.

Thank you in advance for your help!

5 Replies

Brown bearOP
up
can you show the initAdmin function?
maybe initializing firebase in a separate folder helps, similar to an stripe client initialization
Brown bearOP
Hi, thank for your help !

import "server-only";
import admin from "firebase-admin";

interface FirebaseAdminAppParams {
projectId: string;
clientEmail: string;
storageBucket: string;
privateKey: string;
}

function formatPrivateKey(key: string) {
return key.replace(/\n/g, "\n");
}

export function createFirebaseAdminApp(params: FirebaseAdminAppParams) {
const privateKey = formatPrivateKey(params.privateKey);

if (admin.apps.length > 0) {
return admin.app();
}

const cert = admin.credential.cert({
projectId: params.projectId,
clientEmail: params.clientEmail,
privateKey,
});

return admin.initializeApp({
credential: cert,
projectId: params.projectId,
storageBucket: params.storageBucket,
});
}

export async function initAdmin() {
const params = {
projectId: process.env.NEXT_PUBLIC_PROJECT_ID as string,
clientEmail: process.env.FB_CLIENT_EMAIL as string,
storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET as string,
privateKey: process.env.FB_PRIVATE_KEY as string,
};

return createFirebaseAdminApp(params);
}
maybe you are importing initializeApp incorrectly from the root segment because the docs say it should be imported from firebase-admin/app