本文介绍了VS Code 中的函数调试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VS Code 集成终端我运行 firebase serve --only 功能,托管然后在调试选项卡中我创建了默认的launch.json:

From VS Code integrated terminal I run firebase serve --only functions,hostingthen in the debug tab I created the default launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Program",
      "program": "${file}"
    }
  ]
}

我想调试服务器端(functions/index.js)而不是客户端.

I want to debug the server side (functions/index.js) not the client side.

我从 https://code.visualstudio.com/docs 尝试了一些配置/nodejs/nodejs-debugging 没有运气.

如何在 VS Code 中调试 Firebase 函数?

推荐答案

更新:2021 年 5 月

现在可以调试(放置断点)在 VSCode 上本地运行的 Firebase 函数.

Update: May 2021

It's now possible to debug (put breakpoints) firebase functions running locally on VSCode.

  1. 将 firebase-tools 更新到 至少 v7.11.0 npm i -g firebase-tools
  2. 将以下内容添加到 launch.json:
  1. update firebase-tools to atleast v7.11.0 npm i -g firebase-tools
  2. add below to launch.json:
    {
      "version": "0.2.0",
      "configurations": [
        {
          "type": "node",
          "request": "attach",
          "name": "Attach",
          "port": 9229,
          "restart": true,
          "skipFiles": ["<node_internals>/**"]
        }
      ]
    }
  1. 运行模拟器:firebase emulators:start --inspect-functions
  2. 使用 attach 选项运行 vscode 调试器.
  3. 在云函数中放置断点.
  4. 调用该函数(类似于http://localhost:5001/your_project/us-central1/helloWorld).断点应该命中.
  1. run the emulator: firebase emulators:start --inspect-functions
  2. run the vscode debugger with attach option.
  3. put breakpoint in a cloud function.
  4. call that function (something like http://localhost:5001/your_project/us-central1/helloWorld). Breakpoint should hit.

注意:尚未实现对 PubSub/调度函数的支持.支持这个问题:https://github.com/firebase/firebase-tools/issues/2034

note: Support for PubSub/scheduled functions is still not implemented. Upvote this issue: https://github.com/firebase/firebase-tools/issues/2034

注意:如果您正在测试来自本地 Firebase 托管设置的功能,那么您需要将您的托管功能指向本地服务器而不是云服务器.请参阅此处https://stackoverflow.com/a/59381328/3073272

note: if you're testing functions from local firebase hosting setup, then you need to point your hosting functions to local server instead of cloud server. see here https://stackoverflow.com/a/59381328/3073272

关于使用终端调试(无断点)firebase 函数的旧答案:

有一个 Firebase 文档,用于使用 shell 以交互方式测试函数.您还可以测试 https 可调用函数.虽然那里没有提到附加调试器的步骤.

There's a firebase documentation for Testing functions interactively using shell. You can also test https callable functions. Though steps for attaching a debugger is not mentioned there.

  1. 打开 Google Cloud Console 的服务帐户窗格.

  1. Open the Service Accounts pane of the Google Cloud Console.

确保选择了 App Engine 默认服务帐户,然后使用右侧的选项菜单选择创建密钥.

Make sure that App Engine default service account is selected, and use the options menu at right to select Create key.

出现提示时,为密钥类型选择 JSON,然后单击创建.

When prompted, select JSON for the key type, and click Create.

将您的 Google 默认凭据设置为指向下载的密钥

Set your Google default credentials to point to the downloaded key

$ set GOOGLE_APPLICATION_CREDENTIALS=pathokey.json

$ set GOOGLE_APPLICATION_CREDENTIALS=pathokey.json

$ firebase 函数:shell

$ firebase functions:shell

Cloud Functions shell 使用交互式 shell 模拟所有类型的函数触发器,以使用测试数据调用函数.选项因函数类型而异,但基本使用格式为:

The Cloud Functions shell emulates all types of function triggers with an interactive shell for invoking the functions with test data. Options vary by function type, but the basic usage format is:

myFunctionName(data, options)

这篇关于VS Code 中的函数调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 17:15