Skip to content
Related Articles

Related Articles

Node.js readStream.isTTY Property

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 07 Jul, 2021

The readStream.isTTY property is an inbuilt application programming interface of class ReadStream within tty module which is used to check if the read stream object is an instance of tty or not. It will always return true for tty, ReadStream.

Syntax:

const readStream.isTTY

Return Value: This property returns true if read stream object is instance of tty.

Example 1: Filename: index.js

javascript




// Node.js program to demonstrate the
// readStream.isTTY property
 
// Importing dgram module
var dgram = require('dgram');
 
// Creating and initializing client
// and server socket
var client = dgram.createSocket("udp4");
var server = dgram.createSocket("udp4");
 
// Handling the message event
server.on("message", function (msg) {
 
    // Creating and initializing a
    // ReadStream object
    let ReadStream = process.stdin;
 
    // Checking if it is instance of TTY
    // or not by using isTTY() method
    const status = ReadStream.isTTY;
 
    // Displaying the result
    if (status) {
        process.stdout.write(msg
            + "an instant of TTY" + "\n");
    } else {
        process.stdout.write(msg
            + "not an instant of TTY" + "\n");
    }
 
    // Exiting process
    process.exit();
})
 
// Binding server with port
.bind(1234, () => {
});
 
// Client sending message to server
client.send("It is ", 0, 7, 1234, "localhost");

Output:

It is an instant of TTY

Example 2: Filename: index.js

javascript




// Node.js program to demonstrate the
// readStream.isTTY property
 
// Creating and initializing a
// ReadStream object
let ReadStream = process.stdin;
 
// Checking if it is instance of TTY
// or not by using isTTY() method
const status = ReadStream.isTTY;
 
// Displaying the result
if(status) {
   console.log("It is an instant of TTY");
} else {
   console.log("it is not an instant of TTY");
}

Run the index.js file using the following command:

node index.js

Output:

It is an instant of TTY

Reference: https://nodejs.org/dist/latest-v12.x/docs/api/tty.html#tty_readstream_istty


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!