Search code examples
firebasegoogle-cloud-functionsfirebase-tools

Check if Firebase Emulator is connected from Node scripts


I am writing unit tests for my Firebase Functions and I want to automatically connect the functions, auth, storage, etc. emulators from my script without having to specify if I am testing in local environment or development environment.

Is there any way I can write a script to see if the Firebase Emulator is running on my local machine from an external node script?

For example, is there a way I can see processes running on specific local ports from a node script?

I tried using

import { exec } from "child_process";


const checkEmulator = exec("lsof -i:5000");

(I am using MacOS)

Then using the output to determine if the Firebase Functions Emulator is running on port 5000, but the output of the exec function does not make any sense to me.

Is there a more efficient way to check if the emulator is running on your local machine?

Thanks for any help!


Solution

  • You can use an HTTP Get request with something like curl:

    curl localhost:4400/emulators

    This will print out a JSON object listing all running emulators. Example:

    {
      "hub":{
        "name": "hub",
        "host": "localhost",
        "port": 4400
      },
      "functions": {
        "name": "functions",
        "host": "localhost",
        "port": 5001
      }
      "firestore": {
        "name": "firestore",
        "host": "localhost",
        "port": 8080
      }
    }
    

    Taken from https://firebase.google.com/docs/emulator-suite/install_and_configure

    Alternatively, psaux can list active processes. Each emulator is a different process and is run from .../.cache/firebase/emulators.

    https://www.npmjs.com/package/psaux