When opening a file using fs.createWriteStream
, how do you handle the case where the underlying file is deleted (eg by a 3rd party, such as logrotate
)?
None of the normal events seem to fire in this case.
For example:
const stream = fs.createWriteStream(filename, { flags: 'a' });
// None of these seem to fire
stream.on('close', () => console.log('CLOSED'));
stream.on('error', () => console.log('ERRORED'));
stream.on('finish', () => console.log('FINISHED'));
stream.write('foo');
// wait for flush...
fs.unlinkSync(filename);
// This doesn't work, but also doesn't error
stream.write('bar');
Working example: https://runkit.com/alecgibson/5da4545e76e836001a191ec0
Related:
- https://github.com/nodejs/node/issues/21122
- https://nodejs.org/api/fs.html#fs_fs_createwritestream_path_options
Notes:
- Node v12.10.0 added the
emitClose
option, which presumably fixes this(?), but I'd like to do this in v10 (LTS)