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.
Code
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,chalkandora— 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
Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.Tar Archive Command BuilderBuild safe tar create, extract, and list commands for gzip, xz, zstd, and bzip2 archives with excludes, target directory (-C), and flag explanations.
FAQ
- Does this require external packages?
- Yes — `inquirer`, `chalk` and `ora`. Add them to your project before running the snippet.
Report a problem with this exampleOpens a pre-filled issue on GitHub.