从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功能?
如果不先定义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功能:
npm install -g @google-cloud/functions-emulator
functions start
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
functions debug helloWorldFunction
你会得到:
Debugger for helloWorldFunction listening on port 5858.
{
"version": "0.2.0",
"configurations": [
{
"name": "Node.JS (local)",
"type": "node",
"request": "attach",
"port": 5858
}
]
}
functions call helloWorldFunction
来触发该功能。有关更多详细信息,请参阅Cloud Functions Local Emulator中的说明。
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"
});
{
// 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"
}
]
}
{
// 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 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调试器。