prompts
❯ Lightweight, beautiful and user-friendly interactive prompts
A lightweight Node.js library for adding interactive questions to command-line tools, text, multiple choice, date pickers, autocomplete, with built-in support for injecting answers in automated tests.
Prompts is a Node.js library for building interactive command-line interfaces. When you run a command-line tool and it asks you to type something, choose from a list, or confirm a choice before continuing, that interaction is usually powered by a library like this one. Prompts handles the input and display so developers do not have to write that logic from scratch.
The library supports a range of input types: plain text, numbers, passwords, yes or no confirmations, single selections from a list, multiple selections from a list, date pickers, and autocomplete fields. Each prompt type is self-contained, so you can use just the ones you need. Prompts are defined as plain JavaScript objects with properties like the question text, the type of input expected, and optional validation logic. You pass one prompt or a list of prompts to the main function and it returns an object containing all the user's answers.
One notable feature is programmatic injection, where you can pre-supply answers in code. This is intended for automated testing: instead of having a test script interact with the terminal manually, you inject the expected answers ahead of time and the prompts resolve without waiting for keyboard input.
The library also supports dynamic prompt chains, where whether a particular question appears at all can depend on how the user answered a previous one. This is useful for wizards or setup flows where some follow-up questions are only relevant in certain situations.
Prompts has no large dependencies and works with modern JavaScript async syntax. It supports Node 14 and above and is installed via npm. It is designed for developers building command-line tools who want to add interactive input collection without taking on a heavyweight dependency.
Where it fits
- Build a CLI setup wizard that asks users for project name, language, and options before generating files.
- Test a CLI tool without keyboard interaction by injecting pre-supplied answers programmatically.
- Create a branching questionnaire where follow-up questions only appear based on how the user answered a previous one.
- Add input validation to any prompt so users must provide a valid answer before the script continues.