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