Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging Firebase functions locally (node.js)

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;
  });

I've 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

like image 822
james murphy Avatar asked Nov 06 '25 05:11

james murphy


2 Answers

You can run the emulator in inspect mode by running :

firebase emulators:start --inspect-functions

See what port it's listening to for debug (e.g. port 9229 ). If you are using VSCode, create(or update) a launch.json inside .vscode subdirectory of your project with the following content:

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

Then simply click on Debug icon in your VScode to connect to the running process. Now put some breakpoints in your functions code and invoke them through browser.

like image 190
pref Avatar answered Nov 08 '25 14:11

pref


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

like image 36
Gabe O'Leary Avatar answered Nov 08 '25 15:11

Gabe O'Leary