【问题标题】:Functions debugging in VS CodeVS Code 中的函数调试
【发布时间】:2018-02-05 18:47:59
【问题描述】:

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

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

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

我尝试了https://code.visualstudio.com/docs/nodejs/nodejs-debugging 的一些配置,但没有成功。

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

【问题讨论】:

标签: debugging firebase visual-studio-code google-cloud-functions


【解决方案1】:

更新:2021 年 5 月

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

  1. 将 firebase-tools 更新到 至少 v7.11.0 npm 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>/**"]
        }
      ]
    }
  1. 运行模拟器:firebase emulators:start --inspect-functions
  2. 使用 attach 选项运行 vscode 调试器。
  3. 在云函数中放置断点。
  4. 调用该函数(类似于http://localhost:5001/your_project/us-central1/helloWorld)。断点应该命中。

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

注意:如果您在本地 Firebase 托管设置中测试功能,那么您需要将托管功能指向本地服务器而不是云服务器。看这里https://stackoverflow.com/a/59381328/3073272


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

有一个使用 shell 的 Testing functions interactively 的 firebase 文档。您还可以测试 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)

【讨论】:

  • 目前最好的解决方案
  • 我花了一整天的时间研究解决方案,这当然是最好的答案
  • 为什么 Google 在其官方文档中没有这个简单的答案。我花了几个小时解决这个问题。
  • 这是唯一对我有用的解决方案。我不知道为什么网上有这么多关于这个主题的相互矛盾和不正确的教程,但截至 2021 年 12 月,这似乎是正确的。
  • 我尝试了很多,但它对我不起作用,然后我意识到启动模拟器命令有额外的参数“--inspect-functions”,添加后它开始像魅力一样工作。跨度>
【解决方案2】:

如果不先定义 Firebase 配置变量,您将无法调试 Firebase 函数。 Firebase CLI 为您完成。

要进行调试,您可以尝试与 Firebase 函数单元测试相同的技巧。

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

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

您现在可以像调试任何其他谷歌云功能一样调试 Firebase 功能:

  1. 安装 Cloud Functions 模拟器:

    npm install -g @google-cloud/functions-emulator
    
  2. 启动模拟器:

    functions start
    
  3. 部署你的函数:

    functions deploy helloWorldFunction --trigger-http
    

    你会得到这样的输出:

    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
    
  4. 使用标准 Node.js 调试器类型进行调试:

    functions debug helloWorldFunction
    

    你会得到:

    Debugger for helloWorldFunction listening on port 5858.
    
  5. 现在将以下行添加到您的 launch.json VS 代码中

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Node.JS (local)",
                "type": "node",
                "request": "attach",
                "port": 5858
            }
        ]
    }
    
  6. 在您的 VS 代码中开始调试,并通过调用您在第 3 步中获得的 URL 来触发您的函数。

    您也可以在终端输入functions call helloWorldFunction来触发该功能。

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

【讨论】:

  • 我在尝试部署时遇到错误。我在谷歌上搜索,没有任何帮助,我放弃了。
  • 你好安德鲁!您的回答很棒,但是我必须进行一些更正才能使其正常工作。首先,应该删除“admin.initializeApp = function () {}”这行;它实际上使对 admin.initializeApp 的下一次调用毫无用处。其次,请澄清 not-a-project.firebaseio.com 应包含您的 firebase 应用程序的正确地址;与非项目名称有点模棱两可。最后,在functions.config 的firebase 对象中,我必须设置'credential: admin.credential.applicationDefault()'。通过这些修改,我能够在 VSCode 上运行模拟器和调试
  • 我有点惊讶你建议在已经安装了 firebase 函数模拟器的情况下安装和使用 GCP 函数模拟器。我假设它们几乎是一样的,但是在使用 FB 函数时使用 FB 函数工具/模拟器更有意义。
  • 使用 GCP 函数模拟器的原因是无法使用firebase serve调试 firebase 函数(即逐步执行代码等)我会很高兴弄错了。
  • https.on调用类型的函数用这个方法可以吗?
【解决方案3】:
  • npm install -g @google-cloud/functions-emulator

  • /functions/index.js

const admin = require('firebase-admin');

if (!admin.apps.length)
    admin.initializeApp({
    apiKey: "... your api key",
    authDomain: "... your auth domain",
    databaseURL: "... your database url",
    projectId: "... your project id",
    storageBucket: "... your storage bucket",
    messagingSenderId: "... your messaging sender id"
});
  • /.vscode/launch.json
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "attach",
            "name": "Attach Firebase",
            "port": 9229,
            "preLaunchTask": "Google Cloud Emulator"
        }
    ]
}
  • /.vscode/tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
    {
        "label": "Start",
        "type": "shell",
        "command": "functions",
        "isBackground": true,
        "args": [
            "start"
        ],
        "group": "build"
    },
    {
        "label": "Deploy",
        "type": "shell",
        "command": "functions",
        "isBackground": true,
        "options": {
            "cwd": "${workspaceFolder}/functions/"
        },
        "args": [
            "deploy",
            "--trigger-http",
            "--timeout",
            "600s",
            "api"
        ],
        "dependsOn": [
            "Start"
        ],
        "group": "build"
    },
    {
        "label": "Inspect",
        "type": "shell",
        "command": "functions",
        "isBackground": true,
        "options": {
            "cwd": "${workspaceFolder}/functions/"
        },
        "args": [
            "inspect",
            "api"
        ],
        "dependsOn": [
            "Deploy"
        ],
        "group": "build"
    },
    {
        "label": "Google Cloud Emulator",
        "dependsOn": [
            "Inspect",
        ],
        "group": "build"
    },
]

}

  • 开始调试“附加 Firebase”

【讨论】:

    【解决方案4】:

    找到了。

    1. 添加到launch.json:
    {
        "version": "0.2.0",
        "configurations": [
            {
                "type": "node",
                "request": "attach",
                "name": "Attach Firebase",
                "port": 9229
            }
        ]
    }
    
    1. 运行:
    $ firebase emulators:start --inspect-functions
    

    【讨论】:

      【解决方案5】:

      我刚刚在另一个问题上回答了如何使用 Firebase Functions v1.0 来解决这个问题: Debugging firebase cloud functions

      您可以使用 Firebase 函数 1.0 使其在 Visual Studio Code 上运行,而无需更改函数代码上的任何内容。

      您基本上只需要在运行functions deploy 命令时正确设置 FIREBASE_CONFIG 环境变量即可。 类似的东西(不要忘记转义 " 字符):

      FIREBASE_CONFIG="{\"databaseURL\":\"https://YOUR-FIREBASE-PROJECT.firebaseio.com\",\"storageBucket\":\"YOUR-FIREBASE-PROJECT.appspot.com\",\"projectId\":\"YOUR-FIREBASE-PROJECT\"}
      functions deploy --trigger-http --timeout 600s FUNCTION_NAME
      

      之后,您只需运行 functions debug FUNCTION_NAME 即可启动该函数并附加您的 vs Code 调试器。

      【讨论】:

        【解决方案6】:

        这是我的解决方案:

        我在 {root}/src/koios/index.js 中有一个 http Google Cloud Function

        {root}/src/koios/package.json

        "scripts": {
            "start": "env URL=xxx.xxx.com node --inspect-brk=9229 node_modules/@google-cloud/functions-framework --target=queryElasticsearch --port=8888"
          }
        

        {root}/.vscode/launch.json

        {
                    "type": "node",
                    "request": "launch",
                    "name": "Launch koios via npm",
                    "cwd": "${workspaceFolder}/src/koios",
                    "runtimeExecutable": "npm",
                    "runtimeArgs": ["run-script", "start"],
                    "port": 9229
                }
        

        在 vscode 中开始调试,并通过 postman 发布来触发它,您可以在 vscode 中点击 Cloud Function 代码中的断点。

        享受编码!

        【讨论】:

          猜你喜欢
          • 2020-12-30
          • 2021-06-06
          • 1970-01-01
          • 2019-02-09
          • 2020-08-25
          • 1970-01-01
          • 2017-06-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多