Open In App

Node.js Readline() Module

Readline Module in Node.js allows the reading of input stream line by line. This module wraps up the process standard output and process standard input objects. The Readline module makes it easier to input and read the output given by the user. To use this module, create a new JavaScript file and write the following code at the start of the application – 

const readline = require('readline');

The Readline module comes with different methods to interact with the user.



Interaction with the user: For the interaction, we will first create an interface for the input and output. For creating an interface, write the following code – 




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

Here, the createInterface() method takes two arguments. The first argument will be for the standard input and the second one will be for reading the standard output. 






rl.question('What is your age? ', (age) => {
    console.log('Your age is: ' + age);
});

Here, rl.question() method is used for asking questions from the user and reading their reply (output). The first parameter is used to ask the question and the second parameter is a callback function that will take the output from the user as the parameter and return it to the console. 

The output for the above code will be – 

What is your age? 20
Your age is: 20

Here, the problem is it will not exit the application and it will keep asking for the inputs. To resolve this issue, rl.close() method is used. This method will close the interface. To use it in the application, write the following –




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

We can also use a setPrompt() method is used to set the particular statement to the console. prompt() method for displaying the statement which is set in setPrompt() Method. Therefore, write the following code –




const readline = require('readline');
let rl = readline.createInterface(
    process.stdin, process.stdout);
 
rl.setPrompt(`What is your age? `);
rl.prompt()

This code will take input from the user. Now, we need a listener for reading the input from the user and displaying it to the console. For this purpose, Readline Module has a listener method called on that will take two parameters. The first parameter will the event and the second parameter will be a callback function that will return the output to the console. For example – 




const readline = require('readline');
 
const rl = readline.createInterface(
        process.stdin, process.stdout);
 
rl.setPrompt(`What is your age? `);
rl.prompt();
rl.on('line', (age) => {
    console.log(`Age received by the user: ${age}`);
    rl.close();
});

Here, rl.on() method takes the first argument as a line event. This event is invoked whenever the user presses Enter key. The output for the above code will be – 

What is your age? 20
Age received by the user: 20

Events: Along with the listener, readline module also comes with the event’s properties. Let us learn about the various events. 

For example:




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




rl.on('pause', () => {
    console.log('Paused Event is invoked');
});

Paused Event is invoked




rl.on('resume', () => {
    console.log('Resume Event is invoked.');
});




rl.on('SIGINT', () => {
    rl.question('Exit (y or n)? ', (input) => {
          if (input.match(/^y(es)?$/i)) { rl.pause(); }
    });
});




rl.on('SIGTSTP', () => {
    console.log('SIGTSTP event is invoked.');
})




rl.on('SIGCONT', () => {
     console.log('SIGCONT event is invoked.');
     rl.prompt();
})

Conclusion: In this article, we learned about the Readline Module in Node.js. Also, we learned about its various implementations using methods, variables, events, and listeners.


Article Tags :