Skip to content
VaelJS Blog

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"
Since v0.0.0-alpha.2 vjs (V8)
Cargo feature: node-compat (enabled by default in vjs; absent in vjs-lite; disable with --no-node-compat)
import { createInterface } from "node:readline/promises";
const rl = createInterface({ input: someReadableStream });
const answer = await rl.question("Continue? ");
rl.close();
functionSince v0.0.0-alpha.2vjs only
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.

Overload 1 — createInterface(input, output?, completer?, terminal?)
NameTypeDescription
inputimport("node:readline").ReadlineInputStreamReadable stream to consume.
output optionalimport("node:readline").ReadlineOutputStreamOptional writable stream.
completer optionalAsyncCompleterOptional sync or async tab-completion callback.
terminal optionalbooleanTreat streams as TTY when true.
Interface — New promise-based Interface.
Overload 2 — createInterface(options)

Create a new promise-based readline/promises.Interface from options.

NameTypeDescription
optionsReadLineOptionsInterface configuration.
Interface — New promise-based Interface.
classSince v0.0.0-alpha.2vjs only
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().

Properties
PropertyTypeDescriptionSince
terminal readonlybooleantrue if the interface is operating in TTY mode.v0.0.0-alpha.2
line readonlystringCurrent buffered line text.v0.0.0-alpha.2
cursor readonlynumberCurrent cursor offset within line.v0.0.0-alpha.2
Methods
methodSince v0.0.0-alpha.2
getPrompt(): string;

Return the current prompt string.

Returns
string
methodSince v0.0.0-alpha.2
setPrompt(prompt: string): void;

Set the prompt string rendered by prompt().

Parameters
NameTypeDescription
promptstring—
Returns
void
methodSince v0.0.0-alpha.2
prompt(preserveCursor?: boolean): void;

Write the configured prompt to output and await user input.

Parameters
NameTypeDescription
preserveCursor optionalboolean—
Returns
void
methodSince v0.0.0-alpha.2
question(
  query: string,
  options?: { signal?: import("node:readline").AbortSignalLike },
): Promise<string>;

Display query as a prompt and resolve with the user’s answer line.

Parameters
NameTypeDescription
querystringPrompt text written to output.
options optional{ signal?: import("node:readline").AbortSignalLike }Optional abort signal configuration.
Returns
Promise<string> — Promise resolving to the entered line string.
methodSince v0.0.0-alpha.2
pause(): this;

Pause reading from input.

Returns
this
methodSince v0.0.0-alpha.2
resume(): this;

Resume reading from input.

Returns
this
methodSince v0.0.0-alpha.2
close(): void;

Close the interface and release ownership of input and output.

Returns
void
methodSince v0.0.0-alpha.2
write(data: string | Uint8Array | null, key?: import("node:readline").Key): void;

Simulate writing data or key input to the interface.

Parameters
NameTypeDescription
datastring | Uint8Array | null—
key optionalimport("node:readline").Key—
Returns
void
methodSince v0.0.0-alpha.2
getCursorPos(): import("node:readline").CursorPos;

Return the current prompt cursor { rows, cols }.

Returns
import("node:readline").CursorPos
methodSince v0.0.0-alpha.2
on(eventName: string | symbol, listener: (...args: any[]) => void): this;

Register an event listener.

Parameters
NameTypeDescription
eventNamestring | symbol—
listener(...args: any[]) => void—
Returns
this
methodSince v0.0.0-alpha.2
once(eventName: string | symbol, listener: (...args: any[]) => void): this;

Register a one-time event listener.

Parameters
NameTypeDescription
eventNamestring | symbol—
listener(...args: any[]) => void—
Returns
this
methodSince v0.0.0-alpha.2
off(eventName: string | symbol, listener: (...args: any[]) => void): this;

Remove an event listener.

Parameters
NameTypeDescription
eventNamestring | symbol—
listener(...args: any[]) => void—
Returns
this
methodSince v0.0.0-alpha.2
emit(eventName: string | symbol, ...args: any[]): boolean;

Synchronously emit an event.

Parameters
NameTypeDescription
eventNamestring | symbol—
...argsany[]—
Returns
boolean
methodSince v0.0.0-alpha.2
[Symbol.asyncIterator](): AsyncIterableIterator<string>;

Iterate asynchronously over input lines.

Returns
methodSince v0.0.0-alpha.2
[Symbol.dispose](): void;

Close the interface when disposed via using.

Returns
void
classSince v0.0.0-alpha.2vjs only
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
constructor(
  stream: import("node:readline").ReadlineOutputStream,
  options?: { autoCommit?: boolean },
);

Construct a Readline command buffer bound to stream.

Parameters
NameTypeDescription
streamimport("node:readline").ReadlineOutputStreamTarget writable TTY stream.
options optional{ autoCommit?: boolean }Optional { autoCommit } configuration.
Methods
methodSince v0.0.0-alpha.2
clearLine(dir: import("node:readline").Direction): this;

Queue clearing the current line in direction dir.

Parameters
NameTypeDescription
dirimport("node:readline").Direction—
Returns
this
methodSince v0.0.0-alpha.2
clearScreenDown(): this;

Queue clearing the screen downward from the cursor.

Returns
this
methodSince v0.0.0-alpha.2
commit(): Promise<void>;

Flush all queued cursor and clear operations to stream.

Returns
Promise<void>
methodSince v0.0.0-alpha.2
cursorTo(x: number, y?: number): this;

Queue moving the cursor to absolute coordinates (x, y).

Parameters
NameTypeDescription
xnumber—
y optionalnumber—
Returns
this
methodSince v0.0.0-alpha.2
moveCursor(dx: number, dy: number): this;

Queue moving the cursor relatively by (dx, dy).

Parameters
NameTypeDescription
dxnumber—
dynumber—
Returns
this
methodSince v0.0.0-alpha.2
rollback(): this;

Discard all uncommitted operations from the internal queue.

Returns
this
interfaceSince v0.0.0-alpha.2vjs only
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().

Properties
PropertyTypeDescriptionSince
input import("node:readline").ReadlineInputStreamReadable stream to consume line-by-line.v0.0.0-alpha.2
output optionalimport("node:readline").ReadlineOutputStreamOptional writable stream for prompts and echoed input.v0.0.0-alpha.2
completer optionalAsyncCompleterOptional sync or async tab-completion callback.v0.0.0-alpha.2
terminal optionalbooleanTreat input/output as a TTY when true.v0.0.0-alpha.2
history optionalstring[]Initial history entries (most recent first).v0.0.0-alpha.2
historySize optionalnumberMaximum number of retained history lines (default 30).v0.0.0-alpha.2
removeHistoryDuplicates optionalbooleanOmit consecutive duplicate lines from history when true.v0.0.0-alpha.2
prompt optionalstringPrompt string displayed by prompt() (default "> ").v0.0.0-alpha.2
crlfDelay optionalnumberDelay in milliseconds before processing standalone \r as a line ending.v0.0.0-alpha.2
escapeCodeTimeout optionalnumberTimeout in milliseconds for multi-byte escape sequences.v0.0.0-alpha.2
tabSize optionalnumberNumber of spaces used for tab-completion formatting.v0.0.0-alpha.2
signal optionalimport("node:readline").AbortSignalLikeOptional abort signal closing the interface when aborted.v0.0.0-alpha.2
typeSince v0.0.0-alpha.2vjs only
export type AsyncCompleter = (
  line: string,
) => [string[], string] | Promise<[string[], string]>;

Tab-completion callback returning [completions, substring] synchronously or via Promise.

defaultSince v0.0.0-alpha.2vjs only
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.