How to show a prompt and get input from the command line in Node.js?

March 27, 2021 - 3 min read

To show prompt and get input from CLI or command line, you can use the readline module and then use the createInterface() method to create an IO interface in Node.js.

Jump to the full code →

To do that first, require the readline module like this,

readline is an in-built module in Node.js

// require readline module
const readline = require("readline");

After that, we need to create our IO interface or a system where we need to define the input and output sources. For that, we can use the createInterface() method from the readline module.

It can be done like this,

// require readline module
const readline = require("readline");

// create the IO interface
const commandLineIO = readline.createInterface();

But still, we haven't told the input and the output sources for our interfaces, for that we need to pass an object as an argument to the createInterface() method.

The object should have a key called:

  • the input with the value of process.stdin, which tells that the terminal is the input source.
  • and output with the value of process.stdout, which tells that the terminal is the output source.

It can be done like this,

// require readline module
const readline = require("readline");

// create the IO interface
// also specify the input and output sources
const commandLineIO = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

After we have made out the command-line interface, we need to show the users questions or prompts and get answers.

To do that we can use the question() method from the commandLineIO interface.

The question method accepts 2 arguments:

  • The first one is the question or prompt to be shown to the user
  • the second one is a callback function that will be invoked after the user types some input and the Enter key is pressed. The callback function will have one argument which is the answer entered by the user.

It can be done like this,

// require readline module
const readline = require("readline");

// create the IO interface
// also specify the input and output sources
const commandLineIO = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

// show a question to the user
commandLineIO.question("What is your Name?", (answer) => {
  console.log(`You name is ${answer}`);
});

At the last, after showing the answer to the user and you need to stop accepting the input, you can use the close() method on the commandLineIO like this,

// require readline module
const readline = require("readline");

// create the IO interface
// also specify the input and output sources
const commandLineIO = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

// show a question to the user
commandLineIO.question("What is your Name?", (answer) => {
  console.log(`You name is ${answer}`);

  // stop accepting the input
  commandLineIO.close();
});

See the above code live in repl.it.

That's all! 😃

Feel free to share if you found this useful 😃.