ERR_STREAM_DESTROYED
Node.jsERRORNotableStreamsHIGH confidence

Cannot call a method on a destroyed stream.

Production Risk

Medium. This indicates a state management issue with streams, which could lead to unhandled exceptions or data loss if not handled correctly.

What this means

This is a generic error indicating that an operation was attempted on a stream that has already been destroyed. A stream can be destroyed via `.destroy()`, by an internal error, or by ending naturally. Once destroyed, most operations (like `pipe`, `write`, or `read`) are no longer permitted.

Why it happens
  1. 1Attempting to pipe to or from a stream that has already been closed or has errored.
  2. 2Calling a method on a stream in a callback that executes after the stream has completed its lifecycle.
  3. 3A race condition where a stream is destroyed by one part of the code while another is about to use it.
How to reproduce

This error is thrown when methods of a stream are called, but an internal flag shows that the stream has already been dismantled.

trigger — this will error
trigger — this will error
const { Writable } = require('stream');
const myStream = new Writable();

myStream.destroy(new Error('something bad happened'));

try {
  myStream.write('some data');
} catch (err) {
  console.error(err.code);
}

expected output

ERR_STREAM_DESTROYED

Fix

Check the 'destroyed' Property

WHEN Before interacting with a stream whose state might be unknown.

Check the 'destroyed' Property
function safePipe(readable, writable) {
  if (readable.destroyed || writable.destroyed) {
    console.error('Cannot pipe from/to a destroyed stream.');
    return;
  }
  readable.pipe(writable);
}

Why this works

Before operating on a stream, check its `destroyed` property. If `true`, the stream is no longer usable and the operation should be aborted.

Code examples
Triggerjs
const { Writable } = require('stream');
const myStream = new Writable();

myStream.destroy(new Error('something bad happened'));

try {  // this triggers ERR_STREAM_DESTROYED
Handle in try/catchjs
try {
  // operation that may throw ERR_STREAM_DESTROYED
  riskyOperation()
} catch (err) {
  if (err.code === 'ERR_STREAM_DESTROYED') {
    console.error('ERR_STREAM_DESTROYED:', err.message)
  } else {
    throw err
  }
}
Defensive pattern to avoid itjs
function safePipe(src, dst) {
  if (src.destroyed || dst.destroyed) return
  src.pipe(dst)
}
What not to do

Same error in other languages
Sources
Official documentation ↗

https://github.com/nodejs/node/blob/main/lib/internal/streams/destroy.js

More information

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

← All Node.js errors