我已经启动了一个 Firebase 云函数项目,想知道如何在 WebStorm IDE 中运行调试?
我读到我可以使用@google-cloud/functions-emulator 存档我的目标。 因此我安装了它并遵循了这个文档
运行
functions inspect myFunction
后,我得到以下输出。
Warning: You're using Node.js v10.6.0 but the Google Cloud Functions runtime is only available in Node.js 6 and Node.js 8. Therefore, results from running emulated functions may not match production behavior.
Debugger for app listening on port 9229.
我认为调试现在应该可以工作了。在浏览器中打开 myFunction(例如 http://localhost:8010/my-project/us-central1/myFunction/)工作正常。
现在我正在挣扎。要将 IDE 连接到调试器或将调试器连接到 IDE,我需要做什么?我不知道调试是如何工作的。
预期结果:我想在 Chrome 浏览器中打开该函数,然后在 WebStorm IDE 中的断点处暂停。
感谢您提前提供帮助;)
从 firebase-tools v7.11.0 开始,Firebase 模拟器现在支持使用
--inspect-functions
选项附加调试器。这允许您使用 WebStorm 调试本地运行的 firebase 函数,同时使用其余的(大大改进的)模拟器工具。
首先确保您拥有必要的
firebase-tools
:
$ npm install firebase-tools@latest
现在您可以从项目目录启动 Firebase 模拟器中的函数:
$ firebase emulators:start --inspect-functions
输出将显示类似以下内容:
$ firebase emulators:start --inspect-functions
i emulators: Starting emulators: functions, hosting
⚠ functions: You are running the functions emulator in debug mode (port=9229). This means that functions will execute in sequence rather than in parallel.
✔ functions: Using node@10 from host.
注意上面输出中的“port=9229”。这是我们要告诉 WebStorm 附加的端口。
在 WebStorm 中打开您的项目,然后:
从 WebStorm 主菜单中,您现在可以选择 运行 |调试... 并选择您的新配置。 WebStorm 将附加到托管您的函数的进程,您可以使用调试功能(断点等),就像 WebStorm 中的正常调试会话一样。
2020 年更新:
从 firebase-tools v7.11.0 开始,Firebase 模拟器允许使用
--inspect-functions
标志调试托管函数。与使用functions-framework相比,这有很多优点,如下所述。有关详细信息,请参阅此答案。
原答案:
配置 WebStorm 来调试 Firebase 函数可能会令人困惑,因为有几种不同的方法可以在本地运行 Firebase 函数。
这对我有用:
npm install --save-dev @google-cloud/functions-framework
Node.js
调试配置
node_modules/@google-cloud/functions-framework/build/src/index.js
--target=<your-function-name>
(在我的示例中,我还指定了
--port
开关来设置函数将侦听的端口)。
之后,从 WebStorm 菜单运行
Run | Debug | Debug Function
将加载您的函数并命中您设置的任何断点。
将此
npm run
脚本添加到 package.json
:
"dev": "npm run build && firebase emulators:start --only functions --inspect-functions 10001 & tsc --watch"
& tsc --watch
您可以为此创建一个 WebStorm 运行配置:
然后,为 Firebase 功能创建调试配置,告诉 WebStorm 连接到您之前指定的端口号 (10001):
在终端中运行
npm run dev
,或从 WebStorm 启动您之前创建的 npm dev
配置。
您每次都需要使用 WebStorm 启动它或在下一步之前手动启动它,除非您使用复合运行配置(请参阅上一节)
通过从下拉列表中运行调试 Firebase 功能配置,将 WebStorm 的调试器连接到您在上一步中启动的 Firebase 模拟器:
要测试调试器,请对 Firebase 函数进行
curl
调用。
您可以创建一个
Compound
运行配置,它会启动您可能需要的所有内容。在这种情况下,您将拥有 npm dev
命令和 Debug Firebase functions
命令: