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

如何为Firebase Functions配置ESM?遇初始化报错求助

问题

服务端应用需使用官方IPFS访问包ipfs-http-client的最新版本(v57.0.0起要求ESM),但本地运行Firebase Functions时持续报错:

⬢ functions: Failed to load function definition from source: FirebaseError: Failed to load function definition from source: Failed to generate manifest from function source: TypeError: admin.initializeApp is not a function

环境信息:

  • macOS 13.3.1
  • Node.js v18.14.0
  • firebase-tools v12.0.0

解决方案

1. 切换项目为ESM模式

修改functions/package.json,添加"type": "module"声明,同时更新依赖和脚本:

{
  "name": "functions",
  "type": "module",
  "scripts": {
    "build": "tsc",
    "serve": "firebase emulators:start --only functions",
    "deploy": "firebase deploy --only functions"
  },
  "dependencies": {
    "firebase-admin": "^11.8.0",
    "firebase-functions": "^4.3.1",
    "ipfs-http-client": "^60.0.1"
  },
  "devDependencies": {
    "@types/node": "^18.15.11",
    "typescript": "^5.0.4",
    "firebase-tools": "^12.0.0"
  }
}

2. 调整TypeScript配置适配ESM

更新functions/tsconfig.json,确保模块系统和解析策略匹配ESM:

{
  "compilerOptions": {
    "module": "ESNext",
    "moduleResolution": "NodeNext",
    "target": "ES2020",
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": false,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", ".firebase", "dist"]
}

3. 修改函数代码的导入逻辑

在ESM环境下,必须使用import语法,且需要从firebase-admin/app解构导出的方法,不能直接导入整个模块:

import { initializeApp } from 'firebase-admin/app';
import { getFirestore } from 'firebase-admin/firestore';
import { onRequest } from 'firebase-functions/v2/https';
import { create } from 'ipfs-http-client';

// 初始化Firebase Admin
const adminApp = initializeApp();
const db = getFirestore(adminApp);

// 初始化IPFS客户端
const ipfsClient = create({ url: 'https://ipfs.infura.io:5001/api/v0' });

// 示例HTTP函数
export const testIpfsIntegration = onRequest(async (req, res) => {
  try {
    const uploadResult = await ipfsClient.add('Firebase + IPFS ESM Test');
    res.status(200).json({ cid: uploadResult.cid.toString() });
  } catch (err) {
    res.status(500).json({ error: (err as Error).message });
  }
});

4. 配置Firebase Functions指向编译后的ESM代码

修改项目根目录的firebase.json,指定函数的运行时为nodejs18,并确保编译产物目录正确:

{
  "functions": {
    "source": "functions",
    "runtime": "nodejs18",
    "ignore": [
      "node_modules",
      ".git",
      "firebase-debug.log",
      "firebase-debug.*.log"
    ]
  }
}

5. 重新构建并测试

执行以下命令完成构建和本地运行:

cd functions
npm run build
firebase emulators:start --only functions

错误原因

这个报错本质是ESM与CommonJS模块系统冲突:ipfs-http-client v57+强制使用ESM,而原项目可能仍沿用CommonJS配置,导致firebase-admin的导入方式错误——在ESM环境下直接require('firebase-admin')会返回一个包含导出成员的模块对象,而非直接暴露initializeApp方法,因此出现initializeApp is not a function的错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 23:33:24