You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Functions(Node.js)中Firebase初始化出现INTERNAL未定义异常

Firebase Admin SDK 在Azure Function App中初始化失败问题

环境:Node 20、firebase-admin v13.0.1、Azure Function App

负责获取证书并初始化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 });
    }
}

调用initializeAppSingleton函数时,证书已成功加载,但抛出如下异常:

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)

已检查node_modules无异常,希望了解初始化失败原因。

编辑提示:如果和我一样使用VSCode的“添加所有缺失导入”功能,需注意:正确的导入方式与VSCode建议的不同。


解决方案

这个错误的核心原因是Firebase Admin SDK的导入方式不正确,VSCode自动导入的路径不符合SDK的模块化要求。

正确的导入方式是从firebase-admin/app模块同时导入initializeApp和cert,替换原来分开的导入语句:

// 正确导入方式
import { initializeApp, cert } from 'firebase-admin/app';

修改导入后重新运行初始化函数即可解决问题。

原因说明:firebase-admin v13.x的模块化结构要求核心初始化方法必须从firebase-admin/app模块导入,直接从firebase-admin根模块导入initializeApp会导致SDK内部依赖未正确加载,从而抛出INTERNAL未定义的错误。

内容的提问来源于stack exchange,提问作者Kaiak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 16:25:06