I have been experimenting with using Firebase emulators to decrease the number of reads/writes against my actual Firestore db while I am developing a web app. More specifically, I would like to create some re-useable test data from the Emulator UI to work with as I am developing my app, before I deploy to production.
I saw there are options to export data from the Emulator UI and re-import them in later sessions, so after following the docs, I have setup a simple npm script in package.json:
"em-startup": "firebase emulators:start --export-on-exit=data".
https://firebase.google.com/docs/emulator-suite/install_and_configure#export_and_import_emulator_data
When I run npm run em-startup, the emulator starts up as expected, I can create collections/docs, etc. in the Emulator UI:
> [email protected] em-startup
> firebase emulators:start --export-on-exit=data
i emulators: Starting emulators: auth, firestore, database, hosting, pubsub, storage
! emulators: It seems that you are running multiple instances of the emulator suite for project drew-daniels-wheres-waldo. This may result in unexpected behavior.
i firestore: Firestore Emulator logging to firestore-debug.log
i database: Database Emulator logging to database-debug.log
i pubsub: Pub/Sub Emulator logging to pubsub-debug.log
i hosting: Serving hosting files from: build
+ hosting: Local server: http://localhost:5000
i ui: Emulator UI logging to ui-debug.log
┌─────────────────────────────────────────────────────────────┐
│ ✔ All emulators ready! It is now safe to connect your app. │
│ i View Emulator UI at http://localhost:4000 │
└─────────────────────────────────────────────────────────────┘
┌────────────────┬────────────────┬─────────────────────────────────┐
│ Emulator │ Host:Port │ View in Emulator UI │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Authentication │ localhost:9099 │ http://localhost:4000/auth │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Firestore │ localhost:8080 │ http://localhost:4000/firestore │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Database │ localhost:9000 │ http://localhost:4000/database │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Hosting │ localhost:5000 │ n/a │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Pub/Sub │ localhost:8085 │ n/a │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Storage │ localhost:9199 │ http://localhost:4000/storage │
└────────────────┴────────────────┴─────────────────────────────────┘
Emulator Hub running at localhost:4400
Other reserved ports: 4500
Issues? Report them at https://github.com/firebase/firebase-tools/issues and attach the *-debug.log files.
but when I ctrl+C to stop the emulator, PowerShell generates the following output:
i emulators: Received SIGINT (Ctrl-C) for the first time. Starting a clean shutdown.
i emulators: Please wait for a clean shutdown or send the SIGINT (Ctrl-C) signal again to stop right now.
i Automatically exporting data using --export-on-exit "data" please wait for the export to finish...
Terminate batch job (Y/N)? Error: Storage Emulator Rules runtime exited unexpectedly.
i Found running emulator hub for project (my-project-name) at http://localhost:4400
i Exporting data to: C:\Users\MyUserName\my\folder\project\data
i emulators: Received export request. Exporting data to C:\Users\MyUserName\my\folder\project\data.
Additionally, no matter what file path I pass as a parameter in --export-on-exit= seems to be taken into account, and all that gets exported are files like these output to my project's root directory:

Here is my full project file structure:

It appears that the Firebase emulator is not gracefully shutting down with ctrl+C and consequently the data from my emulator UI session are not being successfully output and saved in my data directory.
What I have tried:
./data to dataEDIT There is an open issue on Github regarding this as it appears to be a bug that more people are experiencing. I posted a comment detailing what I'm seeing from my end.
https://github.com/firebase/firebase-tools/issues/3092
The edited doc is exported in a separate folder with name like 'firebase-export-1654933704513LgLzMo' on exiting the emulator. you have rename it to 'firebase-export' so that the import is done from that file once you start the emulator.
The script is like this:
firebase emulators:start --only functions,firestore --import ./firebase-export --export-on-exit ./firebase-export
You have to keep deleting the old one and renaming the latest one to import the latest data.
My use-case isn't exactly the same, because I was both importing and exporting the data (so that the emulator data would persist across restarts), whereas your example command line doesn't specify that you want to import the data on start-up.
That said, I was running the emulator explicitly specifying both the import and export directories, like this: firebase emulators:start --import ./.emulator-data --export-on-exit=./.emulator-data, and getting the same issue as you describe, with the export going to auto-generated directory names firebase-export-${guid} on each shutdown instead of the directory name I specified.
What fixed it for me was to stop specifying the export directory twice, and only specify it for the import argument. According to the firebase documentation: "If --import is used, the export path defaults to the same; for example: firebase emulators:start --import=./data-path --export-on-exit"
For the record, I'm using "firebase-tools": "^11.24.0", and am also running on Windows 10.
So my command line is now firebase emulators:start --import ./.emulator-data --export-on-exit and the current state of the emulator is being saved into the .emulator-data folder each time the emulator shuts down, as desired.
That command is also able to start up when the .emulator-data folder does not exist, so if your goal is to populate your emulator once manually and then load it on start thereafter, you could use the version of the command with both --import and --export-on-exit, populate your data, shut down the emulator, then remove the --export-on-exit for subsequent invocations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With