fs.writeFile is an asynchronous function. So I think this means that the handler ends without waiting for the writing function to resolve.
fs.writeFile
You can either:
await
fs.writeFileSync