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

Express应用与Firebase Functions混合部署报错,求解决方法

问题描述

我采用如下文件目录结构开发Firebase Functions与Express混合应用:

.
└── src/
    ├── index.ts
    ├── auth/
    │   └── auth.ts
    ├── controllers/
    │   ├── user.ts
    │   └── event.ts
    ├── functions/
    │   └── email.ts
    └── models/
        ├── user-model.ts
        ├── event-model.ts
        └── email-model.ts

index.ts代码如下:

import * as admin from "firebase-admin";
admin.initializeApp();
admin.firestore().settings({ ignoreUndefinedProperties: true });

module.exports = {
  ...require("./controllers/user"),
  ...require("./controllers/event"),
  ...require("./functions/email"),
};

event.ts和user.ts遵循以下结构:

const eventApp: express.Application = express();
eventApp.use(bodyParser.json());
eventApp.use(cors({ origin: true }));
eventApp.get("/",
    isAuthenticated,
    isAuthorized({ hasRole: ["admin"] }),
    async (req: express.Request, res: express.Response) => { ... });

module.exports.events = functions.https.onRequest(eventApp);

email.ts结构:

module.exports.sendInitialInvitation = functions.firestore
.document('{collection}/{docId}')
.onCreate((snap: QueryDocumentSnapshot, context: any) => { ... });

部署时持续出现错误:

Error: Failed to load function definition from source: Failed to generate manifest from function source: TypeError: Cannot read properties of undefined (reading 'document')

删除functions文件夹及index.ts中对应引用后,部署可正常完成。请问该错误的原因是什么?如何解决?


错误原因

email.ts中未导入Firebase Functions模块,导致functions变量未定义,调用functions.firestore.document()时触发Cannot read properties of undefined (reading 'document')错误。同理,user.ts和event.ts中如果也没导入functions模块,其实也存在潜在问题,只是之前删除email.ts后没暴露出来。

解决步骤
  1. 补全模块导入
    在email.ts顶部添加Firebase Functions的导入语句:

    import * as functions from "firebase-functions";
    

    同时在user.ts和event.ts中补全所有必要的导入:

    import * as functions from "firebase-functions";
    import * as express from "express";
    import * as bodyParser from "body-parser";
    import * as cors from "cors";
    // 根据实际情况导入auth中间件(isAuthenticated、isAuthorized)
    
  2. 优化导出逻辑(TypeScript推荐写法)
    原index.ts使用CommonJS的require混合ES模块导入,容易引发模块加载冲突。建议统一使用ES模块导出:

    import * as admin from "firebase-admin";
    import * as userControllers from "./controllers/user";
    import * as eventControllers from "./controllers/event";
    import * as emailFunctions from "./functions/email";
    
    admin.initializeApp();
    admin.firestore().settings({ ignoreUndefinedProperties: true });
    
    // 导出所有控制器和函数
    export const events = eventControllers.events;
    export const users = userControllers.users; // 假设user.ts导出的函数名为users
    export const sendInitialInvitation = emailFunctions.sendInitialInvitation;
    
  3. 验证编译配置
    检查tsconfig.json,确保编译目标为CommonJS(Firebase Functions默认支持),关键配置如下:

    {
      "compilerOptions": {
        "module": "commonjs",
        "target": "ES2017",
        "outDir": "./lib",
        "rootDir": "./src"
      }
    }
    

    确保部署时使用编译后的lib目录文件,而非原始src文件。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 17:45:00