9

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?

Claus
  • 5,662
  • 10
  • 77
  • 118
  • 4
    as per [Official Documentation](https://firebase.google.com/docs/functions/local-emulator) the code is updated automatically on the emulator but if the code needs to be transpiled you need to start the emulator again. Are you using TypeScript or React? – Soni Sol Feb 15 '21 at 20:38

3 Answers3

11

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.

Praveen
  • 332
  • 4
  • 13
0

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.

0

One of the solutions is running 2 commands in 2 separate terminals:

1. firebase emulators:start (on root)
2. yarn run build:watch (inside functions folder)

If you want to execute only 1 command i added the following scripts on my root package.json:

"start:emulators": "firebase emulators:start & yarn run build:functions",
"build:functions": "cd functions && yarn run build:watch"

Then we can only run:

yarn run start:emulators
Miguel Jesus
  • 311
  • 4
  • 6