Node.js Reference ยท Streams and readline
Node.js rl.question() method
Reference for rl.question() in Node.js readline: print a prompt, wait for one line, use callbacks or promises, abort with a signal and ask several questions.
rl.question(query, callback) writes query to the interface's output, waits for the user to type a line and passes that line to callback. It is the simplest way to build an interactive command-line prompt: ask, get a string back, continue.
The callback form lives on node:readline; the promise form lives on node:readline/promises, where await rl.question(...) returns the answer directly and fits naturally into sequential code. While a question is pending, the usual 'line' event is not emitted for that answer: it goes to the question callback instead.
import readline from 'node:readline'; const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.question('Your name? ', (name) => { console.log(`Hello, ${name}`); rl.close(); });
Try it
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.
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
rl.question(query[, options], callback): void // node:readline await rl.question(query[, options]): Promise<string> // node:readline/promises
| Name | Description |
|---|---|
query |
The prompt text written to output before waiting. |
options.signal |
An AbortSignal that cancels the pending question. |
callback |
Called once with the typed line (a string, without the line ending). |
Returns: nothing for the callback form; a promise of the answer for the promise form.
Errors: in node:readline/promises, an aborted signal rejects with AbortError, and calling question() after close() rejects with ERR_USE_AFTER_CLOSE. With node:readline, asking on a closed interface throws ERR_USE_AFTER_CLOSE.
Usage notes
- Always close. Call
rl.close()when you are done; an open interface onprocess.stdinkeeps the process alive. - Piped input arrives all at once. When stdin is a file or pipe, readline may read several lines before your second
question()is called, and those lines are emitted as'line'events that nobody hears. For scripted input, usefor awaitover the interface instead. - End of input. If the input ends (Ctrl+D, or a piped file that runs out) before the user answers, the question never resolves in the promise form; the interface closes instead. Listen for
'close', or pass a signal. - Several questions at once. Only one question can be pending; a second
question()call while the first is still waiting queues behind it in the callback API, but the cleaner pattern is to ask in sequence withawait. - Validation.
question()returns any text. Loop until the answer is acceptable, and convert numbers yourself. - Echo. The typed text is echoed only when
outputis a terminal. In a piped or test setup nothing is echoed. - Secret input. There is no password mode. Use raw mode on
process.stdinand mute output yourself for secrets. - Timeouts. Combine with
AbortSignal.timeout(ms)to give up after a delay.