VS Code中的函数调试

Ali*_*n C 16 debugging firebase visual-studio-code google-cloud-functions

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

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Program",
      "program": "${file}"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

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

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

如何在VS Code中调试Firebase功能?

Jer*_*yal 32

更新:2021 年 5 月

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

  1. 将firebase -tools 更新至至少v7.11.0npm i -g firebase-tools
  2. 在下面添加到launch.json
    {
      "version": "0.2.0",
      "configurations": [
        {
          "type": "node",
          "request": "attach",
          "name": "Attach",
          "port": 9229,
          "restart": true,
          "skipFiles": ["<node_internals>/**"]
        }
      ]
    }
Run Code Online (Sandbox Code Playgroud)
  1. 运行模拟器: firebase emulators:start --inspect-functions
  2. 使用attach选项运行 vscode 调试器。
  3. 将断点放在云函数中。
  4. 调用该函数(类似于http://localhost:5001/your_project/us-central1/helloWorld)。断点应该命中。

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

注意:如果您正在测试来自本地 firebase 托管设置的功能,那么您需要将您的托管功能指向本地服务器而不是云服务器。看这里/sf/answers/4156692991/


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

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

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

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

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

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

    $ 设置 GOOGLE_APPLICATION_CREDENTIALS=path\to\key.json

    $ firebase 函数:shell

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

myFunctionName(data, options)
Run Code Online (Sandbox Code Playgroud)

  • 目前最好的解决方案 (4认同)
  • 为什么谷歌在其官方文档中没有这个简单的答案。我花了几个小时来解决这个问题。 (3认同)
  • 我花了一整天的时间研究解决方案,这当然是最好的答案 (2认同)

And*_*sov 7

您必须先定义Firebase配置变量才能调试Firebase函数。Firebase CLI会为您完成此任务。

要进行调试,您可以尝试使用与Firebase功能单元测试相同的技巧。

在调用之前,将以下几行添加到index.js文件admin.initializeApp(functions.config().firebase)

admin.initializeApp = function () {}
functions.config = function() {
    return {
        firebase: {
          databaseURL: 'https://not-a-project.firebaseio.com',
          storageBucket: 'not-a-project.appspot.com',
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

您现在可以像使用其他任何Google Cloud函数一样调试Firebase函数:

  1. 安装云功能模拟器:

    npm install -g @google-cloud/functions-emulator
    
    Run Code Online (Sandbox Code Playgroud)
  2. 启动模拟器:

    functions start
    
    Run Code Online (Sandbox Code Playgroud)
  3. 部署您的功能:

    functions deploy helloWorldFunction --trigger-http
    
    Run Code Online (Sandbox Code Playgroud)

    您将获得如下输出:

    Waiting for operation to finish...done.
    Deploying function........done.
    Function helloWorldFunction deployed.
    
    Property | Value
    ---------|------------------------------------------------------------------------
    Name     | helloWorldFunction
    Trigger  | HTTP
    Resource | http://localhost:8010/helloWorldProject/us-central1/helloWorldFunction
    
    Run Code Online (Sandbox Code Playgroud)
  4. 要使用标准的Node.js调试器类型进行调试:

    functions debug helloWorldFunction
    
    Run Code Online (Sandbox Code Playgroud)

    你会得到:

    Debugger for helloWorldFunction listening on port 5858.
    
    Run Code Online (Sandbox Code Playgroud)
  5. 现在将以下行添加到您的launch.json VS代码中

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Node.JS (local)",
                "type": "node",
                "request": "attach",
                "port": 5858
            }
        ]
    }
    
    Run Code Online (Sandbox Code Playgroud)
  6. 开始在VS Code中进行调试,并通过调用第3步中的URL触发函数。

    您也可以通过functions call helloWorldFunction在终端中键入来触发功能。

有关更多详细信息,请参阅此处的“ Cloud Functions Local Emulator”中的说明

  • 使用GCP功能仿真器的原因是,没有办法使用“ firebase serve”(即,逐步执行代码等)来***一个firebase函数(我很乐意弄错了)。 (4认同)