Let's say I run a Node script in a Linux environment that contains the following:
fs.writeFileSync("output.json", JSON.stringify(data))
Then, I abort the script (cmd/ctrl + c) after the writeFileSync operation has begun but before it has finished. Does "output.json" exist? If so, does it contain any data?
I did a quick test on Windows 10 and had some interesting results:
fs.openSync and fs.writeSync. Between both calls, I took the time to find the file handle in Process Explorer, which I then closed during the fs.writeSync call. Now PE froze until the write closed. Tried ending the Node process using Task Manager in the meantime. Result? Every byte was still written to the file.ctrl+c. Node throws a nice error and immediately exits, but the file still got created with 0 bytes. Your mileage might vary though. especially if you set up signal handling.I didn't go as far as cutting the power or unplugging the disk during write, although at that point it's probably OS/disk-dependent on what happens next. Seems like NodeJS will always create the file though. Not that surprising, since fs.openSync('path', 'w') already does that, although still a bit unexpected it even succeeds in creating the (empty) file right before it errors about not enough disk space.