Friday, April 24, 2020

Node.js readline Module

Node.js 'readline' Module

readline module nodejs

The 'readline' module specifies an interface for reading the data streams line by line. The stream must be a readable stream like process.stdin. The module can be accessed by require(‘readline’).

var readline=require('readline');

For example,

var read = require('readline');
var  readline= read.createInterface({
  input: process.stdin,
  output: process.stdout

readline.question('How are you feeling today?', (youranswer) => {
  console.log(`you answered: ${youranswer}`);

How are you feeling today? Fantastic

you answered: Fantastic


The readline.Interface class

The object of readline.Interface(extends EventEmitter) class can be created by using the readline.createInterface() method. This object contains one input Readable stream and one Writable output stream. The output stream is used to send data to print for the input supplied by the user. The data is read using the input stream. The program will not halt until the 'readline.Interface' is closed using the close() method, because then it will keep waiting for the next data to come.



This event is emitted as one of the following condition appears,
  • If the readline.close() method is invoked
  • If the input stream receives its 'end' event;
  • If the input stream receives ctrl+D to signal end-of-transmission (EOT);
  • If the input stream receives ctrl+C to signal SIGINT and there is no 'SIGINT' event listener registered on the readline.Interface instance.


This event is emitted when the input stream encounters an end-of-line input (\n, \r, or \r\n). Generally emitted when the <Enter>, or <Return> keys are pressed.

var read = require('readline');
var  readline= read.createInterface({
  input: process.stdin,
  output: process.stdout

readline.on('line', (input) => {
  console.log(`Received: ${input}`);

 will wait till enter press
Received: will wait till enter press


This event is emitted if the input stream gets paused or the input stream has received the 'SIGCONT' event.


Emitted as the input stream is resumed.


Emitted when a Node.js process running in the background(send to the background using ctrl+z) is brought back to the foreground.

'SIGINT' event

Emitted when the input stream has received ctrl+C input(e.g. SIGINT).

'SIGTSTP' event

Emitted when the input stream has received ctrl+Z input(e.g. SIGTSTP).



This method is used to set a prompt, for example when we run node with cmd, see >, that is Node.js default prompt.


This method prepares readline.Interface instance for the user input and places the current setPrompt options to a new line. This method sets preserveCursor to true to prevent the cursor placement being reset to 0.

readline.question(query, callback)

This method is used to receive input from the user and then invokes callback with the user's response after it has been typed.


This method pauses the readline input stream. This can be resumed later if needed, using the resume() method.


This method is used to resume the paused readline input stream.


This method is used to closes the readline.Interface objects, and transfers the control to the input and output streams. The 'close' event is also emitted.

readline.write(data[, key])

This method is used to writes data to the output stream unless the output is set to null or undefined. The key argument is an object used to represent a key sequence in the case of TTY terminal.


This method returns the exact position of the cursor in relative to the input prompt + string.

readline.clearLine(stream, dir[, callback])

This method is used to clear the current line of a specified TTY stream in a given direction identified by dir.


This method is used to construct a new readline.Interface instance.

readline.moveCursor(stream, dx, dy[, callback])

This method is used to move the cursor relative to its current position in a given TTY stream.


Fires keypress events for the specified stream.


var read = require('readline');
var readline = read.createInterface({
  input: process.stdin,
  output: process.stdout,
  prompt: 'ProgrammingHunk Says> '

console.log('What are you using these days???\n');
readline.on('line', (line) => {
  switch (line.trim()) {
    case 'Node.js':
      console.log('You are using Node.js');
      console.log(`You are using '${line.trim()}' not Node.js`);
}).on('close', () => {


 ProgrammingHunk Says> =======================

What are you using these days???

You are using Node.js
ProgrammingHunk Says> react
You are using 'react' not Node.js
ProgrammingHunk Says> ||========ProgrammingHunk=======||