Firebase emulator for Cloud Functions not updating the code

Viewed 1158

I'm deploying the following cloud function using firebase deploy --only functions :

export const testFunction = functions.https.onCall((data, context) => {
  return {"data": "hello"};
});

and when call it from the client app using the code

var testFunction = firebase.functions().httpsCallable("testFunction");
      testFunction().then((result) => {
        // Read result of the Cloud Function.
        this.data = result.data;
});

it works as expected. Now, I want to continue developing the function testing it on the local emulator so, following the documentation, I added this line to the web app code (before the function)

firebase.functions().useEmulator("localhost", 5001); // for local simulator

and I run the local emulator with:

firebase emulators:start --only functions

If I run the client app now, I correctly see the call going through the local emulator instead of the remote cloud function.

Problem: If modify the code the local function doesn't get updated. I need to run firebase deploy again in order to see the change in the response. How can I just deploy locally?

2 Answers

This stackoverflow solves the issue.

Solution in short:

  • After every change in your code, run npm run build to recomplie the code.

  • To auto-compile for every change in your code. Change "build": "tsc" to "build": "tsc -w" in your package.json file, and run the emulator in an another terminal window.

You can use the functions shell as described in this documentation here in order to deploy, run and test functions locally. I find the documentation quite confusing, as it only mentions how to use .sh scripts to test locally.

firebase functions:shell

Note that if you use the shell, you don't need to run the emulators separately; the shell will run them for you. The shell will automatically update based on changes to the index.js functions file, but if you're using TypeScript you may have to rebuild after you make changes. This is typically done by going into the functions directory and running npm run build. You can also automate this as well, according to this answer, but I haven't tried this approach yet.

Related