I needed to pass my serviceAccount.json
to Firebase Admin SDK in a serverless environment via an environment variable.
To solve this, I took the following steps:
Copied the contents of serviceAccount.json
Minified it to a single-line JSON string
Set it as the value of the FIREBASE_ADMIN_CREDENTIALS
environment variable
Then, I updated my code like this:
import { cert, initializeApp, getApps } from 'firebase-admin/app';
import { getAuth } from 'firebase-admin/auth';
const credsString = process.env.FIREBASE_ADMIN_CREDENTIALS!;
const serviceAccount = JSON.parse(credsString);
const adminApp = getApps()[0] || initializeApp({
credential: cert(serviceAccount),
});
export const adminAuth = getAuth(adminApp);
✅ Build Success: This resolved the build-time issue because the cert()
function expects a JavaScript object with camelCase keys like privateKey
, clientEmail
, etc.
⚠️ Runtime Note: It's important that the JSON string in FIREBASE_ADMIN_CREDENTIALS
exactly matches the original serviceAccount.json
format. Firebase internally maps the snake_case keys to the required camelCase format during cert()
.