Debug firebase http functions v1.0 locally with VS Code
You will need to have google cloud functions emulator modules installed:
npm install -g @google-cloud/functions-emulator
Then you will need to get your firebase project configuration. For that, you need to go to your firebase functions project folder, log in to your project and run the setup:web command:
firebase login
firebase setup:we
The result will be something like this:
{
"apiKey": "YOUR-API-KEY",
"databaseURL": "https://YOUR-FIREBASE-PROJECT.firebaseio.com",
"storageBucket": "YOUR-FIREBASE-PROJECT.appspot.com",
"authDomain": "YOUR-FIREBASE-PROJECT.firebaseapp.com",
"messagingSenderId": "SENDER-ID",
"projectId": "YOUR-FIREBASE-PROJECT"
}
Now, we are gonna use that information to run our firebase functions with google cloud functions emulator.
First, escape special characters, to use it on an environment variable. You can easily achieve this by using node to stringify the configuration object:
node
console.log(JSON.stringify(JSON.stringify({
"databaseURL": "https://YOUR-FIREBASE-PROJECT.firebaseio.com",
"storageBucket": "YOUR-FIREBASE-PROJECT.appspot.com",
"projectId": "YOUR-FIREBASE-PROJECT"
})))
The result is printed to console as:
"{\"databaseURL\":\"https://YOUR-FIREBASE-PROJECT.firebaseio.com\",\"storageBucket\":\"YOUR-FIREBASE-PROJECT.appspot.com\",\"projectId\":\"YOUR-FIREBASE-PROJECT\"}"
Now that we have our configuration string we just need to run the function :)
start cloud functions emulator:
functions start
Deploy your firebase function code:
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 YOUR_BEAUTIFUL_FUNCTION_NAME
Remeber using a long timeout to be able to debug your function without getting a timeout error.
Now we just need to start the function debugger:
functions debug YOUR_BEAUTIFUL_FUNCTION_NAME [--port 6000] [--pause true]
You can use the optional arguments:
port
to change the debugger portpause
to pause the function execution until the a debugger is connected, so you can debug your function setup code and not just the http handlerFinally, just create an attach
run configuration on VS Code to attach the debugger to your running function:
{
"type": "node",
"request": "attach",
"name": "Attach",
"port": 5858
}
And run your Attach configuration on debug mode!