Accept input from the command line in Node

By

Learn how to accept input from the command line in Node.js and make CLI programs interactive using the built-in readline module and the Inquirer.js package.

~~~

How to make a Node.js CLI program interactive?

Node provides the built-in readline module to perform exactly this: get input from a readable stream such as the process.stdin stream, which during the execution of a Node program is the terminal input, one line at a time.

const readline = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
})

readline.question(`What's your name?`, (name) => {
  console.log(`Hi ${name}!`)
  readline.close()
})

This piece of code asks the username, and once the text is entered and the user presses enter, we send a greeting.

The question() method shows the first parameter (a question) and waits for the user input. It calls the callback function once enter is pressed.

In this callback function, we close the readline interface.

If you prefer await over callbacks, Node.js 17+ ships a promise-based version of the same API in node:readline/promises. question() returns a promise that resolves with the answer:

import readline from 'node:readline/promises'
import { stdin as input, stdout as output } from 'node:process'

const rl = readline.createInterface({ input, output })

const name = await rl.question(`What's your name? `)
console.log(`Hi ${name}!`)
rl.close()

readline offers several other methods, and I’ll let you check them out on the package documentation I linked above.

If you need to require a password, it’s best to not echo it back, but instead showing a * symbol.

The simplest way is to use the readline-sync package which is very similar in terms of the API and handles this out of the box.

A more complete and abstract solution is provided by the Inquirer.js package.

The current API lives in the @inquirer/prompts package (8.7.2 as of September 2026), which exports one function per prompt type. Install it with npm install @inquirer/prompts. It is ESM-only, so your file needs "type": "module" in package.json, or a .mjs extension. Then you can replicate the above code like this:

import { input } from '@inquirer/prompts'

const name = await input({ message: "What's your name?" })
console.log(`Hi ${name}!`)

The original inquirer package (14.2.2) still exists and is maintained, but its maintainers call it the legacy version and point new projects to @inquirer/prompts. If you have older code, the legacy API looks like this:

import inquirer from 'inquirer'

const questions = [
  {
    type: 'input',
    name: 'name',
    message: "What's your name?",
  },
]

const answers = await inquirer.prompt(questions)
console.log(`Hi ${answers.name}!`)

Inquirer.js lets you do many things like asking multiple choices, having radio buttons, confirmations, and more.

It’s worth knowing all the alternatives, especially the built-in ones provided by Node, but if you plan to take CLI input to the next level, Inquirer.js is an optimal choice.

For parsing flags and options instead of prompts, see how to get input from the command line in Node.js. And for printing output nicely, I wrote about output to the CLI.

Tagged: Node.js · All topics

Want me to talk about your product? You can sponsor this site.

~~~

Related posts about node: