ERR_STREAM_ALREADY_FINISHED
Node.jsERRORNotableStreamHIGH confidence

Stream operation called after stream has finished

Production Risk

Common in async HTTP handlers; always check stream state before writing.

What this means

Thrown when a write or end operation is attempted on a Writable stream that has already emitted the finish event or been explicitly ended. Once a stream finishes, no more data can be written to it.

Why it happens
  1. 1Calling write() or end() after end() has already been called
  2. 2Async handler that writes to a response stream after it was already finalised
  3. 3Double-ending a stream in different code paths
How to reproduce

Triggered when write() or end() is called on a Writable after its finish event has fired.

trigger — this will error
trigger — this will error
const { Writable } = require('stream');
const w = new Writable({ write(c, e, cb) { cb(); } });
w.end('done');
w.write('more'); // throws — stream already finished

expected output

Error [ERR_STREAM_ALREADY_FINISHED]: write() after stream is finished

Fix

Guard writes with the writableFinished flag

WHEN In async handlers that may fire after the stream has ended

Guard writes with the writableFinished flag
if (!stream.writableFinished && !stream.destroyed) {
  stream.write(chunk);
}

Why this works

Checking writableFinished prevents write calls on an already-ended stream.

Code examples
Triggerjs
const { Writable } = require('stream');
const w = new Writable({ write(c, e, cb) { cb(); } });
w.end('done');
w.write('more'); // throws — stream already finished  // this triggers ERR_STREAM_ALREADY_FINISHED
Handle in try/catchjs
try {
  // operation that may throw ERR_STREAM_ALREADY_FINISHED
  riskyOperation()
} catch (err) {
  if (err.code === 'ERR_STREAM_ALREADY_FINISHED') {
    console.error('ERR_STREAM_ALREADY_FINISHED:', err.message)
  } else {
    throw err
  }
}
Defensive pattern to avoid itjs
// Validate inputs before calling the operation
function safe_err_stream_already_finished(...args) {
  // validate args here
  return performOperation(...args)
}
What not to do

Write to a response or stream after calling end()

end() seals the stream; subsequent writes throw this error.

Same error in other languages
Sources
Official documentation ↗

Node.js Error Codes Documentation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Node.js errors