Configs2026年4月25日·1 分钟阅读

Inquirer.js — Interactive CLI Prompts for Node.js

Inquirer.js provides a collection of common interactive command-line user interfaces including lists, checkboxes, confirmations, and text input for Node.js CLI tools.

assetLangBanner.body

Introduction

Inquirer.js is the standard library for building interactive command-line interfaces in Node.js. It provides a set of prompt types (input, list, checkbox, confirm, password, editor) that handle user input, validation, and formatting out of the box.

What Inquirer.js Does

  • Presents interactive prompts (text input, single select, multi-select, confirm, password)
  • Validates user input with custom validation functions
  • Transforms and filters answers before returning them
  • Supports searchable and paginated lists for large option sets
  • Provides a composable API where each prompt is an independent package

Architecture Overview

Inquirer.js v2+ uses a modular architecture where each prompt type is a separate npm package under @inquirer/prompts. Each prompt manages its own render loop using an internal hook system inspired by React. Prompts read raw keypress events from stdin, update internal state, and re-render the terminal UI. The top-level API is promise-based, resolving with the user's answer when the prompt completes.

Self-Hosting & Configuration

  • Install the unified package @inquirer/prompts or individual prompt packages
  • Import prompt functions directly: input, select, checkbox, confirm, password, editor
  • Pass a config object with message, choices (for selects), default, and validate
  • Use theme option to customize colors and prefix symbols
  • For legacy code, inquirer (v9) still supports the chained .prompt() API

Key Features

  • Modular design: install only the prompt types you need
  • Built-in validation with custom error messages
  • Searchable lists and autocomplete for large datasets
  • Separator support for grouping choices in list prompts
  • Theming API to match your CLI's visual style

Comparison with Similar Tools

  • prompts — lightweight alternative with fewer prompt types; Inquirer.js has richer features and a larger ecosystem
  • Enquirer — similar API with a focus on performance; Inquirer.js has broader adoption and more prompt types
  • readline — Node.js built-in for basic line input; Inquirer.js adds structured prompt UI on top
  • Clack — beautiful prompts with spinner support; Inquirer.js offers more prompt types and deeper customization
  • Bubble Tea (Go) — full TUI framework; Inquirer.js is focused specifically on CLI prompts for Node.js

FAQ

Q: What is the difference between inquirer and @inquirer/prompts? A: @inquirer/prompts is the modern modular rewrite. The legacy inquirer package wraps these modules and adds the .prompt([]) chained API.

Q: Can I use Inquirer.js with TypeScript? A: Yes. All @inquirer/* packages ship with TypeScript types and provide generic type parameters for answer types.

Q: How do I create a custom prompt type? A: Use @inquirer/core to build a prompt with the createPrompt hook API. You get useState, useKeypress, and a render function.

Q: Does Inquirer.js work in non-interactive environments (CI)? A: No. Prompts require a TTY. In CI, supply answers via environment variables or default values and skip interactive prompts.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产