Node.js Tutorial

Node.js TTY Module

Node.js TTY Module

Node.js TTY Module

The Node.js TTY (Teletype) module controls terminal-based input and output. It exposes two classes ÔÇö tty.ReadStream and tty.WriteStream ÔÇö useful for building CLI applications and interactive terminal tools.

Node.js Tutorial:-
Complete Python Course:-

Accessing the TTY Module

const tty = require('tty');

When Node.js runs inside a TTY, process.stdin becomes a tty.ReadStream and process.stdout becomes a tty.WriteStream automatically.

Check if You Are in a TTY

node -p -e "Boolean(process.stdout.isTTY)"
// true if connected to a terminal

ReadStream and Raw Mode

process.stdin.setRawMode(true);  // read byte-by-byte
process.stdin.resume();

Raw mode is essential for capturing single keypresses without waiting for Enter.

WriteStream and Resize Events

process.stdout.on('resize', () => {
  console.log('Terminal resized!');
  console.log(`${process.stdout.columns} x ${process.stdout.rows}`);
});

Useful Properties

  • process.stdout.columns ÔÇö current width.
  • process.stdout.rows ÔÇö current height.
  • process.stdin.isTTY ÔÇö true if interactive.
  • process.stdin.setRawMode(true) ÔÇö enable raw mode.

Example: Detect Ctrl+C

process.stdin.setRawMode(true);
process.stdin.resume();
console.log('Press Ctrl+C to exit');

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

Download New Real Time Projects:- Click here
Complete Advance AI topics:-

Conclusion

The TTY module lets you build rich interactive CLI tools ÔÇö capture keypresses, react to terminal resizes, and control output formatting. For more tutorials, stay tuned to .

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

Source Code Available

Interested in This Project?

Get the complete source code for this project at a very affordable price — perfect for your portfolio, college submission, or learning. Message us on WhatsApp and we'll get back to you instantly!

Full source code included Step-by-step setup guide Instant delivery on WhatsApp Instant reply on WhatsApp
Chat on WhatsApp

We usually reply within a few minutes

Leave a Reply

Your email address will not be published. Required fields are marked *

Chat with us