I'm using webpack hot module reload (hmr) for my nest.js application. The reload works but doesn't wait for the old instance to close fully (database connection, telegram bot,...) before starting up a new instance. This makes typeorm throw the following error:
AlreadyHasActiveConnectionError: Cannot create a new connection named "default", because connection with such name already exist and it now has an active connection session.
In my main.ts
I have a dispose handler that closes the old instance:
if (module && module.hot) {
module.hot.accept();
module.hot.dispose(async () => {
console.log('disposing module');
await app.close();
console.log('has closed app');
});
}
When I run hmr and make a change to my app, I can see that it calls the dispose handler and immediately starts up the new application. How can I make webpack wait for the promise to resolve that is returned by the dispose handler before starting the new instance?