34

Up until now I've been doing the following to use and test my functions locally during development:

I leave this running in one terminal:

firebase serve --only functions

And I add this on my client code when I'm initializing my Firebase app:

const config = {
  apiKey: process.env.FIREBASE_APP_API_KEY,
  authDomain: process.env.FIREBASE_APP_AUTH_DOMAIN,
  databaseURL: process.env.FIREBASE_APP_DATABASE_URL,
  projectId: process.env.FIREBASE_APP_PROJECT_ID,
  storageBucket: process.env.FIREBASE_APP_STORAGE_BUCKET,
  messagingSenderId: process.env.FIREBASE_APP_MESSAGING_SENDER_ID
};

firebase.initializeApp(config);

// THIS IS THE DEFAULT HOST AND PORT USED BY 'firebase serve command'
firebase.functions().useFunctionsEmulator('http://localhost:5000');

I have tested only HTTP callable functions and so far this has been working fine.


But in the docs, I see this:

https://firebase.google.com/docs/functions/local-emulator

Run functions locally The Firebase CLI includes a Cloud Functions emulator which can emulate the following function types:

  • HTTPS functions
  • Callable functions
  • Cloud Firestore functions

You can run functions locally to test them before deploying to production.

1. Install the Firebase CLI - Link

2. Set up admin credentials (optional) - Link

$ set GOOGLE_APPLICATION_CREDENTIALS=path\to\key.json
$ firebase emulators:start

After completing these steps, your functions tests can access Firebase and Google APIs using the Admin SDK. For example, when testing an Authentication trigger, the emulated function could call admin.auth().getUserByEmail(email).

QUESTION

What is the difference between the two methods of running functions locally?

cbdeveloper
  • 27,898
  • 37
  • 155
  • 336

1 Answers1

47

firebase emulators:start is part of the new Firebase emulator suite, which is designed to allow several emulated products work together. It's completely different than firebase serve --only functions, which is based on the @google-cloud/functions-emulator npm package, which is not actively maintained (click through and you will see that it's deprecated). It's recommended that you start moving to the new emulator suite and away from firebase serve.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thanks! And what about `firebase deploy` and `firebase serve` with other flags like `firebase serve --only hosting` ? Can we still use them ? Was anything else deprecated other than `firebase serve --only functions` ? – cbdeveloper Jun 16 '19 at 18:09
  • You might want to look through the release notes on the firebase-tools repo. – Doug Stevenson Jun 16 '19 at 18:11
  • We've noticed that `serve` is about twice as fast (600ms) as `emulate` (1200ms) for a simple function. Is the expected? Why? – Shaun Luttin Oct 23 '19 at 17:49
  • @ShaunLuttin If you have issues with the emulator, please post your question to its GitHub. https://github.com/firebase/firebase-tools – Doug Stevenson Oct 23 '19 at 20:14
  • @DougStevenson Beginner here, hoping for clarification re: your comment on moving from "firebase serve" to the emulator suite: does the emulator suite offer a web server? (I did not see any mention of one in the docs.) If not, how does one replicate the functionality of the deprecated "firebase serve" command? I'm happy to open a new question, if you'd prefer. – ultraGentle Dec 10 '19 at 16:27
  • emulator also will print to the console. (e.g. a console.log statement in your firestore cloud function) – Renzhentaxi Baerde Jan 28 '20 at 05:25
  • 1
    Found this question helpful but still was unclear on how to use the Emulator Suite with React during local development without needing to build my project into static files, so I wrote this article on how to use the Emulator without building: https://medium.com/@samshapiro/firebase-local-development-with-firestore-cloud-functions-and-react-c29c188bd60e – Sam Mar 08 '20 at 20:48
  • @Sam thanks for that post - indeed the local emulator server is not particularly useful for apps that have a compilation step which I believe would be most of the modern ones. I wonder if there is a better built in mechanism? It would be great if firebase-tools could be told which command to run to build the app that will be served tbh – Gustavo Hoirisch May 31 '20 at 12:17