Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท Streams and readline

Node.js Readable.from() method

Reference for Readable.from() in Node.js: create a readable stream from arrays, generators and async iterables, with object mode, strings and error behavior.

Readable.from(iterable, options) creates a readable stream from any iterable or async iterable: an array, a Set, a generator, an async generator, or an object with Symbol.asyncIterator. Each value the iterable produces becomes one chunk of the stream.

It is the shortest way to put data into a stream pipeline, to turn a generator into something that works with pipeline(), or to feed test data to a transform. By default the stream is in object mode, so chunks can be any JavaScript value and are not concatenated or split.

NODE
import { Readable } from 'node:stream';

const stream = Readable.from(['a', 'b', 'c']);

Try it

Node.js From an array and from an async generator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Strings, buffers and object mode

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Errors from the iterable

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
Readable.from(iterable[, options]): Readable
Name Description
iterable An iterable or async iterable, or a string or Buffer.
options Readable options. objectMode defaults to true; highWaterMark defaults to 1 object.

Returns: a new Readable.

Errors: a non-iterable argument throws ERR_INVALID_ARG_TYPE synchronously. An exception thrown by the iterable destroys the stream and is emitted as 'error'.

Usage notes

  • A string or Buffer is not iterated: it becomes a single chunk. Use [...str] or an array of pieces if you want several chunks.
  • With objectMode: false, make the iterable yield strings or buffers (as the second example does); other values are not valid chunks for a byte stream.
  • The iterable is pulled lazily, one item per read(), so infinite generators are safe as long as the consumer stops and the stream is destroyed (leaving a for await loop does that).
  • Destroying the stream calls the iterator's return(), so finally blocks in generators run.
  • For a stream that must be written to later, use PassThrough; for custom pull logic with state, use new Readable({ read() {} }).
  • The reverse operation is simple: any readable is async iterable, so for await or readable.toArray() collects it.
  • Added in Node.js 12.3 and 10.17.