Readable stream subclass did not implement _read()
Production Risk
Causes stream to error on first read; easy to catch in development.
Thrown when a class extends stream.Readable but does not implement the required _read() method. The _read() method is the hook that custom Readable streams must override to supply data to consumers.
- 1Subclassing Readable without overriding _read()
- 2Forgetting to implement _read() when converting a class to a Readable
- 3Typo in the method name (e.g. read() instead of _read())
Triggered when the stream infrastructure calls _read() and finds only the base-class no-op that throws.
const { Readable } = require('stream');
class MyStream extends Readable {
// _read() not implemented
}
const s = new MyStream();
s.on('data', console.log); // triggers _read(), which throwsexpected output
Error [ERR_STREAM_READ_NOT_IMPLEMENTED]: _read() is not implemented
Fix 1
Implement _read() in your Readable subclass
WHEN Always — required when subclassing Readable
class MyStream extends Readable {
constructor() {
super();
this._data = ['a', 'b', 'c', null];
this._index = 0;
}
_read() {
this.push(this._data[this._index++]);
}
}Why this works
_read() is the producer hook; implementing it correctly supplies data to the stream buffer.
Fix 2
Use Readable.from() for simple iterables instead of subclassing
WHEN When the data source is an iterable or async iterable
const { Readable } = require('stream');
const stream = Readable.from(['a', 'b', 'c']);Why this works
Readable.from() wraps an iterable in a Readable without requiring _read() implementation.
const { Readable } = require('stream');
class MyStream extends Readable {
// _read() not implemented
}
const s = new MyStream();
s.on('data', console.log); // triggers _read(), which throws // this triggers ERR_STREAM_READ_NOT_IMPLEMENTEDtry {
// operation that may throw ERR_STREAM_READ_NOT_IMPLEMENTED
riskyOperation()
} catch (err) {
if (err.code === 'ERR_STREAM_READ_NOT_IMPLEMENTED') {
console.error('ERR_STREAM_READ_NOT_IMPLEMENTED:', err.message)
} else {
throw err
}
}// Validate inputs before calling the operation
function safe_err_stream_read_not_implemented(...args) {
// validate args here
return performOperation(...args)
}✕ Subclass Readable without implementing _read()
_read() is abstract; the base class implementation explicitly throws this error.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev