Inquirer Checkbox Multiselect Prompt Flow
Build interactive CLI flows with Inquirer checkbox prompts, validated input, default selections, and answer-driven branching between steps.
Code
import inquirer from 'inquirer';
const answers = await inquirer.prompt([
{
type: 'checkbox',
name: 'services',
message: 'Which services should the report cover?',
choices: [
{ name: 'api-gateway', value: 'gw', checked: true },
{ name: 'billing', value: 'bill' },
{ name: 'search', value: 'search', disabled: 'data source offline' },
],
validate: (list) => (list.length > 0 ? true : 'select at least one service'),
},
{
type: 'input',
name: 'ticket',
message: 'Ops ticket number:',
validate: (value) =>
/^OPS-\d{4,6}$/.test(value) ? true : 'expected format: OPS-12345',
},
{
type: 'confirm',
name: 'notify',
message: 'send summary to the on-call channel?',
default: false,
},
]);
console.log('report scope:', answers.services, 'ticket:', answers.ticket);How it works
Await points sequence the asynchronous work; a rejection lands at the awaiting line.
The file’s state is a handful of top-level bindings: answers.
Points of care
- Plan for an install step:
inquireris a real dependency, resolved by the package manager.
Taking the usage further
The demo output is labeled (report scope:), so every printed line maps back to its originating operation.
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
- Which packages must be installed first?
- Yes. The imports resolve to `inquirer`, which the package manager must provide.
Report a problem with this exampleOpens a pre-filled issue on GitHub.