Inquirer Interactive Prompts
Add interactive prompts with Inquirer for choices, confirmations, and multi-select, with sensible defaults so the CLI works non-interactively too.
On this page
Introduction
This guide covers Inquirer interactive prompts, a recurring concern in the cli-tooling scenario. A CLI that only takes flags is fast for scripts but hard for a first-time user who does not know the options. Interactive prompts guide them through choices, and sensible defaults plus a non-interactive fallback let the same command work in a pipeline where there is no tty.
Match prompt to input
Use list for one-of-many, confirm for yes-no, and checkbox for multi-select, so the prompt shape matches the data and the user cannot type an invalid value.
The trade-off worth naming is that use list for one-of-many, confirm for yes-no, and checkbox for multi-select, so the prompt shape matches the data and the user cannot type an invalid value adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
import inquirer from 'inquirer';
const a = await inquirer.prompt([
{ type: 'list', name: 'env', message: 'Target env', choices: ['dev','staging','prod'], default: 'dev' },
{ type: 'confirm', name: 'migrate', message: 'Run migrations?', default: true },
]);
Default everything
Give every prompt a default so the command can run non-interactively by accepting defaults, which is what makes it usable in CI.
The trade-off worth naming is that give every prompt a default so the command can run non-interactively by accepting defaults, which is what makes it usable in ci adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Non-interactive fallback
Check stdin.isTTY and skip prompts when false, reading from flags or defaults instead, so the command works in a pipeline.
The trade-off worth naming is that check stdin.istty and skip prompts when false, reading from flags or defaults instead, so the command works in a pipeline adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
if (!process.stdin.isTTY) {
return { env: process.env.TARGET_ENV || 'dev', migrate: true };
}
Validate answers
Add a validate function to free-text prompts so an invalid entry is rejected with a reason and re-asked, which keeps bad input out of the handler.
The trade-off worth naming is that add a validate function to free-text prompts so an invalid entry is rejected with a reason and re-asked, which keeps bad input out of the handler adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Cancel cleanly
On Ctrl-C or an empty answer, exit with a non-zero code and no partial side effects, so a cancelled prompt does not leave half a scaffold on disk.
The trade-off worth naming is that on ctrl-c or an empty answer, exit with a non-zero code and no partial side effects, so a cancelled prompt does not leave half a scaffold on disk adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Test defaults
Run the prompt with non-tty stdin and assert the defaults are used, then run with answers and assert they flow through, so both paths work.
The trade-off worth naming is that run the prompt with non-tty stdin and assert the defaults are used, then run with answers and assert they flow through, so both paths work adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Sources
Sources: Inquirer, TTY detection.
FAQ
- How do I make prompts non-interactive in CI?
- Provide defaults and check process.stdin.isTTY; if not a tty, skip prompts and use the defaults or flags, so the command runs unattended.
- List or rawlist?
- Use list for a named choice with a default; rawlist lets the user type the index, which is faster for long lists but less discoverable.
Report a problem with this articleOpens a pre-filled issue on GitHub.