Node.js CLI with Inquirer

Build an interactive CLI tool in Node.js using Inquirer for prompts, chalk for colored output, and ora for spinners. Includes command chaining, template.

JavascriptNodeCli Tooling

Code

snippet
import inquirer from 'inquirer';
import chalk from 'chalk';
import ora from 'ora';

const answers = await inquirer.prompt([
  { type: 'input', name: 'name', message: 'Project name:' },
  { type: 'list', name: 'framework', choices: ['react', 'vue', 'svelte'], default: 'react' },
  { type: 'confirm', name: 'ts', message: 'Use TypeScript?', default: true },
]);

const spinner = ora('Scaffolding...').start();
await scaffold(answers);
spinner.succeed(chalk.green(`Created ${answers.name}`));

Inside the snippet

Asynchronous boundaries use await, so failures surface as rejected promises at the call site instead of being swallowed.

Top-level bindings answers, spinner hold the demo state — nothing sits inside a function scope.

Caveats

  • External dependency: inquirer, chalk and ora — install them before running the snippet.

Core lines

await scaffold(answers);
const answers = await inquirer.prompt([
  { type: 'input', name: 'name', message: 'Project name:' },
  { type: 'list', name: 'framework', choices: ['react', 'vue', 'svelte'], default: 'react' },
  { type: 'confirm', name: 'ts', message: 'Use TypeScript?', default: true },
]);

Interactive Tools for This Topic

FAQ

Does this require external packages?
Yes — `inquirer`, `chalk` and `ora`. Add them to your project before running the snippet.