Node.js Tutorial – TTY

The tutorial provides the detail on the node.js TTY module. The node.js TTY module provides the capability to run the node.js with a Text Terminal (TTY) attached. The node.js tty module provides the tty.ReadStream and tty.WriteStream classes. The node.js tty module can be accessed using the below command

const tty = require('tty');

The node.js peforms the below activities when it is being run as a TTY context:

  • process.stdin to be initialized as an instance of tty.ReadStream
  • process.stdout and process.stderr to be the instances of tty.WriteStream

Validate if Node.js is running as TTY context

The below command can be used to identify if the node.js is being run as TTY context

var tty = require('tty');  
 console.log('This is sample text to show TTY module usage...');  
process.stdin.on('keypress', function(char, key) {  
  if (key && key.ctrl && == 'c') {  

Class tty.ReadStream in node.js TTY module

The class tty.ReadStream in the TTY module is for reading the TTY Context and uses the process.stdin as the instance for the tty.ReadStream. The below given are the class methods for tty.ReadStream

tty.ReadStream Class Methodstty.ReadStream Method Description
readStream.isRawreturns TRUE if the TTY is configured to operate as a raw device
readStream.isTTYalways TRUE for tty.ReadStream instances
readStream.setRawMode(mode) mode: TRUE if the tty.ReadStream is configured to operate as a raw device
return: <this> which returns the read stream instance

Class tty.WriteStream in node.js TTY module

The class tty.WriteStream in the TTY module is for writing the TTY Context and uses the process.stdout and process.stderr as the instance for the tty.WriteStream.

Event: ‘resize’: The event resize is invoked when the writeStream.cloumns or writeStream.rows have changed but it does not return any arguments to the listener callback.

process.stdout.on('resize', () => {
  console.log('application screen size has changed!');