4

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

Pang
  • 9,564
  • 146
  • 81
  • 122
james murphy
  • 1,505
  • 5
  • 31
  • 57
  • Does this answer your question? [Functions debugging in VS Code](https://stackoverflow.com/questions/45920014/functions-debugging-in-vs-code) – GorvGoyl Jun 24 '20 at 23:48

5 Answers5

9

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.

pref
  • 1,651
  • 14
  • 24
  • 1
    Port 9229 is important. I first thought it was what my Emulators terminal was telling me (5001), but it is actually a separate port for debugging 9229. – Sebastian Patten Dec 14 '22 at 13:10
  • What @SebastianPatten says is very important You will see a message like: `Debugger listening on ws://127.0.0.1:9229/....` Find the port number there and use it in your debugger config – Alexis Rengifo Jan 01 '23 at 20:41
  • 9229 is your websocket port. 5001 is your HTTP port. Different ports for different protocols. More context, 9229 is the Node.js inspector port https://nodejs.org/en/docs/guides/debugging-getting-started/ – dwu39 Feb 10 '23 at 23:29
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

Gabe O'Leary
  • 2,010
  • 4
  • 24
  • 41
1

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.
Dery
  • 11
  • 1
0

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
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • ...when I try this I get a new error......"Not starting the database emulator, make sure you have run firebase init". I definitely ran "firebase init functions" when I first created the project – james murphy Nov 11 '19 at 21:48
  • Also run `firebase init database` to initialize the database product as well. – Doug Stevenson Nov 11 '19 at 23:38
  • OK thats progress thanks - I now have functions emulator on port 5001 and database emulator on port 9000. I want to set a breakpoint in my function so I can step through my code when the database update triggers it. What URL do I go to, to debug? Thanks – james murphy Nov 12 '19 at 18:49
  • 2
    The emulator suite currently does not support step through debugging. It's being worked on. – Doug Stevenson Nov 12 '19 at 19:03
  • 1
    OK thats interesting.....so for now you just use the emulator for console output? Can you tell me how I can run a function and view console output. Sorry for all questions but I am a newbie at this – james murphy Nov 12 '19 at 20:27
  • I'm not sure what you mean. Console log should appear just fine. – Doug Stevenson Nov 12 '19 at 20:30
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/202227/discussion-between-james-murphy-and-doug-stevenson). – james murphy Nov 12 '19 at 20:35
0

Solution given by pref works. Below is my .vscode/launch.json

{
    "configurations": [
        {   "type": "node",
            "request": "attach",
            "name": "Node.js-Debugger",
            "port": 9229
        }
    ]
}

Below is my firebase command.

$ firebase emulators:start --inspect-functions --import emulator_data --export-on-exit
Kamal Mehta
  • 3
  • 1
  • 3