Readline

readlinePromises.question()

Writes a question to the configured output and resolves with a line of input. Always close the interface; use an abort signal for cancellation. This fixture uses synthetic input and asks for no secrets. Tested on Node.js 22.16.0. Example output (one line per entry): ["4"].

Formal Syntax & Structure

nodejs syntaxnodejs
rl.question(query[, options])

Parameters & Attributes

NameTypeDescription
querystringPrompt.
optionsobjectOptional AbortSignal.

Return Value

Promise<string>

Answer.

Working Example

Example Snippetnodejs
import { createInterface } from 'node:readline/promises';import { PassThrough } from 'node:stream';const input = new PassThrough(), output = new PassThrough();output.resume();const rl = createInterface({ input, output, terminal: false });try {    const answer = rl.question('Quantity: ');    input.end('4\n');    console.log(await answer);}finally {    rl.close();    input.destroy();    output.destroy();}
Node.js Reference