Non-object-mode stream received null value
Production Risk
Low — caught immediately at call site; fix by using end() or object mode.
Thrown when null is passed to a Readable or Writable stream write/push in a non-object-mode stream in a context where null is not the intended EOF signal, or when the stream implementation misuses null. In Readable streams, push(null) is valid as EOF; but write(null) on a Writable is always invalid.
- 1Calling writable.write(null) on a non-object-mode stream
- 2Passing null as a chunk to a Transform stream
- 3Misunderstanding that null is the EOF signal for push(), not write()
Triggered when write() or push() receives null in a context where it is not treated as EOF.
const { Writable } = require('stream');
const w = new Writable({
write(chunk, enc, cb) { cb(); }
});
w.write(null); // throws — null is not a valid write() argumentexpected output
Error [ERR_STREAM_NULL_VALUES]: May not write null values to stream
Fix 1
Use end() instead of write(null) to finish a Writable
WHEN When signalling end of data to a Writable
w.write(Buffer.from('last chunk'));
w.end(); // correct way to end a WritableWhy this works
end() properly signals completion; write(null) is never valid for Writables.
Fix 2
Enable objectMode if you need to pass null as a value
WHEN When your stream legitimately handles null as a data value
const w = new Writable({
objectMode: true,
write(obj, enc, cb) { console.log(obj); cb(); }
});
w.write(null); // valid in objectModeWhy this works
In object mode, null written via write() is allowed as a plain object value.
const { Writable } = require('stream');
const w = new Writable({
write(chunk, enc, cb) { cb(); }
});
w.write(null); // throws — null is not a valid write() argument // this triggers ERR_STREAM_NULL_VALUEStry {
// operation that may throw ERR_STREAM_NULL_VALUES
riskyOperation()
} catch (err) {
if (err.code === 'ERR_STREAM_NULL_VALUES') {
console.error('ERR_STREAM_NULL_VALUES:', err.message)
} else {
throw err
}
}// Validate inputs before calling the operation
function safe_err_stream_null_values(...args) {
// validate args here
return performOperation(...args)
}✕ Pass null to write() on a byte/string stream
null is reserved as the EOF signal for push(); write(null) is always invalid.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev