Course contentsShow
← JavaScript
Lesson 1568 of 2,72334. Streams and BuffersFree lesson

Object Mode in Transform Streams

Processing JavaScript objects instead of buffers for structured data transformations.

Object Mode in Transform Streams

You'll learn how to process JavaScript objects directly through Transform streams instead of being limited to raw bytes.

What Is Object Mode?

By default, Node.js streams work with buffers or strings — raw chunks of data. But what if you want to process structured data like JSON objects, user records, or parsed CSV rows? That's where Object Mode comes in.

Object Mode lets your Transform stream accept and emit any JavaScript value (objects, arrays, numbers, etc.) instead of just Buffer or string chunks. This is perfect when you're transforming structured data rather than raw bytes.

How to Enable It

Set objectMode: true in your stream options:

const { Transform } = require('stream');

const upperCaseTransform = new Transform({
  objectMode: true,
  transform(record, encoding, callback) {
    // 'record' is now a JavaScript object, not a Buffer
    const transformed = {
      ...record,
      name: record.name.toUpperCase()
    };
    this.push(transformed);
    callback();
  }
});

Real-World Analogy

Think of a regular stream as a pipe moving water (raw bytes). Object Mode is like a conveyor belt carrying packages (structured items). Instead of processing one drop at a time, you handle complete, labeled boxes.

Example Flow

// Input: stream of user objects
{ name: 'alice', age: 30 }
{ name: 'bob', age: 25 }

// Transform: capitalize names
{ name: 'ALICE', age: 30 }
{ name: 'BOB', age: 25 }

Without Object Mode, you'd have to manually serialize/deserialize JSON between transforms. Object Mode handles structured data natively.

Key Takeaway: Object Mode allows Transform streams to process JavaScript objects directly, making data transformation pipelines cleaner and more intuitive for structured data.