TTY#

Stability: 2 - Stable

The node:tty module provides the tty.ReadStream and tty.WriteStream classes. In most cases, it will not be necessary or possible to use this module directly. However, it can be accessed using:

const tty = require('node:tty');
js

When Node.js detects that it is being run with a text terminal ("TTY") attached, process.stdin will, by default, be initialized as an instance of tty.ReadStream and both process.stdout and process.stderr will, by default, be instances of tty.WriteStream. The preferred method of determining whether Node.js is being run within a TTY context is to check that the value of the process.stdout.isTTY property is true:

$ node -p -e "Boolean(process.stdout.isTTY)"
true
$ node -p -e "Boolean(process.stdout.isTTY)" | cat
false
console

In most cases, there should be little to no reason for an application to manually create instances of the tty.ReadStream and tty.WriteStream classes.

Class: tty.ReadStream#

Represents the readable side of a TTY. In normal circumstances process.stdin will be the only tty.ReadStream instance in a Node.js process and there should be no reason to create additional instances.

readStream.isRaw#

A boolean that is true if the TTY is currently configured to operate as a raw device.

This flag is always false when a process starts, even if the terminal is operating in raw mode. Its value will change with subsequent calls to setRawMode.

readStream.isTTY#

A boolean that is always true for tty.ReadStream instances.

readStream.setRawMode(mode)#

  • mode <boolean> | <string> If true or 'raw', configures the tty.ReadStream to operate as a raw device. If 'io', configures the tty.ReadStream to operate in binary-safe I/O mode. If false, configures the tty.ReadStream to operate in its default mode. The readStream.isRaw property will be set to whether the stream is in raw mode, and the readStream.rawMode property will be set to the resulting mode.
  • Returns: <this> The read stream instance.

Allows configuration of tty.ReadStream so that it operates as a raw device.

When in raw mode, input is always available character-by-character, not including modifiers. Additionally, all special processing of input characters by the terminal is disabled, including echoing input characters. Ctrl+C will no longer cause a SIGINT when in this mode. This mode does not affect terminal output processing, such as newline translation on Unix terminals.

On Windows, setRawMode() requires write permission to the console input buffer. When opening "\\\\.\\CONIN$" with the fs.open() family of APIs (for passing into new tty.ReadStream()), be sure to use a read/write flag such as 'r+'.

When in binary-safe I/O mode, terminal output processing is also disabled. This corresponds to libuv's UV_TTY_MODE_IO mode and is not supported on Windows.

readStream.rawMode#

The current raw mode for the