gpt4 book ai didi

node.js - 当我尝试在本地运行函数时 firebase 显示错误

转载 作者:行者123 更新时间:2023-12-05 06:26:07 24 4
gpt4 key购买 nike

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

$ firebase emulators:start --only functions

Starting emulators: ["functions"]

functions: Using node@8 from host.

functions: Emulator started at http://localhost:5001

functions: Watching "E:\dir\functions" for Cloud Functions...

Error: Cannot find module 'E:\dir\functions'

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

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

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

at require (internal/module.js:11:18)

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

at Generator.next ()

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

at new Promise ()

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

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

Your function was killed because it raised an unhandled error.

我使用 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
}

项目结构

enter image description here

最佳答案

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

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

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

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

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

关于node.js - 当我尝试在本地运行函数时 firebase 显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56462033/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com