stream.Writable

A destination you write data to chunk by chunk.

Since Node 0.x Spec ↗

Syntax

new Writable({ write(chunk, enc, cb) {} })

Returns

Writable — A writable stream with write() and end().

Examples

import { Writable } from 'node:stream';

const sink = new Writable({
  write(chunk, enc, cb) {
    console.log('->', chunk.toString());
    cb();
  },
});
sink.write('one');
sink.end('two');
Output
-> one
-> two
import { Writable } from 'node:stream';

const lines = [];
const collector = new Writable({
  write(chunk, _e, cb) { lines.push(chunk.toString()); cb(); },
});
collector.on('finish', () => console.log(lines.join('|')));
collector.end('done');
Output
done

Notes

Always call the `write` callback (`cb`) when done with a chunk; forgetting it stalls the stream. Pass an error to `cb(err)` to fail the stream. The `'finish'` event fires after `end()` flushes.

See also