Node.js › CLI Tools & Scripting with Node.js

How would you build an interactive CLI wizard that prompts the user for multiple pieces of input using a library like Inquirer.js?

Advanced

Answer

Inquirer.js provides a set of prompt types (input, list/select, checkbox for multi-select, confirm) that render interactive terminal UI, collecting a structured object of answers after the user completes each prompt in sequence -- far more ergonomic than hand-building this interaction with the raw readline module, and it handles keyboard navigation, validation, and formatting automatically.
const inquirer = require('inquirer');

const answers = await inquirer.prompt([
  { type: 'input', name: 'projectName', message: 'Project name:' },
  { type: 'list', name: 'framework', message: 'Choose a framework:', choices: ['Express', 'Fastify', 'Koa'] },
  { type: 'confirm', name: 'useTypeScript', message: 'Use TypeScript?', default: true },
]);

console.log(answers); // { projectName: '...', framework: '...', useTypeScript: true/false }
Real-world example A project-scaffolding CLI tool (similar to 'create-react-app') uses Inquirer.js to interactively ask the user for a project name, preferred framework, and whether to include TypeScript, then generates the appropriate boilerplate files based on the structured answers object it receives.

Common follow-ups: How would you add input validation to an Inquirer prompt, like ensuring a project name doesn't contain invalid characters?;How does Inquirer's prompt chaining compare to manually building the same flow with the built-in readline module?