Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging firebase cloud functions

How do I debug with visual studio code firebase-database trigger functions? I tried the emulator, but I get an error when I call this

functions debug myMethod

C:\functions\functions>functions debug createUserChat
ERROR: Error: Function myMethod in location us-central1 in project myProject does not exist
at C:\Users\Dev\AppData\Roaming\npm\node_modules\@google-cloud\functions-emulator\node_modules\grpc\src\client.js:554:15

This code I want to debug

require('@google-cloud/debug-agent').start({ allowExpressions: true });;

const functions = require('firebase-functions'),
        admin = require('firebase-admin'),
        logging = require('@google-cloud/logging')();
        admin.initializeApp(functions.config().firebase);

exports.myMethod= functions.database.ref('Tasks/{taskID}/taskStatus').onUpdate(event =>{
       // do sth.
});

this is my launch file

{
"version": "0.2.0",
"configurations": [
    {
        "name": "Debug Function",
        "type": "node",
        "request": "attach",
        "port": 5858
    }
]
}
like image 450
nixn Avatar asked Dec 03 '17 16:12

nixn


4 Answers

use firebase emulators:start --inspect-functions --only functions

more in the docs: https://firebase.google.com/docs/emulator-suite/install_and_configure

using npm ls -g --depth=0 ├── [email protected] ├── [email protected]

related question and answer: https://stackoverflow.com/a/65430902/965666

previously (no longer works with the above updated versions):

try: ndb firebase serve

this opens a specific Chrome browser with the debugging tools and can be a little slow to instrument all the child processes, so give it a some time. Once running it should hit debugger breakpoints, etc.

  • https://github.com/GoogleChromeLabs/ndb
  • more detail https://stackoverflow.com/a/56715735/965666
like image 60
jimmont Avatar answered Oct 21 '22 08:10

jimmont


You can make it work on Visual Studio Code using Firebase functions 1.0 without having to change anything on the function code. And your launch configuration seems to be correct...

You basically just need to set the FIREBASE_CONFIG environment variable properly when running the functions deploy command. Something like (don not forget to escape the " characters):

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

This works with Firebase Functions 1.0 because in the new version Firebase functions read it's configuration from the environment (https://firebase.google.com/docs/functions/beta-v1-diff#new_initialization_syntax_for_firebase_admin)

After that, you just run normally the functions debug FUNCTION_NAME [--port] to start the function debugger and run your 'Attach' VS Code configuration.

I wrote a little tutorial on that with more details and images: https://medium.com/@mwebler/debugging-firebase-functions-with-vs-code-3afab528bb36

like image 40
mwebler Avatar answered Oct 21 '22 08:10

mwebler


debug-agent required only for remote debugging. If you want to debug function locally use Cloud Functions Emulator.

https://cloud.google.com/functions/docs/emulator

like image 44
QuestionAndAnswer Avatar answered Oct 21 '22 08:10

QuestionAndAnswer


In a simple steps i did the following and I am not only able to debug the function but I gain auto rebuild every time I made a change: first create a launcher like this (I suppose you use vs-code):

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

After that run this two commandes separately:

npm run build:watch

firebase emulators:start --inspect-functions

Happy debugging

like image 29
Mohamed Dernoun Avatar answered Oct 21 '22 08:10

Mohamed Dernoun