Hello everyone, Hope you all are doing well! Today I am writing a blog on a npm library readlineSync. So without any further delay , let me tell you what all I understood about readlineSync when I read about it from the npmjs docs
To install it you can use the following command -
npm i readline-sync
So to start with readlineSync is used to communicate with user via a console. In a CLI application - we use readlineSync npm library to read user input.
var readlineSync = require('readline-sync');
var name = readlineSync.question('What's your name?')
console.log('Whatsup!' + name + 'Welcome')
What's your name? Sahil
Whatsup! Sahil Welcome
So now we know the basic idea of what readlineSync is , lets see some functions in readline-sync library. We will see some basic methods
In this method, as we saw in above example, it displays a query to user and then returns the input of user that he/she has entered after the user presses the Enter key
Moving on to next method,
In this method, it displays a query to the user , it returns true if 'Y' or'y' is pressed, for other inputs , it returns false. Note that the userhas no chance to change the input.
var readlineSync = require('readline-sync');
if (readlineSync.keyInYN('Do you like Football')){
// Y key was pressed
console.log('Cool, its a great sport');
} else {
// Another key was pressed
console.log('Ahh, my bad');
This method is used to prompt a user to choose an item from a list. The function will return the number the user selected. The user doesn’t have to press the Enter button when we use this function.
var readlineSync = require('readline-sync');
let players = ['Ronaldo', 'Messi', 'Neymar', 'Pele', 'Maradona'];
let index = readlineSync.keyInSelect(players, 'Favorite Player?');
console.log (players[index]);
Thats it thats all I learned about readlinesync , hope you guys are clear to with the readline library and are aware about its function after reading the blog.
These are some basic functions of readlineSync library, you can use Utility functions which are advanced basic functions for more efficiency.
To know more about readlineSync -
readlineSync Document