首页 > 解决方案 > firebase 函数:TypeError:func 不是函数

问题描述

Angular 项目中带有 expressjs 的 firebase-tools 会导致错误: 在我的本地机器! functions: TypeError: func is not a function 上运行时。firebase serve

快递服务器在没有firebase的情况下运行,没有错误,即:运行时node dist/server.js

回购

我使用所有东西的最新版本(Angular、Firebase、express)

日志:

i  functions: Beginning execution of "ssr"
>  (node:4852) [DEP0005] DeprecationWarning: Buffer() is deprecated due to secur
ity and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), o
r Buffer.from() methods instead.

!  functions: TypeError: func is not a function
    at runFunction (node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:621:20)
    at node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:596:19
    at Generator.next (<anonymous>)
    at node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:7:71
    at new ZoneAwarePromise (dist\server.js:1397:29)
    at __awaiter (node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:3:12)
    at runFunction (node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:593:12)
    at node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:620:15
    at Generator.next (<anonymous>)
    at node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:7:71
    at new ZoneAwarePromise (dist\server.js:1397:29)
    at __awaiter (node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:3:12)
    at runHTTPS (node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:616:12)
    at node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:543:27
    at Generator.next (<anonymous>)
    at node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:7:71

!  Your function was killed because it raised an unhandled error.

服务器.ts:

import "zone.js/dist/zone-node";
import express from "express";


const app = express();
const DIST_FOLDER = join(process.cwd(), "./dist/browser"); 


const {
  AppServerModuleNgFactory,
  LAZY_MODULE_MAP,
  ngExpressEngine,
  provideModuleMap
} = require("./dist/server/main");

app.engine(
  "html",
  ngExpressEngine({
    bootstrap: AppServerModuleNgFactory,
    providers: [provideModuleMap(LAZY_MODULE_MAP)]
  })
);

app.set("view engine", "html");
app.set("views", DIST_FOLDER);

app.use((req, res, next) => {
  if (!req.path) req.url = `/{req.url}`;
  next();
});


app.get("/api/", (req, res) => {
  res.json({ ok:true});
});

// Serve static files from /browser
app.get(
  "*.*",
  express.static(DIST_FOLDER, {
    maxAge: "1y"
  })
);

// All regular routes use the Universal engine
app.get("*", (req, res) => {
  res.render("index", { req });
});

//app.listen(4200)
export { app }; //for firebase

firebase 功能 ssr:

import * as functions from "firebase-functions";
const app = require("../server"); 

export const ssr = functions.https.onRequest(app);

更新const app = require('../server')我通过更改为 修复了此错误const {app} = ...

但现在出现了一个新错误:

i  functions: Beginning execution of "ssr"

!  functions: TypeError: handler is not a function
    at cloudFunction (node_modules\firebase-functions\lib\providers\https.js:49:9)
    at runFunction (node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:621:20)
    at node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:596:19
    at Generator.next (<anonymous>)
    at node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:7:71
    at new ZoneAwarePromise (node_modules\zone.js\dist\zone-node.js:931:29)
    at __awaiter (node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:3:12)
    at runFunction (node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:593:12)
    at node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:620:15
    at Generator.next (<anonymous>)
    at node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:7:71
    at new ZoneAwarePromise (node_modules\zone.js\dist\zone-node.js:931:29)
    at __awaiter (node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:3:12)
    at runHTTPS (node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:616:12)
    at node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:543:27
    at Generator.next (<anonymous>)

!  Your function was killed because it raised an unhandled error.

解决方案 我意识到问题在于 webpack 的导出并没有真正导出模块前节点。

标签: node.jsfirebaseexpressgoogle-cloud-functionsfirebase-tools

解决方案


推荐阅读