Skip to content
VaelJS Blog

node:tty

Node-compatible TTY detection and terminal stream classes (node:tty).

Available in vjs (vjs-lite exports isatty and default only, omitting ReadStream and WriteStream). Enabled by default unless --no-node-compat is passed.

import ... from "node:tty"
Since v0.0.0-alpha.2 vjs (V8) vjs-lite (QuickJS)
Cargo feature: node-compat (enabled by default; disable with --no-node-compat)
ReadStream and WriteStream classes are V8-only (vjs). isatty(fd) runs on both vjs and vjs-lite.
import { isatty } from "node:tty";
console.log("stdout is TTY:", isatty(1));
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function isatty(fd: number): boolean;

Return true if fd refers to a TTY terminal device, otherwise false.

Parameters
NameTypeDescription
fdnumberNumeric file descriptor (for example 0, 1, or 2).
Returns
boolean — true if fd is associated with a TTY, otherwise false.
classSince v0.0.0-alpha.2vjs only
export class ReadStream {
  readonly isTTY: boolean;
  readonly isRaw: boolean;
  readonly readable: boolean;
  readonly readableEnded: boolean;
  readonly destroyed: boolean;
  constructor(fd: number, options?: Record<string, unknown>);
  setRawMode(mode: boolean): this;
  setEncoding(encoding: string): this;
  read(size?: number): any;
  pause(): this;
  resume(): this;
  destroy(error?: Error): this;
  ref(): this;
  unref(): this;
  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;
  addListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
  removeListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
  removeAllListeners(eventName?: string | symbol): this;
  emit(eventName: string | symbol, ...args: any[]): boolean;
  [Symbol.asyncIterator](): AsyncIterableIterator<any>;
}

Readable stream wrapping a TTY file descriptor (vjs only).

Constructor
constructor(fd: number, options?: Record<string, unknown>);

Construct a TTY ReadStream for file descriptor fd.

Parameters
NameTypeDescription
fdnumberNumeric file descriptor (for example 0).
options optionalRecord<string, unknown>Optional stream configuration.
Properties
PropertyTypeDescriptionSince
isTTY readonlybooleanAlways true for a TTY ReadStream.v0.0.0-alpha.2
isRaw readonlybooleanWhether the TTY is currently configured in raw mode.v0.0.0-alpha.2
readable readonlybooleanWhether the stream is currently readable.v0.0.0-alpha.2
readableEnded readonlybooleanWhether "end" has been emitted.v0.0.0-alpha.2
destroyed readonlybooleanWhether destroy() has been called.v0.0.0-alpha.2
Methods
methodSince v0.0.0-alpha.2
setRawMode(mode: boolean): this;

Enable or disable raw mode on the underlying terminal device.

Parameters
NameTypeDescription
modebooleantrue to enable raw mode, false to restore cooked mode.
Returns
this — this for chaining.
methodSince v0.0.0-alpha.2
setEncoding(encoding: string): this;

Set character encoding for emitted data chunks.

Parameters
NameTypeDescription
encodingstring—
Returns
this
methodSince v0.0.0-alpha.2
read(size?: number): any;

Pull buffered data from the stream.

Parameters
NameTypeDescription
size optionalnumber—
Returns
any
methodSince v0.0.0-alpha.2
pause(): this;

Pause "data" event emission.

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

Resume "data" event emission.

Returns
this
methodSince v0.0.0-alpha.2
destroy(error?: Error): this;

Destroy the stream.

Parameters
NameTypeDescription
error optionalError—
Returns
this
methodSince v0.0.0-alpha.2
ref(): this;

Keep the event loop active while this stream is open.

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

Allow the event loop to exit if this stream is the only active handle.

