VS Code中的函数调试

问题描述 投票:14回答:3

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
3个回答
4
投票

如果不先定义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',
        }
    };
}

您现在可以使用与任何其他Google云功能相同的方式调试Firebase功能:

  1. 安装云功能模拟器: 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 Code中 { "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中的说明。


1
投票
  • npm install -g @ google-cloud / functions-emulator
  • /functions/index.就是
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"
});
  • /.vs code/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"
        }
    ]
}
  • /.vs code/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”

0
投票

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

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

您基本上只需要在运行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调试器。

© www.soinside.com 2019 - 2024. All rights reserved.