12

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
    }
]
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
nixn
  • 1,337
  • 3
  • 16
  • 33
  • Does this answer your question? [Functions debugging in VS Code](https://stackoverflow.com/questions/45920014/functions-debugging-in-vs-code) – GorvGoyl Dec 19 '19 at 13:24

5 Answers5

3

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

Artsiom Miksiuk
  • 3,896
  • 9
  • 33
  • 49
  • 1
    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?https://cloud.google.com/functions/docs/emulator#debugging_with_the_emulator – Artsiom Miksiuk Dec 04 '17 at 08:23
  • 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? – nixn Dec 04 '17 at 08:33
  • 1
    So.. Yes. Did you check this thing? https://firebase.google.com/docs/functions/local-emulator – Artsiom Miksiuk Dec 04 '17 at 08:56
  • 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-tools\node_modules\grpc\src\node\extension_binary\node-v48-win32-x64\grpc_node.node' – nixn Dec 04 '17 at 09:14
  • 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. – Artsiom Miksiuk Dec 04 '17 at 09:32
  • No packages are missing. I even installed grpc. The problem still exists. https://github.com/firebase/firebase-tools/issues/442 – nixn Dec 04 '17 at 12:14
  • Is there any solution for debuging the functions in IDE like WebStorm? – E A Mar 08 '18 at 16:42
  • 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. – Artsiom Miksiuk Mar 08 '18 at 17:31
3

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

Mohamed Dernoun
  • 776
  • 5
  • 13
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

mwebler
  • 41
  • 4
1

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 ├── firebase-tools@9.2.2 ├── firebase@8.2.5

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.

jimmont
  • 2,304
  • 1
  • 27
  • 29
  • 'ndb' is not recognized as an internal or external command, operable program or batch file. – Elia Weiss Oct 06 '19 at 12:29
  • 1
    @EliaWeiss installing it in your path should make it available in your environment, for more information see the docs via https://www.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 – jimmont Oct 08 '19 at 02:55
  • 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. – KClough Nov 07 '19 at 19:39
  • @KClough I have had problems with ndb after version 1.0.48, specifically using Firebase so I do: `npm i -g ndb@v1.0.48` more details in the bug report--and I'm not certain this applies (hope it's useful in finding a solution) https://github.com/GoogleChromeLabs/ndb/issues/269 – jimmont Nov 07 '19 at 23:33
  • 1
    @jimmont I updated my firebase-functions to 3.3.0, and it is working now with ndb@v1.1.5. – KClough Nov 13 '19 at 15:58
0

An easy way to debug triggered functions is to just test it as a callable function in the emulator and trigger it manually. Then when you know it works how you want it, turn it into a triggered function. This way you at least know the problem is with your path or how you are accessing variables.

nitroplr
  • 41
  • 3