Normally, the Node.js process will exit when there is no work scheduled (docs). You shouldn’t call
process.exit() unless it is necessary to terminate the Node.js process immediately due to an error condition. Calling
process.exit() doesn’t let pending events to complete which may lead to unpredictable results as demonstrated in the docs.
First you need to cancel any asynchronous listeners. For example, if you subscribed for data changes, you need to unsubscribe:
let func = firebase.database().ref(userDB).on("value", myHandler); ... firebase.database().ref(userDB).off("value", func);
Some people suggest that you also call
firebase.database().goOffline() in the final stage.
Additionally, as described in these bug reports (#1 and #2), if you used
firebase.auth() you need to call
And finally, you also need to destroy the Firebase application by calling
This has worked for me using Node.js version 10 and Firebase JS SDK version 8.