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"
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.Example
Section titled “Example”import { isatty } from "node:tty";
console.log("stdout is TTY:", isatty(1));Functions
Section titled “Functions”isatty
Section titled “isatty”export function isatty(fd: number): boolean;
Return true if fd refers to a TTY terminal device, otherwise false.
| Name | Type | Description |
|---|---|---|
fd | number | Numeric file descriptor (for example 0, 1, or 2). |
boolean — true if fd is associated with a TTY, otherwise false.Classes
Section titled “Classes”ReadStream
Section titled “ReadStream”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(fd: number, options?: Record<string, unknown>);
Construct a TTY ReadStream for file descriptor fd.
| Name | Type | Description |
|---|---|---|
fd | number | Numeric file descriptor (for example 0). |
options optional | Record<string, unknown> | Optional stream configuration. |
| Property | Type | Description | Since |
|---|---|---|---|
isTTY readonly | boolean | Always true for a TTY ReadStream. | v0.0.0-alpha.2 |
isRaw readonly | boolean | Whether the TTY is currently configured in raw mode. | v0.0.0-alpha.2 |
readable readonly | boolean | Whether the stream is currently readable. | v0.0.0-alpha.2 |
readableEnded readonly | boolean | Whether "end" has been emitted. | v0.0.0-alpha.2 |
destroyed readonly | boolean | Whether destroy() has been called. | v0.0.0-alpha.2 |
ReadStream.setRawMode
Section titled “ReadStream.setRawMode”setRawMode(mode: boolean): this;
Enable or disable raw mode on the underlying terminal device.
| Name | Type | Description |
|---|---|---|
mode | boolean | true to enable raw mode, false to restore cooked mode. |
this — this for chaining.ReadStream.setEncoding
Section titled “ReadStream.setEncoding”setEncoding(encoding: string): this;
Set character encoding for emitted data chunks.
| Name | Type | Description |
|---|---|---|
encoding | string | — |
thisReadStream.read
Section titled “ReadStream.read”read(size?: number): any;
Pull buffered data from the stream.
| Name | Type | Description |
|---|---|---|
size optional | number | — |
anyReadStream.pause
Section titled “ReadStream.pause”pause(): this;
Pause "data" event emission.
thisReadStream.resume
Section titled “ReadStream.resume”resume(): this;
Resume "data" event emission.
thisReadStream.destroy
Section titled “ReadStream.destroy”destroy(error?: Error): this;
Destroy the stream.
| Name | Type | Description |
|---|---|---|
error optional | Error | — |
thisReadStream.ref
Section titled “ReadStream.ref”ref(): this;
Keep the event loop active while this stream is open.
thisReadStream.unref
Section titled “ReadStream.unref”unref(): this;
Allow the event loop to exit if this stream is the only active handle.
thisReadStream.on
Section titled “ReadStream.on”on(eventName: string | symbol, listener: (...args: any[]) => void): this;
Register an event listener.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
listener | (...args: any[]) => void | — |
thisReadStream.once
Section titled “ReadStream.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 | — |
thisReadStream.off
Section titled “ReadStream.off”off(eventName: string | symbol, listener: (...args: any[]) => void): this;
Remove an event listener.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
listener | (...args: any[]) => void | — |
thisReadStream.addListener
Section titled “ReadStream.addListener”addListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
Register an event listener.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
listener | (...args: any[]) => void | — |
thisReadStream.removeListener
Section titled “ReadStream.removeListener”removeListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
Remove an event listener.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
listener | (...args: any[]) => void | — |
thisReadStream.removeAllListeners
Section titled “ReadStream.removeAllListeners”removeAllListeners(eventName?: string | symbol): this;
Remove all listeners for eventName or all events.
| Name | Type | Description |
|---|---|---|
eventName optional | string | symbol | — |
thisReadStream.emit
Section titled “ReadStream.emit”emit(eventName: string | symbol, ...args: any[]): boolean;
Synchronously emit an event.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
...args | any[] | — |
booleanReadStream.[Symbol.asyncIterator]
Section titled “ReadStream.[Symbol.asyncIterator]”[Symbol.asyncIterator](): AsyncIterableIterator<any>;
Iterate asynchronously over input chunks.
WriteStream
Section titled “WriteStream”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(fd: number);
Construct a TTY WriteStream for file descriptor fd.
| Name | Type | Description |
|---|---|---|
fd | number | Numeric file descriptor (for example 1 or 2). |
| Property | Type | Description | Since |
|---|---|---|---|
isTTY readonly | boolean | Always true for a TTY WriteStream. | v0.0.0-alpha.2 |
columns readonly | number | Current terminal column count. | v0.0.0-alpha.2 |
rows readonly | number | Current terminal row count. | v0.0.0-alpha.2 |
writable readonly | boolean | Whether the stream is currently writable. | v0.0.0-alpha.2 |
destroyed readonly | boolean | Whether destroy() has been called. | v0.0.0-alpha.2 |
WriteStream.clearLine
Section titled “WriteStream.clearLine”clearLine(dir: Direction, callback?: () => void): boolean;
Clear the current line in the specified direction.
| Name | Type | Description |
|---|---|---|
dir | Direction | -1 (left), 0 (entire line), or 1 (right). |
callback optional | () => void | Optional completion callback. |
boolean — false if the stream asks the caller to wait for "drain", otherwise true.WriteStream.clearScreenDown
Section titled “WriteStream.clearScreenDown”clearScreenDown(callback?: () => void): boolean;
Clear the screen from the current cursor position downward.
| Name | Type | Description |
|---|---|---|
callback optional | () => void | Optional completion callback. |
boolean — false if backpressure is active, otherwise true.WriteStream.cursorTo
Section titled “WriteStream.cursorTo”cursorTo(x: number, y?: number | (() => void), callback?: () => void): boolean;
Move the cursor to absolute coordinates (x, y).
| Name | Type | Description |
|---|---|---|
x | number | Zero-based column index. |
y optional | number | (() => void) | Optional zero-based row index, or completion callback. |
callback optional | () => void | Optional completion callback. |
boolean — false if backpressure is active, otherwise true.WriteStream.moveCursor
Section titled “WriteStream.moveCursor”moveCursor(dx: number, dy: number, callback?: () => void): boolean;
Move the cursor relative to its current position by (dx, dy).
| Name | Type | Description |
|---|---|---|
dx | number | Horizontal cell delta. |
dy | number | Vertical row delta. |
callback optional | () => void | Optional completion callback. |
boolean — false if backpressure is active, otherwise true.WriteStream.getColorDepth
Section titled “WriteStream.getColorDepth”getColorDepth(env?: Record<string, string | undefined>): number;
Determine the bit depth of colors supported by this terminal (1, 4, 8, or 24).
| Name | Type | Description |
|---|---|---|
env optional | Record<string, string | undefined> | Optional environment variable override map. |
number — Color bit depth.WriteStream.hasColors
Section titled “WriteStream.hasColors”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).
hasColors(count?, env?)| Name | Type | Description |
|---|---|---|
count optional | number | Minimum color count or environment map. |
env optional | Record<string, string | undefined> | Optional environment variable override map. |
boolean — true if supported, otherwise false.hasColors(env?)Check whether the terminal supports colors using env.
| Name | Type | Description |
|---|---|---|
env optional | Record<string, string | undefined> | Environment variable override map. |
boolean — true if color output is supported, otherwise false.WriteStream.getWindowSize
Section titled “WriteStream.getWindowSize”getWindowSize(): [number, number];
Return [columns, rows] representing the current terminal dimensions.
[number, number] — Two-element [columns, rows] tuple.WriteStream.write
Section titled “WriteStream.write”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.
write(chunk, callback?)| Name | Type | Description |
|---|---|---|
chunk | string | Uint8Array | — |
callback optional | (error?: Error | null) => void | — |
booleanwrite(chunk, encoding, callback?)Write string data with explicit encoding to the TTY stream.
| Name | Type | Description |
|---|---|---|
chunk | string | — |
encoding | string | — |
callback optional | (error?: Error | null) => void | — |
booleanWriteStream.end
Section titled “WriteStream.end”end(callback?: () => void): this;
end(chunk: string | Uint8Array, callback?: () => void): this;
Finish writing to the stream.
end(callback?)| Name | Type | Description |
|---|---|---|
callback optional | () => void | — |
thisend(chunk, callback?)Write a final chunk and finish the stream.
| Name | Type | Description |
|---|---|---|
chunk | string | Uint8Array | — |
callback optional | () => void | — |
thisWriteStream.destroy
Section titled “WriteStream.destroy”destroy(error?: Error): this;
Destroy the stream.
| Name | Type | Description |
|---|---|---|
error optional | Error | — |
thisWriteStream.on
Section titled “WriteStream.on”on(eventName: string | symbol, listener: (...args: any[]) => void): this;
Register an event listener.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
listener | (...args: any[]) => void | — |
thisWriteStream.once
Section titled “WriteStream.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 | — |
thisWriteStream.off
Section titled “WriteStream.off”off(eventName: string | symbol, listener: (...args: any[]) => void): this;
Remove an event listener.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
listener | (...args: any[]) => void | — |
thisWriteStream.addListener
Section titled “WriteStream.addListener”addListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
Register an event listener.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
listener | (...args: any[]) => void | — |
thisWriteStream.removeListener
Section titled “WriteStream.removeListener”removeListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
Remove an event listener.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
listener | (...args: any[]) => void | — |
thisWriteStream.removeAllListeners
Section titled “WriteStream.removeAllListeners”removeAllListeners(eventName?: string | symbol): this;
Remove all listeners for eventName or all events.
| Name | Type | Description |
|---|---|---|
eventName optional | string | symbol | — |
thisWriteStream.emit
Section titled “WriteStream.emit”emit(eventName: string | symbol, ...args: any[]): boolean;
Synchronously emit an event.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
...args | any[] | — |
booleanType Aliases
Section titled “Type Aliases”Direction
Section titled “Direction”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).
Default Export
Section titled “Default Export”default export
Section titled “default export”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;
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.
