Debugging Firebase functions locally (node.js)

时光怂恿深爱的人放手 提交于 2021-02-19 05:34:20

问题


see below my code is very simple....it listens to a change on a database field in realtime-database.

const functions = require("firebase-functions");
var admin = require("firebase-admin");

exports.onActiveUpdate = functions.database
  .ref("/profiles/users/{userId}/active")
  .onUpdate((change, context) => {
      //code here

    return true;
  });

Ive tried to debug code locally with the following commands

firebase serve --only functions
firebase serve
firebase emulators:start

I always get the same message...

+  functions: Emulator started at http://localhost:5001
i  functions: Watching "C:\code\rn\xs\fb_functions\functions" for Cloud Functions...
i  functions[onActiveUpdate]: function ignored because the database emulator does not exist or is not running.
+  All emulators started, it is now safe to connect.

But then when I go to localhost:5001....all I see is a blank page with with {"status":"alive"} at the top.

Is this correct behavior? Thanks


回答1:


You can use https://github.com/GoogleChromeLabs/ndb to debug your http firebase functions.

Install it locally or globally and run your normal serve command with ndb:

ndb yarn serve




回答2:


According to the documentation, the command line for starting both the emulators for Cloud Functions and Realtime Database is this:

firebase emulators:start --only database,functions



回答3:


I just tried firebase emulator suite and found it does enable node.js Inspector and hence support step-by-step local debug nicely.

Assuming all your modules are good for emulator so we can focus on enabling local debug.

I choose MS Code as my Inspector Client.

  • Execute firebase emulators:start and verify your function is working fine on local.
  • Go to MS Code Run/Debug screen, select "add a configuration..." and select "{}Node.js: Attach to process" to create a new .vscode/launch.json. You will see the debug configuration with name of "Attach by Process ID" is generated.
  • From MS Code Run/Debug screen, run this new "Attach by Process ID" debugger. You will be asked to choose a node process to attach the debugger.
  • You should see 2 "functionsEmulatorRuntime" processes among all other node.js processes (1 for the functionsEmulator itself and another 1 for your function code). Try pick each of them and check if the debugger is able to pause on your break point.



回答4:


you can run the emulator in inspect mode by running :

firebase emulators:start --inspect-functions

see what port it is listening for debug client (e.g. port 9229 ). if you are using VSCode have launch.json inside .vscode subdir

{
"configurations": [
    {   "type": "node",
        "request": "attach",
        "name": "Debug",
        "port": 9229
    }
]

} then simply click on Debug icon in your VScode to connect to running process. now put some break point in your functions code and invoke them through browser.



来源:https://stackoverflow.com/questions/58808799/debugging-firebase-functions-locally-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!