How to show a prompt and get input from the command line in Node.js?
Tags: node
Published: 2021-03-27 00:00:00

---
  title: How to show a prompt and get input from the command line in Node.js?
  published: true
  tags: Nodejs
  date: Sat Mar 27 2021 05:30:00 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/show-prompt-and-get-input-from-command-line-nodejs
  cover_image: https://melvingeorge.me/_next/static/images/main-f264b376f889eee401e97a4b04fb5c04.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/show-prompt-and-get-input-from-command-line-nodejs)
  
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 &rarr;](#full-code)

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

### `readline` is an in-built module in Node.js

```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,

```js
// 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,

```js
// 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,

```js
// 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,

<a name="full-code"></a>

```js
// 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](https://replit.com/@melvin2016/get-input-from-command-line-in-nodejs#index.js).

That's all! 😃

### Feel free to share if you found this useful 😃.

---