node.jstypescriptfirebaseazurefirebase-admin

Exception during Firebase set up in Microsoft Azure Function (Node)


I'm setting up firebase server side on my Azure Function App, using Node 20 and firebase-admin v13.0.1 (Firebase Admin Node.js SDK)

I'm having problem with setting up firebase.

This is the code responsible of fetching the certificate and initialize firebase:

import { BlobServiceClient } from '@azure/storage-blob';
import { initializeApp } from 'firebase-admin';
import { cert } from 'firebase-admin/app';
import {
    FIREBASE_CERT_FILE_NAME,
    FIREBASE_CONTAINER_NAME,
    FIREBASE_STORAGE_CONNECTION_STRING,
} from '../../../costants';
import { streamToString } from '../../../util';

let firebaseCert: string | null = null;
let isAppInit: boolean = false;

export async function loadCert() {
    const blobServiceClient = BlobServiceClient.fromConnectionString(
        FIREBASE_STORAGE_CONNECTION_STRING
    );

    const containerClient = blobServiceClient.getContainerClient(
        FIREBASE_CONTAINER_NAME
    );

    const blobClient = containerClient.getBlobClient(FIREBASE_CERT_FILE_NAME);
    const downloaded = await blobClient.download();
    const cert = await streamToString(downloaded.readableStreamBody);

    return cert;
}

export async function initializeAppSingleton() {
    console.log({ isAppInit });

    try {
        if (!isAppInit) {
            console.log(firebaseCert == null);

            if (firebaseCert == null) {
                firebaseCert = await loadCert();
            }
            initializeApp({
                credential: cert(JSON.parse(firebaseCert)),
            });
            isAppInit = true;
        }
    } catch (e) {
        console.log({ e });
    }
}

When i call the initializeAppSingleton function the certificate is loaded and the i got this exception:

TypeError: Cannot read properties of undefined (reading 'INTERNAL')
          at initializeApp (/home/antonio/Workspace/AME/AZURE_FUNCTIONS/vehicleBridge/node_modules/firebase-admin/lib/app/firebase-namespace.js:246:21)
          at /home/antonio/Workspace/AME/AZURE_FUNCTIONS/vehicleBridge/common/services/external/firebase/firebase.util.ts:40:17
          at Generator.next (<anonymous>)
          at fulfilled (/home/antonio/Workspace/AME/AZURE_FUNCTIONS/vehicleBridge/common/services/external/firebase/firebase.util.ts:5:58)
          at processTicksAndRejections (node:internal/process/task_queues:95:5)

I have checked the node_modules and everything seems allright, I'd like to understand what is preventing the init of the app.

Thanks a lot.

EDIT
Be careful if you like me use VsCode "Add all missing imports": @Doug Stevenson answered with the correct imports, that are different from the ones suggested by VsCode


Solution

  • See the example in the documentation. You need to import initializeApp from firebase-admin/app.

    import { cert, initializeApp } from 'firebase-admin/app';