NodeJS Readline Module

The Readline Module is a built-in module in NodeJS used for reading the input stream and providing resultant output. This module implements the process input stream and output stream to give the functionality of reading input and providing resultant output. It enables the question answers type operation, such as when initiating NPM, it asks for several inputs for creating the configuration file package-log.json.

Syntax and Example of Readline Module

Below is the syntax to include Readline Module in the NodeJS projects.

const readline = require('readline');

Example:

Below is an example of a Readline Module to ask for a name and then print a message “Hello” concatenated with the entered value. 

const readline = require('readline');

const rl = readline.createInterface(process.stdin, process.stdout);

rl.question('What is your name? ', (name) => {
    console.log('Hello ' + name);
    rl.close();
});

Output:

Readline Module Example

Method of NodeJs Readline Module

Readline has many methods having different operations, let’s understand their use case, syntax, and example one by one.

createInterface()

This method creates an instance of the InterfaceConstructor class. This method takes standard input and stands output as an argument to create the instance. This instance is associated with a single input stream for reading and an output stream for writing.

Syntax:

readline.createInterface(input, output);

Example:

const readline = require('readline');

const rl = readline.createInterface(process.stdin, process.stdout);

rl.question()

This method is used to take input from the user. This method takes two arguments, a question to ask from the user and a callback function. This callback has an argument to get the entered value.

Syntax:

rl.question(message, callback);

Example:

const readline = require('readline');

const rl = readline.createInterface(process.stdin, process.stdout);

rl.question('What is your name? ', (name) => {
    console.log('Hello ' + name);
});

Output:

rl.question Example Output

In the output, you can notice that the application keeps asking for input even after entering an input, for getting rid of this we have another method rl.close().

rl.close()

This method is used to close the Interface i.e take control of the input and output stream from the instance of InterfaceConstructor.

Note: This method does not immediately stop the other event to happens like ‘line’, ‘close’, ‘resume’, etc.

Syntax:

rl.close()

Example:

const readline = require('readline');

const rl = readline.createInterface(process.stdin, process.stdout);

rl.question('What is your name? ', (name) => {
    console.log('Hello ' + name);
    rl.close();
});

Output:

rl.close Example Output

rl.resume()

This method is similar to prompt(), it resumes the input streams if it is paused but it doesn’t prompt anything.

Syntax:

rl.resume()

Example:

const readline = require('readline');

const rl = readline.createInterface(process.stdin, process.stdout);

rl.question('What is your name? ', (name) => {
    console.log('Hello ' + name);
    rl.close();
    rl.resume();
});

Output:

rl.question Example Output

rl.setPrompt()

This method is used to set prompts. It takes a string containing any kind of message as an argument and then it is required to call the rl.prompt() method to show that message in the console.

Syntax:

rl.setPrompt(message)

Example:

const readline = require('readline');

const rl = readline.createInterface(process.stdin, process.stdout);

rl.setPrompt('Enter your name: ');

rl.prompt()

This method is used to output the value set by rl.setPrompt() in the new line in the console. It resumes the input stream if it is paused and provides a new location for the user to enter the input.

Syntax:

rl.prompt()

Example:

const readline = require('readline');

const rl = readline.createInterface(process.stdin, process.stdout);

rl.setPrompt('Enter your name: ');

rl.prompt();

Output:

rl.prompt Example Output

To get the entered input, we have to use another method rl.on().

rl.on()

This method takes an event and a callback to return the output. The event can be “line” which will activate when entered key is pressed, “pause” which is active when input steam is paused, “resume” when it resumes, “SIGINT” when the user press ctrl + c, and “SIGTSTP” when user press ctrl + z.  

Syntax:

rl.on(event, callback)

Example:

const readline = require('readline');

const rl = readline.createInterface(process.stdin, process.stdout);

rl.setPrompt('Enter your name: ');

rl.prompt();

rl.on('line', (line) => {
  console.log(`Hello ${line}`);
  rl.close();
});

Output:

rl.on Example Output

Summary and Reference

The Readline Module is used in reading input and writing output. It provides a way to prompt different types of messages while giving the option to enter the input. It also helps in performing question-answer types operations. Hope this tutorial helps you to understand the Readline Module and its methods in NodeJS. 

Reference

https://nodejs.org/api/readline.html

Aditya Gupta
Aditya Gupta
Articles: 94