【问题标题】:firebase show error when i try to run functions locally当我尝试在本地运行函数时,firebase 显示错误
【发布时间】:2019-10-21 01:19:36
【问题描述】:

每当我在 CLI 中使用模拟器在本地运行函数时都会显示 Firebase 错误

$ firebase emulators:start --only functions

启动模拟器:["functions"]

功能:从主机使用 node@8。

功能:模拟器开始于http://localhost:5001

functions:正在查看 Cloud Functions 的“E:\dir\functions”...

错误:找不到模块“E:\dir\functions”

在 Function.Module._resolveFilename (module.js:548:15)

在 Function.Module._load (module.js:475:25)

在 Module.require (module.js:597:17)

在需要时(内部/module.js:11:18)

在 C:\Users\d\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:459:29

在Generator.next()

在 C:\Users\d\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:7:71

在新的 Promise() 处

在 __awaiter (C:\Users\d\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:3:12)

在 main (C:\Users\d\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:421:12)

您的函数已被终止,因为它引发了未处理的错误。

我使用 typescript 编写云函数。

这是我的 index.ts

import * as functions from 'firebase-functions';
import * as admin from "firebase-admin";

var cert = require("./skey.json");

admin.initializeApp({
    credential: admin.credential.cert(cert),
    databaseURL: "https://bhau-tk.firebaseio.com"
});

exports.basicHTTP = functions.https.onRequest((req, res) => {
    res.send("Hello world!!");
})

package.json 包含

{
  "name": "functions",
  "scripts": {
    "lint": "tslint --project tsconfig.json",
    "build": "tsc",
    "serve": "npm run build && firebase serve --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "8"
  },
  "main": "lib/index.js",
  "dependencies": {
    "firebase-admin": "~7.0.0",
    "firebase-functions": "^2.3.0",
    "firebase-functions-test": "^0.1.6"
  },
  "devDependencies": {
    "tslint": "^5.12.0",
    "typescript": "^3.2.2"
  },
  "private": true
}

项目结构

【问题讨论】:

  • 你的项目布局是什么? skey.json 在哪里,你指的对吗?
  • 在src文件夹里面!
  • 您能否编辑问题以说明您的项目布局?

标签: node.js typescript firebase google-cloud-functions firebase-cli


【解决方案1】:

您应该记住,您的项目配置是在 src 下编译 TS 源文件,并将生成的 JavaScript 文件放入 lib。因此,您应该参考相对于该位置的函数文件夹中的内容。

由于您编译的 index.js 在 lib 中,并且您的 skey.json 文件在 src 下,因此您必须这样引用它:

var cert = require("../src/skey.json");

但是,我不会那样做。我将 skey.json 放在函数文件夹中(因为它不是源代码),并像这样引用它:

var cert = require("../skey.json");

【讨论】:

  • 谢谢!为了增加我的知识?
  • 是否有任何记录?我的失败并显示“您的函数已被杀死”消息,但没有堆栈跟踪等。
猜你喜欢
  • 2020-04-20
  • 1970-01-01
  • 2021-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-21
相关资源
最近更新 更多