2

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:

Notes:

  • Node v12.10.0 added the emitClose option, which presumably fixes this(?), but I'd like to do this in v10 (LTS)
Alec
  • 2,432
  • 2
  • 18
  • 28

0 Answers0