Firebase Emulator --export-on-exit not working properly (Windows 10)
Asked Answered
M

6

16

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:

enter image description here

Here is my full project file structure:

enter image description here

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:

  • Changing the relative file paths from ./data to data
  • Ensuring all java.exe instances are closed (via task manager)
  • Restarting my computer

EDIT 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

Microdont answered 22/4, 2022 at 17:53 Comment(3)
Same issue today but on mac. I'm using firebase-tools version 10.7.0. The work around I'm using is to manually run firebase emulators:export ./emulators (or in your case ./data) before terminating the emulator and not using --export-on-exit.Vigilant
@Vigilant maybe you saved me weeks of workDenni
To avoid restarting your computer ... You can kill all emulator processes using the npm package 'kill-port'.Gildea
Q
3

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.

Quasimodo answered 11/6, 2022 at 7:54 Comment(1)
Sometimes this works - sometimes it doesn't, esp. when the .firebase folder still exists and a new folder is created. At that point, neither folder has up-to-date content. I'm assuming I'd have to merge them somehow. Any way to make it always work? (p.s. Re-specifying the same value for --export-on-exit is unnecessary)Xiphisternum
D
2

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.

Dedicated answered 30/3, 2023 at 4:17 Comment(2)
I can't get this to work on 11.25.3 :(Bragg
No ... won't work for me.Gildea
N
1

I had the same issue today and i solved this problem creating a .bat with the follow command:

    @ECHO Starting emulator...

    call firebase emulators:start --only auth,functions,firestore,pubsub,storage --import=./firebase_export --export-on-exit

    pause

If we don't use --only <modules>, like the example, always when exit, the emulator will export in differents folders.

Probably this is a bug from firebase-tools, but this workaround worked to me.

Naamana answered 28/11, 2022 at 23:35 Comment(1)
You don't need a .bat file ... just add a command to your package scripts.Gildea
B
1

I'm using firebase-tools version 13.5.2 on Windows 11.

In my case I was both importing and exporting data using this command:

firebase emulators:start --import=./firebase-export --export-on-exit

But the issue here was with the data export which was generating the firebase-export-{uid} folder, while showing this error on exit:

i  Exporting data to: C:\Users\user\Documents\Dev\firebase-export

i  emulators: Received export request. Exporting data to C:\Users\user\Documents\Dev\firebase-export.

!  emulators: Export failed: EPERM: operation not permitted, rename 'C:\Users\user\Documents\Dev\firebase-export-1711276697951kF5xq2' -> 'C:\Users\user\Documents\Dev\firebase-export'

!  Export request failed, see emulator logs for more information.

!  Automatic export to "./firebase-export" failed, going to exit now...

i  emulators: Shutting down emulators.

The import works just fine after renaming the folder.

It seems that firebase emulator first exports the data to the folder with uid firebase-export-{uid} then fails to rename the folder due to the lack of permission on Windows.

Note: I tried using Linux for instance on a virtual machine and it worked seamlessly without any issues.

Solution :

I had to use another folder outside the C:/Users/user/Documents folder in order to export the data correctly, for instance I used a separate folder inside another partition D:/Dev/firebase-export and it worked just fine. Just try to avoid Windows folders and you will be fine.

Bravar answered 24/3, 2024 at 10:59 Comment(0)
K
0

For those using Vite, set this on vite.config.ts:

server: { watch: { usePolling: true }}

The symptoms are caused by the Vite dev server holding locks on nested folders (which can prevent the emulator from renaming its snapshots on Windows).

Credits/Source: bmcbarron - github

Kimbrough answered 28/11, 2023 at 18:24 Comment(0)
F
0

Starting the firebase emulator with --only functions,firestore,auth works for me, so it must be one of the other emulators screwing up the export.

Farny answered 20/5, 2024 at 4:2 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.