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.
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.
- 1Attempting to pipe to or from a stream that has already been closed or has errored.
- 2Calling a method on a stream in a callback that executes after the stream has completed its lifecycle.
- 3A race condition where a stream is destroyed by one part of the code while another is about to use it.
This error is thrown when methods of a stream are called, but an internal flag shows that the stream has already been dismantled.
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.
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.
const { Writable } = require('stream');
const myStream = new Writable();
myStream.destroy(new Error('something bad happened'));
try { // this triggers ERR_STREAM_DESTROYEDtry {
// 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
}
}function safePipe(src, dst) {
if (src.destroyed || dst.destroyed) return
src.pipe(dst)
}✕
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