Returns
this
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
addListener(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
removeListener(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
removeAllListeners(eventName?: string | symbol): this;

Remove all listeners for eventName or all events.

Parameters
NameTypeDescription
eventName optionalstring | symbol—
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<any>;

Iterate asynchronously over input chunks.

classSince v0.0.0-alpha.2vjs only
export class WriteStream {
  readonly isTTY: boolean;
  readonly columns: number;
  readonly rows: number;
  readonly writable: boolean;
  readonly destroyed: boolean;
  constructor(fd: number);
  clearLine(dir: Direction, callback?: () => void): boolean;
  clearScreenDown(callback?: () => void): boolean;
  cursorTo(x: number, y?: number | (() => void), callback?: () => void): boolean;
  moveCursor(dx: number, dy: number, callback?: () => void): boolean;
  getColorDepth(env?: Record<string, string | undefined>): number;
  hasColors(count?: number, env?: Record<string, string | undefined>): boolean;
  hasColors(env?: Record<string, string | undefined>): boolean;
  getWindowSize(): [number, number];
  write(chunk: string | Uint8Array, callback?: (error?: Error | null) => void): boolean;
  write(
      chunk: string,
      encoding: string,
      callback?: (error?: Error | null) => void,
    ): boolean;
  end(callback?: () => void): this;
  end(chunk: string | Uint8Array, callback?: () => void): this;
  destroy(error?: Error): this;
  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;
  addListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
  removeListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
  removeAllListeners(eventName?: string | symbol): this;
  emit(eventName: string | symbol, ...args: any[]): boolean;
}

Writable stream wrapping a TTY file descriptor (vjs only).

Constructor
constructor(fd: number);

Construct a TTY WriteStream for file descriptor fd.

Parameters
NameTypeDescription
fdnumberNumeric file descriptor (for example 1 or 2).
Properties
PropertyTypeDescriptionSince
isTTY readonlybooleanAlways true for a TTY WriteStream.v0.0.0-alpha.2
columns readonlynumberCurrent terminal column count.v0.0.0-alpha.2
rows readonlynumberCurrent terminal row count.v0.0.0-alpha.2
writable readonlybooleanWhether the stream is currently writable.v0.0.0-alpha.2
destroyed readonlybooleanWhether destroy() has been called.v0.0.0-alpha.2
Methods
methodSince v0.0.0-alpha.2
clearLine(dir: Direction, callback?: () => void): boolean;

Clear the current line in the specified direction.

Parameters
NameTypeDescription
dirDirection-1 (left), 0 (entire line), or 1 (right).
callback optional() => voidOptional completion callback.
Returns
boolean — false if the stream asks the caller to wait for "drain", otherwise true.
methodSince v0.0.0-alpha.2
clearScreenDown(callback?: () => void): boolean;

Clear the screen from the current cursor position downward.

Parameters
NameTypeDescription
callback optional() => voidOptional completion callback.
Returns
boolean — false if backpressure is active, otherwise true.
methodSince v0.0.0-alpha.2
cursorTo(x: number, y?: number | (() => void), callback?: () => void): boolean;

Move the cursor to absolute coordinates (x, y).

Parameters
NameTypeDescription
xnumberZero-based column index.
y optionalnumber | (() => void)Optional zero-based row index, or completion callback.
callback optional() => voidOptional completion callback.
Returns
boolean — false if backpressure is active, otherwise true.
methodSince v0.0.0-alpha.2
moveCursor(dx: number, dy: number, callback?: () => void): boolean;

Move the cursor relative to its current position by (dx, dy).

Parameters
NameTypeDescription
dxnumberHorizontal cell delta.
dynumberVertical row delta.
callback optional() => voidOptional completion callback.
Returns
boolean — false if backpressure is active, otherwise true.
methodSince v0.0.0-alpha.2
getColorDepth(env?: Record<string, string | undefined>): number;

Determine the bit depth of colors supported by this terminal (1, 4, 8, or 24).

Parameters
NameTypeDescription
env optionalRecord<string, string | undefined>Optional environment variable override map.
Returns
number — Color bit depth.
methodSince v0.0.0-alpha.2
hasColors(count?: number, env?: Record<string, string | undefined>): boolean;
hasColors(env?: Record<string, string | undefined>): boolean;

Check whether the terminal supports at least count colors (default 16).

Overload 1 — hasColors(count?, env?)
NameTypeDescription
count optionalnumberMinimum color count or environment map.
env optionalRecord<string, string | undefined>Optional environment variable override map.
boolean — true if supported, otherwise false.
Overload 2 — hasColors(env?)

Check whether the terminal supports colors using env.

NameTypeDescription
env optionalRecord<string, string | undefined>Environment variable override map.
boolean — true if color output is supported, otherwise false.
methodSince v0.0.0-alpha.2
getWindowSize(): [number, number];

Return [columns, rows] representing the current terminal dimensions.

Returns
[number, number] — Two-element [columns, rows] tuple.
methodSince v0.0.0-alpha.2
write(chunk: string | Uint8Array, callback?: (error?: Error | null) => void): boolean;
write(
  chunk: string,
  encoding: string,
  callback?: (error?: Error | null) => void,
): boolean;

Write data to the TTY stream.

Overload 1 — write(chunk, callback?)
NameTypeDescription
chunkstring | Uint8Array—
callback optional(error?: Error | null) => void—
boolean
Overload 2 — write(chunk, encoding, callback?)

Write string data with explicit encoding to the TTY stream.

NameTypeDescription
chunkstring—
encodingstring—
callback optional(error?: Error | null) => void—
boolean
methodSince v0.0.0-alpha.2
end(callback?: () => void): this;
end(chunk: string | Uint8Array, callback?: () => void): this;

Finish writing to the stream.

Overload 1 — end(callback?)
NameTypeDescription
callback optional() => void—
this
Overload 2 — end(chunk, callback?)

Write a final chunk and finish the stream.

NameTypeDescription
chunkstring | Uint8Array—
callback optional() => void—
this
methodSince v0.0.0-alpha.2
destroy(error?: Error): this;

Destroy the stream.

Parameters
NameTypeDescription
error optionalError—
Returns
this
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
addListener(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
removeListener(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
removeAllListeners(eventName?: string | symbol): this;

Remove all listeners for eventName or all events.

Parameters
NameTypeDescription
eventName optionalstring | symbol—
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
typeSince v0.0.0-alpha.2vjs only
export type Direction = -1 | 0 | 1;

Line-clearing direction argument for WriteStream.clearLine(): -1 (left of cursor), 0 (entire line), or 1 (right of cursor).

defaultSince v0.0.0-alpha.2vjs & vjs-lite
const _default: {
  /** Return `true` if `fd` is a TTY. @since 0.0.0-alpha.2 */
  isatty: typeof isatty;
  /** TTY `ReadStream` constructor. @since 0.0.0-alpha.2 */
  ReadStream: typeof ReadStream;
  /** TTY `WriteStream` constructor. @since 0.0.0-alpha.2 */
  WriteStream: typeof WriteStream;
};
export default _default;
vjs-lite parity: ReadStream, WriteStream are available in vjs (V8) only and omitted from the default export in vjs-lite.

Default export object providing the node:tty API.