Streams

readable.setEncoding()

Decodes byte chunks into strings while respecting multibyte character boundaries. Set an encoding when consuming text; leave data as buffers for binary protocols or exact byte counts. The example splits a UTF-8 character across two chunks. Tested on Node.js 22.16.0. Example output (one line per entry): ["é 1"].

Formal Syntax & Structure

nodejs syntaxnodejs
readable.setEncoding(encoding)

Parameters & Attributes

NameTypeDescription
encodingstringText encoding.

Return Value

Readable

The same readable instance.

Working Example

Example Snippetnodejs
import { Readable } from 'node:stream';const bytes = Buffer.from('é');const source = Readable.from([bytes.subarray(0, 1), bytes.subarray(1)], { objectMode: false });source.setEncoding('utf8');let text = '';for await (const chunk of source)    text += chunk;console.log(text, text.length);
Node.js Reference