Node.js TTY Module

Node.js TTY Module

The Node.js TTY (Teletype) module provides an interface for controlling and interacting with terminal-based input and output. It includes two important classes — tty.ReadStream and tty.WriteStream. While in most cases developers don’t need to use this module directly, understanding how it works is helpful when building CLI (Command Line Interface) applications or interactive terminal tools.

Introduction to Applied AI:Click Here

Accessing the TTY Module

To use the TTY module, include it in your project using the require() method:

var tty = require('tty');

When Node.js runs inside a TTY context, the following happens automatically:

  • process.stdin becomes an instance of tty.ReadStream
  • process.stdout becomes an instance of tty.WriteStream

Data Science Tutorial:-Click Here

You can check whether your Node.js process is running inside a TTY environment using this command:

node -p -e "Boolean(process.stdout.isTTY)"

If the output is true, it means your Node.js process is connected to a terminal.

Class: ReadStream

The ReadStream class is a subclass of net.Socket and represents the readable portion of a TTY. In most cases, process.stdin is the only instance of tty.ReadStream when isatty(0) returns true.

Key Properties and Methods

Download New Real Time Projects :–Click here

  • rs.isRaw:
    A Boolean property initialized to false. It indicates whether the TTY stream is currently in raw mode.
  • rs.setRawMode(mode):
    Accepts a Boolean value (true or false). When set to true, it enables raw mode — meaning input data is read byte-by-byte without line buffering or interpretation. This is commonly used for reading keypresses directly.

Example:

process.stdin.setRawMode(true);

Class: WriteStream

The WriteStream class, also a subclass of net.Socket, represents the writable portion of a TTY. Typically, process.stdout is the only instance of tty.WriteStream when isatty(1) returns true.

Machine Learning Tutorial:–Click Here

The resize Event

A resize event is emitted whenever the terminal window size changes — that is, when the number of rows or columns updates.

Example:

process.stdout.on('resize', () => {
  console.log('Screen size has changed!');
  console.log(`${process.stdout.columns}x${process.stdout.rows}`);
});

Additional Properties

Complete Advance AI topics:- CLICK HERE

  • ws.columns: Returns the number of columns in the current terminal window.
  • ws.rows: Returns the number of rows in the current terminal window.
    Both of these properties update dynamically on terminal resize.

Example: Node.js TTY Implementation

File: tty.js

var tty = require('tty');  

process.stdin.setRawMode(true);  
process.stdin.resume();  
console.log('I am leaving now');  

process.stdin.on('keypress', function(char, key) {  
  if (key && key.ctrl && key.name == 'c') {  
    process.exit();  
  }  
});

Output:

When you run this script and press Ctrl + C, the process will exit immediately — demonstrating how setRawMode(true) allows for real-time key detection.

Deep Learning Tutorial:– Click Here
Complete Python Course with Advance topics:-Click Here
SQL Tutorial :–Click Here


node js tty module tutorial
node js tty module github
node js tty module example
node js exit codes
fs module in node js
process stdout istty
utility modules in node js
processticksandrejections node js

 

Share this content:

Post Comment