Can't test cloud functions locally, emulator fails to start with TypeError: _onRequestWithOpts is not a function

Can't test cloud functions locally, emulator fails to start with TypeError: _onRequestWithOpts is not a function

当我尝试从项目中的函数目录启动 shell 进行本地测试时,模拟器启动但无法加载我的函数。

当我 运行 "npm run build" 时,我没有收到任何错误。发生了什么事?

我尝试删除函数目录并使用 firebase-tools cli 重新创建它,但我仍然遇到相同的错误。我还导出了管理员凭据。

我使用的是在从 firebase-tools cli 生成的 index.ts 文件中创建的默认方法。

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

admin.initializeApp();
// // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript
//
export const helloWorld = functions.https.onRequest((request, 
response) => {
 response.send("Hello from Firebase!");
});

我希望模拟器启动函数的实例进行测试,但我看到了这个错误:

✔  functions: Emulator started at http://localhost:5001
⚠  TypeError: _onRequestWithOpts is not a function
at Object.httpsProvider._onRequestWithOpts (/usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:278:24)
at Object.httpsProvider.onRequest (/usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:283:34)
at Object.<anonymous> (/Volumes/G-DRIVE mobile USB-C/CLEAN UP/Documents/code_bank/typescript/theincrowdapp/functions/lib/index.js:9:38)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:690:17)
at require (internal/modules/cjs/helpers.js:25:18)
⚠  We were unable to load your functions code. (see above)
   - It appears your code is written in Typescript, which must be compiled before emulation.
   - You may be able to run "npm run build" in your functions directory to resolve this.

将 firebasefunction 降级到版本 3.0.0

我发现错误在:

node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js

第 276 行:

const _onRequestWithOpts = httpsProvider._onRequestWithOpts;

应该是:

const _onRequestWithOpts = httpsProvider._onRequestWithOptions;

如果您遇到此错误,请在您的 functions 目录中 运行 npm install firebase-functions@3.0.2

这将很快得到解决,您可以按照此处进行操作: https://github.com/firebase/firebase-tools/issues/1480

删除node_modules然后运行npm install,npm install firebase-adminnpm install firebase-functions@3.0.2

我在一年前遇到过这个错误,昨天又遇到了这个错误,尽管我将 firebase-funcitons 更新到了最新版本。

我还必须使用 npm i -g firebase-tools@latest

更新 firebase-tools

你需要做的最后一件事是关闭你的命令line/open一个新的选项卡以便使用最新版本

总结一下:

  1. npm i firebase-functions@latest

  2. npm i -g firebase-tools@latest

  3. 关闭和打开命令行

我遇到了同样的问题,但通过执行以下操作使其可以使用最新版本的 firebase-* 包。

  1. Firebase -> 设置 -> 服务帐户“生成新私钥”并将文件保存在函数目录下。尝试将其命名为诸如“service-account.json”之类的名称。

  2. 在命令行上(例如Mac)运行 "export GOOGLE_APPLICATION_CREDENTIALS="

  3. 修改tsconfig.json "strict":false, "noImplicitAny": false,"

  4. 瞧!

如果您最近更新了 GOOGLE_APPLICATION_CREDENTIALS 并且没有重新启动 iTerm or Terminal 请退出并重新启动它。我这样做了,效果很好。

您的终端必须在更新后重新启动 GOOGLE_APPLICATION_CREDENTIALS 很简单.. 这可以帮助某个地方的人。