node:readline
Node-compatible line-by-line readable stream interface and cursor helpers
(node:readline).
Available in vjs only (not available in vjs-lite). Enabled by default
unless --no-node-compat is passed.
import ... from "node:readline"
node-compat (enabled by default in vjs; absent in vjs-lite; disable with --no-node-compat)Example
Section titled “Example”import { createInterface } from "node:readline";
const rl = createInterface({ input: someReadableStream });for await (const line of rl) { console.log(line);}clearLinevjs onlyv0.0.0-alpha.2stream in direction dir.clearScreenDownvjs onlyv0.0.0-alpha.2stream from the cursor position downward.createInterfacevjs onlyv0.0.0-alpha.2readline.Interface.cursorTovjs onlyv0.0.0-alpha.2x and optional row y on stream.emitKeypressEventsvjs onlyv0.0.0-alpha.2stream to emit "keypress" events for decoded input sequences.moveCursorvjs onlyv0.0.0-alpha.2(dx, dy) on stream.AbortSignalLikevjs onlyv0.0.0-alpha.2readline options and question().ReadlineInputStreamvjs onlyv0.0.0-alpha.2createInterface().ReadlineOutputStreamvjs onlyv0.0.0-alpha.2createInterface() and cursor helpers.Keyvjs onlyv0.0.0-alpha.2"keypress" listeners and write().CursorPosvjs onlyv0.0.0-alpha.2Interface.getCursorPos().ReadLineOptionsvjs onlyv0.0.0-alpha.2createInterface(options).ReadlinePromisesNamespacevjs onlyv0.0.0-alpha.2node:readline/promises).Functions
Section titled “Functions”clearLine
Section titled “clearLine”export function clearLine(
stream: ReadlineOutputStream,
dir: Direction,
callback?: () => void,
): boolean;
Clear the current line of stream in direction dir.
| Name | Type | Description |
|---|---|---|
stream | ReadlineOutputStream | Target writable TTY stream. |
dir | Direction | -1 (left), 0 (entire line), or 1 (right). |
callback optional | () => void | Optional completion callback. |
boolean — false if stream backpressure is active, otherwise true.clearScreenDown
Section titled “clearScreenDown”export function clearScreenDown(
stream: ReadlineOutputStream,
callback?: () => void,
): boolean;
Clear the screen of stream from the cursor position downward.
| Name | Type | Description |
|---|---|---|
stream | ReadlineOutputStream | Target writable TTY stream. |
callback optional | () => void | Optional completion callback. |
boolean — false if stream backpressure is active, otherwise true.createInterface
Section titled “createInterface”export function createInterface(
input: ReadlineInputStream,
output?: ReadlineOutputStream,
completer?: Completer,
terminal?: boolean,
): Interface;
export function createInterface(options: ReadLineOptions): Interface;
Create a new callback-based readline.Interface.
createInterface(input, output?, completer?, terminal?)| Name | Type | Description |
|---|---|---|
input | ReadlineInputStream | Readable stream to consume. |
output optional | ReadlineOutputStream | Optional writable stream. |
completer optional | Completer | Optional tab-completion callback. |
terminal optional | boolean | Treat streams as TTY when true. |
Interface — New Interface instance.createInterface(options)Create a new callback-based readline.Interface from options.
| Name | Type | Description |
|---|---|---|
options | ReadLineOptions | Interface configuration. |
Interface — New Interface instance.cursorTo
Section titled “cursorTo”export function cursorTo(
stream: ReadlineOutputStream,
x: number,
y?: number,
callback?: () => void,
): boolean;
Move the cursor to column x and optional row y on stream.
| Name | Type | Description |
|---|---|---|
stream | ReadlineOutputStream | Target writable TTY stream. |
x | number | Zero-based column coordinate. |
y optional | number | Optional zero-based row coordinate. |
callback optional | () => void | Optional completion callback. |
boolean — false if stream backpressure is active, otherwise true.emitKeypressEvents
Section titled “emitKeypressEvents”export function emitKeypressEvents(
stream: ReadlineInputStream,
readlineInterface?: Interface,
): void;
Cause stream to emit "keypress" events for decoded input sequences.
| Name | Type | Description |
|---|---|---|
stream | ReadlineInputStream | Readable input stream. |
readlineInterface optional | Interface | Optional associated Interface. |
voidmoveCursor
Section titled “moveCursor”export function moveCursor(
stream: ReadlineOutputStream,
dx: number,
dy: number,
callback?: () => void,
): boolean;
Move the cursor relative to its current position by (dx, dy) on stream.
| Name | Type | Description |
|---|---|---|
stream | ReadlineOutputStream | Target writable TTY stream. |
dx | number | Horizontal column delta. |
dy | number | Vertical row delta. |
callback optional | () => void | Optional completion callback. |
boolean — false if stream backpressure is active, otherwise true.Classes
Section titled “Classes”Interface
Section titled “Interface”export class Interface {
readonly terminal: boolean;
readonly line: string;
readonly cursor: number;
getPrompt(): string;
setPrompt(prompt: string): void;
prompt(preserveCursor?: boolean): void;
question(query: string, callback: (answer: string) => void): void;
question(
query: string,
options: { signal?: AbortSignalLike },
callback: (answer: string) => void,
): void;
pause(): this;
resume(): this;
close(): void;
write(data: string | Uint8Array | null, key?: Key): void;
getCursorPos(): CursorPos;
on(eventName: string | symbol, listener: (...args: any[]) => void): this;
once(eventName: string | symbol, listener: (...args: any[]) => void): this;
off(eventName: string | symbol, listener: (...args: any[]) => void): this;
emit(eventName: string | symbol, ...args: any[]): boolean;
[Symbol.asyncIterator](): AsyncIterableIterator<string>;
[Symbol.dispose](): void;
}
Line-reading interface returned by createInterface().
| Property | Type | Description | Since |
|---|---|---|---|
terminal readonly | boolean | true if the interface is operating in TTY mode. | v0.0.0-alpha.2 |
line readonly | string | Current buffered line text. | v0.0.0-alpha.2 |
cursor readonly | number | Current cursor offset within line. | v0.0.0-alpha.2 |
Interface.getPrompt
Section titled “Interface.getPrompt”getPrompt(): string;
Return the current prompt string.
stringInterface.setPrompt
Section titled “Interface.setPrompt”setPrompt(prompt: string): void;
Set the prompt string rendered by prompt().
| Name | Type | Description |
|---|---|---|
prompt | string | — |
voidInterface.prompt
Section titled “Interface.prompt”prompt(preserveCursor?: boolean): void;
Write the configured prompt to output and await user input.
| Name | Type | Description |
|---|---|---|
preserveCursor optional | boolean | — |
voidInterface.question
Section titled “Interface.question”question(query: string, callback: (answer: string) => void): void;
question(
query: string,
options: { signal?: AbortSignalLike },
callback: (answer: string) => void,
): void;
Display query as a prompt and pass the user’s answer line to callback.
question(query, callback)| Name | Type | Description |
|---|---|---|
query | string | — |
callback | (answer: string) => void | — |
voidquestion(query, options, callback)Display query with abort options and pass the answer line to callback.
| Name | Type | Description |
|---|---|---|
query | string | — |
options | { signal?: AbortSignalLike } | — |
callback | (answer: string) => void | — |
voidInterface.pause
Section titled “Interface.pause”pause(): this;
Pause reading from input.
thisInterface.resume
Section titled “Interface.resume”resume(): this;
Resume reading from input.
thisInterface.close
Section titled “Interface.close”close(): void;
Close the interface and release ownership of input and output.
voidInterface.write
Section titled “Interface.write”write(data: string | Uint8Array | null, key?: Key): void;
Simulate writing data or key input to the interface.
| Name | Type | Description |
|---|---|---|
data | string | Uint8Array | null | — |
key optional | Key | — |
voidInterface.getCursorPos
Section titled “Interface.getCursorPos”getCursorPos(): CursorPos;
Return the current prompt cursor { rows, cols }.
Interface.on
Section titled “Interface.on”on(eventName: string | symbol, listener: (...args: any[]) => void): this;
Register an event listener ("line", "close", "pause", "resume", "SIGINT", etc.).
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
listener | (...args: any[]) => void | — |
thisInterface.once
Section titled “Interface.once”once(eventName: string | symbol, listener: (...args: any[]) => void): this;
Register a one-time event listener.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
listener | (...args: any[]) => void | — |
thisInterface.off
Section titled “Interface.off”off(eventName: string | symbol, listener: (...args: any[]) => void): this;
Remove an event listener.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
listener | (...args: any[]) => void | — |
thisInterface.emit
Section titled “Interface.emit”emit(eventName: string | symbol, ...args: any[]): boolean;
Synchronously emit an event.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
...args | any[] | — |
booleanInterface.[Symbol.asyncIterator]
Section titled “Interface.[Symbol.asyncIterator]”[Symbol.asyncIterator](): AsyncIterableIterator<string>;
Iterate asynchronously over input lines.
AsyncIterableIterator<string>Interface.[Symbol.dispose]
Section titled “Interface.[Symbol.dispose]”[Symbol.dispose](): void;
Close the interface when disposed via using.
voidInterfaces
Section titled “Interfaces”AbortSignalLike
Section titled “AbortSignalLike”export interface AbortSignalLike {
readonly aborted: boolean;
readonly reason?: unknown;
addEventListener(type: string, listener: (...args: any[]) => void, options?: { once?: boolean }): void;
removeEventListener?(type: string, listener: (...args: any[]) => void): void;
}
Minimal abort-signal contract accepted by readline options and question().
| Property | Type | Description | Since |
|---|---|---|---|
aborted readonly | boolean | Whether the signal has already been aborted. | v0.0.0-alpha.2 |
reason readonly optional | unknown | Optional abort reason value. | v0.0.0-alpha.2 |
AbortSignalLike.addEventListener
Section titled “AbortSignalLike.addEventListener”addEventListener(type: string, listener: (...args: any[]) => void, options?: { once?: boolean }): void;
Register an "abort" listener.
| Name | Type | Description |
|---|---|---|
type | string | — |
listener | (...args: any[]) => void | — |
options optional | { once?: boolean } | — |
voidAbortSignalLike.removeEventListener
Section titled “AbortSignalLike.removeEventListener”removeEventListener?(type: string, listener: (...args: any[]) => void): void;
Unregister an "abort" listener.
| Name | Type | Description |
|---|---|---|
type | string | — |
listener | (...args: any[]) => void | — |
voidReadlineInputStream
Section titled “ReadlineInputStream”export interface ReadlineInputStream {
on(eventName: string | symbol, listener: (...args: any[]) => void): unknown;
pause?(): unknown;
resume?(): unknown;
}
Minimal readable stream contract accepted by createInterface().
ReadlineInputStream.on
Section titled “ReadlineInputStream.on”on(eventName: string | symbol, listener: (...args: any[]) => void): unknown;
Register a stream event listener.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
listener | (...args: any[]) => void | — |
unknownReadlineInputStream.pause
Section titled “ReadlineInputStream.pause”pause?(): unknown;
Pause stream reading.
unknownReadlineInputStream.resume
Section titled “ReadlineInputStream.resume”resume?(): unknown;
Resume stream reading.
unknownReadlineOutputStream
Section titled “ReadlineOutputStream”export interface ReadlineOutputStream {
write(chunk: string | Uint8Array, callback?: () => void): boolean;
}
Minimal writable stream contract accepted by createInterface() and cursor helpers.
ReadlineOutputStream.write
Section titled “ReadlineOutputStream.write”write(chunk: string | Uint8Array, callback?: () => void): boolean;
Write text or bytes to the destination stream.
| Name | Type | Description |
|---|---|---|
chunk | string | Uint8Array | — |
callback optional | () => void | — |
booleanexport interface Key {
sequence?: string;
name?: string;
ctrl?: boolean;
meta?: boolean;
shift?: boolean;
}
Parsed keypress event metadata passed to "keypress" listeners and write().
| Property | Type | Description | Since |
|---|---|---|---|
sequence optional | string | Key sequence string. | v0.0.0-alpha.2 |
name optional | string | Normalized key name (for example "return", "backspace", "up"). | v0.0.0-alpha.2 |
ctrl optional | boolean | Whether Ctrl was held. | v0.0.0-alpha.2 |
meta optional | boolean | Whether Meta/Alt was held. | v0.0.0-alpha.2 |
shift optional | boolean | Whether Shift was held. | v0.0.0-alpha.2 |
CursorPos
Section titled “CursorPos”export interface CursorPos {
rows: number;
cols: number;
}
Cursor row and column coordinates returned by Interface.getCursorPos().
| Property | Type | Description | Since |
|---|---|---|---|
rows | number | Zero-based row position relative to the prompt. | v0.0.0-alpha.2 |
cols | number | Zero-based column position relative to the prompt. | v0.0.0-alpha.2 |
ReadLineOptions
Section titled “ReadLineOptions”export interface ReadLineOptions {
input: ReadlineInputStream;
output?: ReadlineOutputStream;
completer?: Completer;
terminal?: boolean;
history?: string[];
historySize?: number;
removeHistoryDuplicates?: boolean;
prompt?: string;
crlfDelay?: number;
escapeCodeTimeout?: number;
tabSize?: number;
signal?: AbortSignalLike;
}
Options accepted by createInterface(options).
| Property | Type | Description | Since |
|---|---|---|---|
input | ReadlineInputStream | Readable stream to consume line-by-line. | v0.0.0-alpha.2 |
output optional | ReadlineOutputStream | Optional writable stream for prompts and echoed input. | v0.0.0-alpha.2 |
completer optional | Completer | Optional tab-completion callback. | v0.0.0-alpha.2 |
terminal optional | boolean | Treat input/output as a TTY when true. | v0.0.0-alpha.2 |
history optional | string[] | Initial history entries (most recent first). | v0.0.0-alpha.2 |
historySize optional | number | Maximum number of retained history lines (default 30). | v0.0.0-alpha.2 |
removeHistoryDuplicates optional | boolean | Omit consecutive duplicate lines from history when true. | v0.0.0-alpha.2 |
prompt optional | string | Prompt string displayed by prompt() (default "> "). | v0.0.0-alpha.2 |
crlfDelay optional | number | Delay in milliseconds before processing standalone \r as a line ending. | v0.0.0-alpha.2 |
escapeCodeTimeout optional | number | Timeout in milliseconds for multi-byte escape sequences. | v0.0.0-alpha.2 |
tabSize optional | number | Number of spaces used for tab-completion formatting. | v0.0.0-alpha.2 |
signal optional | AbortSignalLike | Optional abort signal closing the interface when aborted. | v0.0.0-alpha.2 |
ReadlinePromisesNamespace
Section titled “ReadlinePromisesNamespace”export interface ReadlinePromisesNamespace {
Interface: typeof import("node:readline/promises").Interface;
Readline: typeof import("node:readline/promises").Readline;
createInterface: typeof import("node:readline/promises").createInterface;
}
Promise-based readline API (node:readline/promises).
| Property | Type | Description | Since |
|---|---|---|---|
Interface | typeof import("node:readline/promises").Interface | Promise-based Interface class. | v0.0.0-alpha.2 |
Readline | typeof import("node:readline/promises").Readline | Queued ANSI cursor/line helper class. | v0.0.0-alpha.2 |
createInterface | typeof import("node:readline/promises").createInterface | Create a promise-based Interface. | v0.0.0-alpha.2 |
Type Aliases
Section titled “Type Aliases”Direction
Section titled “Direction”export type Direction = -1 | 0 | 1;
Direction argument accepted by clearLine() (-1 left, 0 full line, 1 right).
Completer
Section titled “Completer”export type Completer = (
line: string,
callback?: (err?: Error | null, result?: [string[], string]) => void,
) => [string[], string] | void;
Tab-completion callback signature returning [completions, substring].
Constants
Section titled “Constants”promises
Section titled “promises”export const promises: ReadlinePromisesNamespace;
Promise-based readline API (node:readline/promises).
Default Export
Section titled “Default Export”default export
Section titled “default export”const _default: {
/** `Interface` constructor. @since 0.0.0-alpha.2 */
Interface: typeof Interface;
/** Clear line helper. @since 0.0.0-alpha.2 */
clearLine: typeof clearLine;
/** Clear screen downward helper. @since 0.0.0-alpha.2 */
clearScreenDown: typeof clearScreenDown;
/** Create a `readline.Interface`. @since 0.0.0-alpha.2 */
createInterface: typeof createInterface;
/** Move cursor to absolute position. @since 0.0.0-alpha.2 */
cursorTo: typeof cursorTo;
/** Enable `"keypress"` events on a stream. @since 0.0.0-alpha.2 */
emitKeypressEvents: typeof emitKeypressEvents;
/** Move cursor relatively. @since 0.0.0-alpha.2 */
moveCursor: typeof moveCursor;
/** Promise-based readline API (`node:readline/promises`). @since 0.0.0-alpha.2 */
readonly promises: ReadlinePromisesNamespace;
};
export default _default;
Default export object providing the node:readline API.
