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
现在可以调试(放置断点)在 VSCode 上本地运行的 Firebase 函数。
npm i -g firebase-toolslaunch.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)
firebase emulators:start --inspect-functionsattach选项运行 vscode 调试器。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 可调用函数。虽然那里没有提到附加调试器的步骤。
打开 Google Cloud Console 的服务帐户窗格。
确保选择了 App Engine 默认服务帐户,并使用右侧的选项菜单选择创建密钥。
出现提示时,为密钥类型选择 JSON,然后单击创建。
将您的 Google 默认凭据设置为指向下载的密钥
$ 设置 GOOGLE_APPLICATION_CREDENTIALS=path\to\key.json
$ firebase 函数:shell
Cloud Functions shell 使用交互式 shell 模拟所有类型的函数触发器,以使用测试数据调用函数。选项因函数类型而异,但基本使用格式为:
myFunctionName(data, options)
Run Code Online (Sandbox Code Playgroud)
您必须先定义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函数:
安装云功能模拟器:
npm install -g @google-cloud/functions-emulator
Run Code Online (Sandbox Code Playgroud)启动模拟器:
functions start
Run Code Online (Sandbox Code Playgroud)部署您的功能:
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)要使用标准的Node.js调试器类型进行调试:
functions debug helloWorldFunction
Run Code Online (Sandbox Code Playgroud)
你会得到:
Debugger for helloWorldFunction listening on port 5858.
Run Code Online (Sandbox Code Playgroud)现在将以下行添加到您的launch.json VS代码中
{
"version": "0.2.0",
"configurations": [
{
"name": "Node.JS (local)",
"type": "node",
"request": "attach",
"port": 5858
}
]
}
Run Code Online (Sandbox Code Playgroud)开始在VS Code中进行调试,并通过调用第3步中的URL触发函数。
您也可以通过functions call helloWorldFunction在终端中键入来触发功能。
有关更多详细信息,请参阅此处的“ Cloud Functions Local Emulator”中的说明。
| 归档时间: |
|
| 查看次数: |
4392 次 |
| 最近记录: |