Full answer
By default a stream moves bytes. Writing a plain object to a normal Writable throws ERR_INVALID_ARG_TYPE. Object mode removes that limit.
Facts to remember:
- Set objectMode: true on a Readable, Writable, Transform or Duplex. A Transform can use readableObjectMode and writableObjectMode separately, for example bytes in and objects out (a parser) or objects in and bytes out (a serializer).
- highWaterMark is a count of objects, default 16, instead of 64 KB of bytes. Backpressure still works, but it counts items, not sizes. A few huge objects can still use a lot of memory.
- null is the end marker, so it cannot be pushed as data. undefined is allowed in some cases but should be avoided.
- Readable.from(iterable) is the easiest source. Arrays, sync generators and async generators work. By default the result is in object mode. A string passed directly is one chunk, not one character per chunk.
- Async iteration with for await...of is the simplest consumer.
Typical uses: database rows, parsed CSV or JSON lines, events from a message queue, and any stage where a record is the unit of work.
Costs: each object passes through the stream machinery, so object streams are slower per byte than byte streams. For hot paths, batch records into arrays (for example 500 rows at a time).
const { Readable, Writable, Transform } = require('node:stream');
console.log('byte stream highWaterMark:', new Readable().readableHighWaterMark);
console.log('object mode highWaterMark:', new Readable({ objectMode: true }).readableHighWaterMark);
// A normal Writable refuses objects.
const bytesOnly = new Writable({ write(chunk, enc, cb) { cb(); } });
bytesOnly.on('error', () => {});
try { bytesOnly.write({ id: 1 }); } catch (err) { console.log('object into byte stream ->', err.code); }
// Readable.from accepts arrays and generators and uses object mode.
function* students() {
yield { name: 'Asha', marks: 78 };
yield { name: 'Ravi', marks: 64 };
yield { name: 'Meera', marks: 91 };
}
const source = Readable.from(students());
console.log('Readable.from objectMode:', source.readableObjectMode);
// Bytes in, objects out: a tiny parser for lines like "name,marks".
const parse = new Transform({
readableObjectMode: true,
transform(chunk, enc, cb) {
for (const line of chunk.toString().split('\n').filter(Boolean)) {
const [name, marks] = line.split(',');
this.push({ name, marks: Number(marks) });
}
cb();
},
});
async function main() {
const best = [];
for await (const row of source) if (row.marks > 70) best.push(row.name);
console.log('marks above 70:', best.join(', '));
parse.end('Asha,78\nRavi,64\n');
const rows = [];
for await (const row of parse) rows.push(row);
console.log('parsed rows:', JSON.stringify(rows));
// A string is one chunk, not one chunk per character.
let count = 0;
for await (const piece of Readable.from('hello')) count++;
console.log('chunks from Readable.from("hello"):', count);
}
main();Readable.from is used with a generator, and a Transform turns text lines into objects. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): byte stream highWaterMark: 65536 object mode highWaterMark: 16 object into byte stream -> ERR_INVALID_ARG_TYPE Readable.from objectMode: true marks above 70: Asha, Meera parsed rows: [{"name":"Asha","marks":78},{"name":"Ravi","marks":64}] chunks from Readable.from("hello"): 1
Key points
- Object mode streams carry any value except null.
- highWaterMark counts objects (default 16), not bytes.
- Transform can mix byte input and object output.
- Readable.from(iterable) creates an object-mode readable.
- Batch records when per-object overhead matters.
Common mistakes
- Pushing null to mean 'no value'. It ends the stream.
- Assuming highWaterMark is in bytes in object mode.
- Forgetting objectMode on one side of a Transform that needs it.
- Using object mode for large binary data.
- Expecting Readable.from('text') to emit characters.
Follow-ups
- How does backpressure work when objects have very different sizes?
- How do you batch objects before writing them to a database?
- What is the difference between readableObjectMode and writableObjectMode?
Tips
- Say that highWaterMark counts items.
- Show Readable.from with a generator.
- Mention batching for speed.