Hi Firebase Studio / IDX Community & Support,
I’m encountering a persistent issue when trying to use the Firebase Admin SDK (specifically Firestore) within my Firebase Studio (IDX) environment. The core problem is that any Firestore instance obtained via the Admin SDK has its crucial .app
property as undefined
. This renders the Firestore service unusable.
This issue has been reproduced with multiple Firebase Admin SDK versions and, critically, occurs even in a minimal, standalone Node.js script run directly in the Firebase Studio/IDX terminal, isolating it from my Next.js application’s complexities. The same failure pattern was also observed when deploying my Next.js app (developed in IDX) to Google Cloud Run via the Firebase App Hosting “rollout feature.”
Environment Details:
- Primary Development Environment: Firebase Studio / Google Cloud Workstations (IDX)
- Node.js Version in IDX:
v20.19.0
(confirmed withnode -v
) - Firebase Admin SDK Versions Tested (in IDX):
12.1.1
(latest stable) and^13.4.0
(initial version encountered). - Original Project Context: Next.js
15.2.3
(App Router). - Admin SDK Authentication: Using a service account key (JSON, Base64 encoded in
FIREBASE_ADMIN_SDK_CONFIG_BASE64
env var loaded viadotenv
in standalone script, and via Next.js env loading in the app).
Problem Description:
When initializing the Firebase Admin SDK:
admin.initializeApp(...)
appears to successfully return aFirebaseApp
object. Detailed logging shows itsconstructor.name
isFirebaseApp
, and it has the correct app name andoptions.projectId
. Its keys include expected service accessors like.firestore()
.- When a Firestore instance is obtained (either via
app.firestore()
or the modulargetFirestore(app)
), the returned object appears to be aFirestore
instance (correctconstructor.name
, typical internal keys). - However, the
firestoreInstance.app
property is consistentlyundefined
.
This failure prevents validation and use of the Firestore service, leading to adminDb
(our module variable for the Firestore client) being null
and subsequent operations failing.
Key Finding: The issue is starkly reproduced with both firebase-admin@12.1.1
and firebase-admin@^13.4.0
in the following standalone Node.js script executed directly in the Firebase Studio/IDX terminal:
Standalone Test Script (testAdminSdk.js
):
`JavaScript// testAdminSdk.js
require(‘dotenv’).config(); // Loads .env file from project root
const admin = require(‘firebase-admin’);
const { getFirestore: getModularFirestore } = require(‘firebase-admin/firestore’);
const serviceAccountKeyBase64 = process.env.FIREBASE_ADMIN_SDK_CONFIG_BASE64;
if (!serviceAccountKeyBase64) {
console.error(‘TEST-SCRIPT: FIREBASE_ADMIN_SDK_CONFIG_BASE64 is not set in .env file.’);
process.exit(1);
}
let app;
async function runTest() {
try {
const serviceAccountString = Buffer.from(serviceAccountKeyBase64, ‘base64’).toString(‘utf-8’);
const serviceAccount = JSON.parse(serviceAccountString);
const appName = ‘STANDALONE_IDX_TEST_APP_’ + Date.now();
console.log(`TEST-SCRIPT: Initializing Firebase app [${appName}] with projectId "${serviceAccount.project_id}"...`);
app = admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
projectId: serviceAccount.project_id,
databaseURL: `https://${serviceAccount.project_id}.firebaseio.com` // Included for completeness
}, appName);
console.log(`TEST-SCRIPT: App [${appName}] initialized. Project ID from options: ${app.options.projectId}`);
console.log(`TEST-SCRIPT: App constructor: ${app.constructor?.name}`);
console.log('\nTEST-SCRIPT: Attempt 1: Using app.firestore()');
const db1 = app.firestore();
console.log(`TEST-SCRIPT: db1 type: ${typeof db1}, constructor: ${db1?.constructor?.name}`);
console.log(`TEST-SCRIPT: db1.app is defined: ${!!db1?.app}`); // Key check
console.log(`TEST-SCRIPT: db1.app.name (if defined): ${db1?.app?.name}`);
if (!db1?.app) console.error("TEST-SCRIPT: ERROR: db1.app is undefined!");
console.log('\nTEST-SCRIPT: Attempt 2: Using getModularFirestore(app)');
const db2 = getModularFirestore(app);
console.log(`TEST-SCRIPT: db2 type: ${typeof db2}, constructor: ${db2?.constructor?.name}`);
console.log(`TEST-SCRIPT: db2.app is defined: ${!!db2?.app}`); // Key check
console.log(`TEST-SCRIPT: db2.app.name (if defined): ${db2?.app?.name}`);
if (!db2?.app) console.error("TEST-SCRIPT: ERROR: db2.app is undefined!");
} catch (error) {
console.error(‘TEST-SCRIPT: An error occurred:’, error);
} finally {
if (app) {
console.log(\nTEST-SCRIPT: Deleting app [${app.name}]...
);
await app.delete();
console.log(‘TEST-SCRIPT: App deleted successfully.’);
}
}
}
runTest();`
Log Output from testAdminSdk.js
in Firebase Studio/IDX (with firebase-admin@12.1.1
):
`TEST-SCRIPT: Initializing Firebase app [STANDALONE_IDX_TEST_APP_…]…
TEST-SCRIPT: App [STANDALONE_IDX_TEST_APP_…] initialized. Project ID: tripforge-cr566
TEST-SCRIPT: App constructor: FirebaseApp
TEST-SCRIPT: Attempt 1: Using app.firestore()
TEST-SCRIPT: db1 type: object, constructor: Firestore
TEST-SCRIPT: db1.app is defined: false
TEST-SCRIPT: db1.app.name (if defined): undefined
TEST-SCRIPT: ERROR: db1.app is undefined!
TEST-SCRIPT: Attempt 2: Using getModularFirestore(app)
TEST-SCRIPT: db2 type: object, constructor: Firestore
TEST-SCRIPT: db2.app is defined: false
TEST-SCRIPT: db2.app.name (if defined): undefined
TEST-SCRIPT: ERROR: db2.app is undefined!
…`
Summary of My firebaseAdmin.ts
for Next.js (where issue was first seen): The module attempts a singleton initialization for a named Firebase Admin app. It loads the service account from the Base64 environment variable, calls admin.initializeApp
with the credential, explicit projectId
, and databaseURL
. It then uses getAuth(app)
and attempts app.firestore()
(previously getFirestore(app)
). Extensive logging in this file confirms the FirebaseApp
instance looks valid, but the Firestore instance obtained has firestoreInstance.app === undefined
. This failure occurs during next build
on Cloud Run (Node.js v20.19.2
) and during next dev
in IDX.
Questions for the Firebase Studio/IDX Team & Community:
- Is this behavior (Firebase Admin SDK’s Firestore service instance having an
undefined
.app
property) a known issue or limitation within the Firebase Studio / IDX Node.js (v20.19.0
) environment? - Are there any specific configurations, environment variables, or nuances related to the IDX Node.js runtime that might interfere with the internal mechanisms of the Firebase Admin SDK (versions
12.1.1
or^13.4.0
) responsible for linking service instances to their parent app object? - What could cause a seemingly valid
FirebaseApp
object to yield Firestore instances (via eitherapp.firestore()
orgetFirestore(app)
) that are not correctly associated with it? - Are there any recommended diagnostic steps, tools, or
NODE_DEBUG
flags specific to the IDX environment that could help trace this internal SDK linkage failure more effectively? - What is the recommended approach for ensuring stable and correct Firebase Admin SDK (specifically for Firestore) initialization and usage within Firebase Studio/IDX and in apps deployed from IDX to Firebase App Hosting/Cloud Run?
Any insights or guidance you can provide would be greatly appreciated, as this is a fundamental blocker for our server-side Firebase integration. We are happy to provide more detailed logs or run further diagnostic tests as advised.
Thank you for your help!