5

I usually have my Firebase projects organized with a separate repo for the web app, manager portal, and cloud functions. Because the web app and manager portal use the same firebase project I keep the cloud functions in their own separate repo. So 3 different repos/projects using the same Firebase project.

I'm testing out how to use the Firebase Local Emulator Suite with this set up but it doesn't appear I can start the emulator for the cloud functions within my Cloud Function project, then use the emulator in my web app project for firestore and calling the functions, and then the same for my manager portal project. This will cause conflicts and cause them all to run on different ports and the functions project won't have access to the firestore emulator and the web app and manager won't have access to the functions emulator.

Is there a way to make this work? The only way I can see how is to have my manager app, web app, and functions all in the same project/repo...which I don't want to do for a bunch of reasons.

Currently I just set up a 2nd Staging project on firebase and use that for testing but would love to be able to do all of this locally. Any help is much appreciated.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
askilondz
  • 3,264
  • 2
  • 28
  • 40

2 Answers2

2

I was just struggling with the same today and it turns out it is as simple as:

  • in your main project, start your emulators as usual with e.g. firebase emulators:start --only firestore

  • in your dependent projects, do not try to start the firestore emulator, but instead initialise the firebase config as follows:

import * as express from 'express'
import * as admin from 'firebase-admin'
import * as functions from 'firebase-functions'

admin.initializeApp(functions.config().firebase)

var db = admin.firestore()
if (process.env.NODE_ENV !== 'production') {
  db.settings({
    host: "localhost:8080",
    ssl: false
  })
}
rantoniuk
  • 1,083
  • 12
  • 18
  • You're still able to call the local functions with the emulator running `--only firestore`? – askilondz Oct 29 '20 at 13:46
  • To clarify, I have one project that runs `firestore,hosting` emulators and another project that is `functions` only. From the functions project that runs `--only function` I am using the database from another project that runs `--only firestore,hosting` – rantoniuk Oct 29 '20 at 16:06
  • How may I configure something like this for a Flutter project? – Andres Silva Jul 31 '21 at 23:41
0

You can use symbolic links in your project directories to create a "fake" functions directory. Then you can run a single instance of the emulator.

Running two instances of the emulator can cause several weird bugs as it is not recommended.

On windows:

mklink /D "R:\my-cloud-functions-proj\functions" "R:\my-web-app\functions"
MadMac
  • 4,048
  • 6
  • 32
  • 69