I've been poking around the node environment and realized I couldn't script a setTimeout function that successfully calls process.exit(). After a bit of trial and error, I understand the cause to be the node process was started with a command line argument of --watch-path which I presume creates an unhandled promise.
Here's the node command starting the process:
"node --env-file=.env --loader ts-node/esm --watch-path=./src index.ts"
And this is my code block to end the process:
if (process.argv[1] === import.meta.filename) { //if this file is first executed file on node process
fastify.listen({port: Number(process.env.PORT), host: process.env.HOST}, (err, address) => {
if (err) {
fastify.log.error(err)
process.exit(1)
}
console.log(`App listening address: ${address} , on port ${process.env.PORT}`);
console.log("node environment: ", process.env.NODE_ENV);
(() => {
console.log("closing server...");
setTimeout(function () {
fastify.close();
process.exit(0);
}, 3500)
})();
});
}
I've already run an A/B comparison to verify that my database and dev server connections are all closing correctly. The only difference is whether the --watch flag is set when starting the process. Is there a way to script a workaround that resolves this promise from the file being watched?
I tried calling process.exit() expecting the process to end... instead the process kept running in idle.