Debugging firebase cloud functions

14,606

Solution 1

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

Solution 2

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

Solution 3

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.

Share:
14,606

Related videos on Youtube

nixn
Author by

nixn

Updated on July 13, 2022

Comments

  • nixn
    nixn almost 2 years

    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
        }
    ]
    }
    
  • Artsiom Miksiuk
    Artsiom Miksiuk over 6 years
    What you mean not works in case of Local Emulator? I worked with and was able to debug my functions code locally. Did you check Debug section?cloud.google.com/functions/docs/…
  • nixn
    nixn over 6 years
    Yes I did give it a try, but as I mentioned, its not an http request, its a database trigger. I am doing this the first time btw and how could a local emulator check changes on the database?
  • Artsiom Miksiuk
    Artsiom Miksiuk over 6 years
    So.. Yes. Did you check this thing? firebase.google.com/docs/functions/local-emulator
  • nixn
    nixn over 6 years
    Ok, thank you for this answer. I got into it and unfortunatelly I got an error on the CLI: ! functions: Cannot start emulator. Error: Cannot find module 'C:\Users\Dev\AppData\Roaming\npm\node_modules\firebase-tool‌​s\node_modules\grpc\‌​src\node\extension_b‌​inary\node-v48-win32‌​-x64\grpc_node.node'
  • Artsiom Miksiuk
    Artsiom Miksiuk over 6 years
    Some packages are missing. Try to install all required libraries into local folder with --save flag, in order to be sure, that all is in place. In other case, you need to try reinstall missing packages.
  • nixn
    nixn over 6 years
    No packages are missing. I even installed grpc. The problem still exists. github.com/firebase/firebase-tools/issues/442
  • Emad Aghaei
    Emad Aghaei about 6 years
    Is there any solution for debuging the functions in IDE like WebStorm?
  • Artsiom Miksiuk
    Artsiom Miksiuk about 6 years
    Emad, don't think so. Basically, you may try to setup remote debugging via ports exposion. Not sure if it is even possible in case of functions.
  • Elia Weiss
    Elia Weiss over 4 years
    'ndb' is not recognized as an internal or external command, operable program or batch file.
  • Elia Weiss
    Elia Weiss over 4 years
    getting Error: FIREBASE FATAL ERROR: Cannot parse Firebase url. Please use https://<YOUR FIREBASE>.firebaseio.com
  • jimmont
    jimmont over 4 years
    @EliaWeiss installing it in your path should make it available in your environment, for more information see the docs via npmjs.com/package/ndb the answer I provided was for the given question based on my individual experience, setup is available via the linked docs, please read them
  • KClough
    KClough over 4 years
    This was working for me, but when I setup the project on a new machine, I now get the error "Inspector is not available". I'm assuming it is an issue related to either a newer version of ndb or the firebase function emulator.
  • jimmont
    jimmont over 4 years
    @KClough I have had problems with ndb after version 1.0.48, specifically using Firebase so I do: npm i -g [email protected] more details in the bug report--and I'm not certain this applies (hope it's useful in finding a solution) github.com/GoogleChromeLabs/ndb/issues/269
  • KClough
    KClough over 4 years
    @jimmont I updated my firebase-functions to 3.3.0, and it is working now with [email protected].