Usage in Deno
import * as mod from "node:readline";
The node:readline
module provides an interface for reading data from a Readable
stream (such as process.stdin
) one line at a time.
To use the promise-based APIs:
import * as readline from 'node:readline/promises';
To use the callback and sync APIs:
import * as readline from 'node:readline';
The following simple example illustrates the basic use of the node:readline
module.
import * as readline from 'node:readline/promises'; import { stdin as input, stdout as output } from 'node:process'; const rl = readline.createInterface({ input, output }); const answer = await rl.question('What do you think of Node.js? '); console.log(`Thank you for your valuable feedback: ${answer}`); rl.close();
Once this code is invoked, the Node.js application will not terminate until thereadline.Interface
is closed because the interface waits for data to be
received on the input
stream.
Instances of the readline.Interface
class are constructed using thereadline.createInterface()
method. Every instance is associated with a
single input
Readable
stream and a single output
Writable
stream.
The output
stream is used to print prompts for user input that arrives on,
and is read from, the input
stream.
Instances of the readlinePromises.Interface
class are constructed using thereadlinePromises.createInterface()
method. Every instance is associated with a
single input
Readable
stream and a single output
Writable
stream.
The output
stream is used to print prompts for user input that arrives on,
and is read from, the input
stream.
The readline.clearLine()
method clears current line of given TTY
stream
in a specified direction identified by dir
.
The readline.clearScreenDown()
method clears the given TTY
stream from
the current position of the cursor down.
The readline.createInterface()
method creates a new readline.Interface
instance.
The readline.cursorTo()
method moves cursor to the specified position in a
given TTY
stream
.
The readline.emitKeypressEvents()
method causes the given Readable
stream to begin emitting 'keypress'
events corresponding to received input.
The readline.moveCursor()
method moves the cursor relative to its current
position in a given TTY
stream
.
The readlinePromises.createInterface()
method creates a new readlinePromises.Interface
instance.