node:readline/promises
Node-compatible promise-based readline interface and buffered cursor helper
(node:readline/promises).
Available in vjs only (not available in vjs-lite). Enabled by default
unless --no-node-compat is passed.
import ... from "node:readline/promises"
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/promises";
const rl = createInterface({ input: someReadableStream });const answer = await rl.question("Continue? ");rl.close();Functions
Section titled “Functions”createInterface
Section titled “createInterface”export function createInterface(
input: import("node:readline").ReadlineInputStream,
output?: import("node:readline").ReadlineOutputStream,
completer?: AsyncCompleter,
terminal?: boolean,
): Interface;
export function createInterface(options: ReadLineOptions): Interface;
Create a new promise-based readline/promises.Interface.
createInterface(input, output?, completer?, terminal?)| Name | Type | Description |
|---|---|---|
input | import("node:readline").ReadlineInputStream | Readable stream to consume. |
output optional | import("node:readline").ReadlineOutputStream | Optional writable stream. |
completer optional | AsyncCompleter | Optional sync or async tab-completion callback. |
terminal optional | boolean | Treat streams as TTY when true. |
Interface — New promise-based Interface.createInterface(options)Create a new promise-based readline/promises.Interface from options.
| Name | Type | Description |
|---|---|---|
options | ReadLineOptions | Interface configuration. |
Interface — New promise-based Interface.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,
options?: { signal?: import("node:readline").AbortSignalLike },
): Promise<string>;
pause(): this;
resume(): this;
close(): void;
write(data: string | Uint8Array | null, key?: import("node:readline").Key): void;
getCursorPos(): import("node:readline").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;
}
Promise-based line-reading interface returned by readline/promises.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,
options?: { signal?: import("node:readline").AbortSignalLike },
): Promise<string>;
Display query as a prompt and resolve with the user’s answer line.
| Name | Type | Description |
|---|---|---|
query | string | Prompt text written to output. |
options optional | { signal?: import("node:readline").AbortSignalLike } | Optional abort signal configuration. |
Promise<string> — Promise resolving to the entered line string.Interface.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?: import("node:readline").Key): void;
Simulate writing data or key input to the interface.
| Name | Type | Description |
|---|---|---|
data | string | Uint8Array | null | — |
key optional | import("node:readline").Key | — |
voidInterface.getCursorPos
Section titled “Interface.getCursorPos”getCursorPos(): import("node:readline").CursorPos;
Return the current prompt cursor { rows, cols }.
import("node:readline").CursorPosInterface.on
Section titled “Interface.on”on(eventName: string | symbol, listener: (...args: any[]) => void): this;
Register an event listener.
| 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.
voidReadline
Section titled “Readline”export class Readline {
constructor(
stream: import("node:readline").ReadlineOutputStream,
options?: { autoCommit?: boolean },
);
clearLine(dir: import("node:readline").Direction): this;
clearScreenDown(): this;
commit(): Promise<void>;
cursorTo(x: number, y?: number): this;
moveCursor(dx: number, dy: number): this;
rollback(): this;
}
Buffered ANSI cursor and screen-clearing helper whose operations flush on commit().
constructor(
stream: import("node:readline").ReadlineOutputStream,
options?: { autoCommit?: boolean },
);
Construct a Readline command buffer bound to stream.
| Name | Type | Description |
|---|---|---|
stream | import("node:readline").ReadlineOutputStream | Target writable TTY stream. |
options optional | { autoCommit?: boolean } | Optional { autoCommit } configuration. |
Readline.clearLine
Section titled “Readline.clearLine”clearLine(dir: import("node:readline").Direction): this;
Queue clearing the current line in direction dir.
| Name | Type | Description |
|---|---|---|
dir | import("node:readline").Direction | — |
thisReadline.clearScreenDown
Section titled “Readline.clearScreenDown”clearScreenDown(): this;
Queue clearing the screen downward from the cursor.
thisReadline.commit
Section titled “Readline.commit”commit(): Promise<void>;
Flush all queued cursor and clear operations to stream.
Promise<void>Readline.cursorTo
Section titled “Readline.cursorTo”cursorTo(x: number, y?: number): this;
Queue moving the cursor to absolute coordinates (x, y).
| Name | Type | Description |
|---|---|---|
x | number | — |
y optional | number | — |
thisReadline.moveCursor
Section titled “Readline.moveCursor”moveCursor(dx: number, dy: number): this;
Queue moving the cursor relatively by (dx, dy).
| Name | Type | Description |
|---|---|---|
dx | number | — |
dy | number | — |
thisReadline.rollback
Section titled “Readline.rollback”rollback(): this;
Discard all uncommitted operations from the internal queue.
thisInterfaces
Section titled “Interfaces”ReadLineOptions
Section titled “ReadLineOptions”export interface ReadLineOptions {
input: import("node:readline").ReadlineInputStream;
output?: import("node:readline").ReadlineOutputStream;
completer?: AsyncCompleter;
terminal?: boolean;
history?: string[];
historySize?: number;
removeHistoryDuplicates?: boolean;
prompt?: string;
crlfDelay?: number;
escapeCodeTimeout?: number;
tabSize?: number;
signal?: import("node:readline").AbortSignalLike;
}
Options accepted by readline/promises.createInterface().
| Property | Type | Description | Since |
|---|---|---|---|
input | import("node:readline").ReadlineInputStream | Readable stream to consume line-by-line. | v0.0.0-alpha.2 |
output optional | import("node:readline").ReadlineOutputStream | Optional writable stream for prompts and echoed input. | v0.0.0-alpha.2 |
completer optional | AsyncCompleter | Optional sync or async 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 | import("node:readline").AbortSignalLike | Optional abort signal closing the interface when aborted. | v0.0.0-alpha.2 |
Type Aliases
Section titled “Type Aliases”AsyncCompleter
Section titled “AsyncCompleter”export type AsyncCompleter = (
line: string,
) => [string[], string] | Promise<[string[], string]>;
Tab-completion callback returning [completions, substring] synchronously
or via Promise.
Default Export
Section titled “Default Export”default export
Section titled “default export”const _default: {
/** Promise-based `Interface` constructor. @since 0.0.0-alpha.2 */
Interface: typeof Interface;
/** Buffered `Readline` cursor helper constructor. @since 0.0.0-alpha.2 */
Readline: typeof Readline;
/** Create a promise-based `Interface`. @since 0.0.0-alpha.2 */
createInterface: typeof createInterface;
};
export default _default;
Default export object providing the node:readline/promises API.
