Search code examples
node.jsfirebasegoogle-cloud-firestorefirebase-tools

Problem running JS file with firebase emulators: exec


I was trying out the new Firebase Emulators UI announced on May 21, 2020. In the Firebase Docs, I noticed that there is a CLI command that allows us to run a script file:

firebase emulators:exec scriptpath

Run the script at scriptpath after starting emulators for the Firebase products configured in firebase.json. Emulator processes will automatically stop when the script has finished running.

Although the docs does not mention what kind of script file it should be, I presume it could be either a test script or a workable JavaScript file that be executed standalone with node filename.js without any errors. This makes sense because I can actually run an initial JS file that pre-populate some test data to the Firestore Emulator for further testing.

But since I've previously started the emulators, I have to open a new instance of command window to run the firebase emulators:exec command. Unfortunately, I hit the following errors:

D:\Firebase\my-project>firebase emulators:exec setup-db.js
i  emulators: Starting emulators: functions, firestore, database, hosting, pubsub
!  hub: emulator hub unable to start on port 4400, starting on 4401 instead.
!  emulators: It seems that you are running multiple instances of the emulator suite for project my-project-id. This may result in unexpected behavior.
i  emulators: Shutting down emulators.
i  hub: Stopping emulator hub
!  functions: Port 5001 is not open on localhost, could not start Functions Emulator.
!  functions: To select a different host/port, specify that host/port in a firebase.json config file:
      {
        // ...
        "emulators": {
          "functions": {
            "host": "HOST",
            "port": "PORT"
          }
        }
      }
i  emulators: Shutting down emulators.

Error: Could not start Functions Emulator, port taken.

Take note of this line:

! emulators: It seems that you are running multiple instances of the emulator suite for project my-project-id. This may result in unexpected behavior.

So, how can I run the firebase emulators:exec AFTER the emulators start as mentioned in the Firebase Docs and, can I run the JS file for the said purpose? Thanks in advance!

UPDATE:

If I run just the firebase emulators:exec setup-imtp-db.js without prior running the firebase emulators:start command, I ran into the following errors:

enter image description here

Note that the setup-imtp-db.js does not contain any Cloud Functions code but just a standalone JS file that populates data from another JSON file into Firestore via Admin SDK. I'm using Node.js 12.14.0 and firebase-tools 8.4.0.


Solution

  • According to Firebase CLI Github README.md:

    emulators:exec Start the local Firebase emulators, run a test script, then shut down the emulators.

    So the scriptpath parameter isn't really meant to run a JavaScript file that pre-populates data on Firestore Emulator (and presents on its Emulator UI) for the purpose of UI Functional Test or Integrated Test. The emulators:exec will immediately shut down the emulators after finish executing the test script.

    As @Doug Stevenson suggested in the comment, if a proper error handling is expected from the emulators or a new feature is required, a request or bug report can be posted on Github for firebase-tools.