Lightweight, beautiful and user-friendly interactive CLI prompts library with promise-based API
npx @tessl/cli install tessl/npm-prompts@2.4.0Prompts is a lightweight, beautiful and user-friendly interactive CLI prompts library with promise-based API and async/await support. It provides a comprehensive set of prompt types for gathering user input in command-line applications, with beautiful styling, flexible validation, and consistent experience across all prompt types.
npm install promptsconst prompts = require('prompts');For ES modules:
import prompts from 'prompts';Import specific functions:
const { prompt, inject, override } = require('prompts');const prompts = require('prompts');
// Single prompt
const response = await prompts({
type: 'text',
name: 'username',
message: 'What is your GitHub username?'
});
console.log(response.username);
// Multiple prompts
const response = await prompts([
{
type: 'text',
name: 'username',
message: 'What is your GitHub username?'
},
{
type: 'number',
name: 'age',
message: 'How old are you?'
},
{
type: 'confirm',
name: 'subscribe',
message: 'Would you like to subscribe to our newsletter?'
}
]);
console.log(response); // { username: 'alice', age: 25, subscribe: true }Prompts is built around several key components:
Main prompting functionality for single or multiple questions with promise-based API, validation, and callback support.
function prompts(
questions: Question | Question[],
options?: PromptOptions
): Promise<Answers>;
interface Question {
type: string | ((prev: any, values: Answers, prompt: Question) => string | null);
name: string | ((prev: any, values: Answers, prompt: Question) => string);
message: string | ((prev: any, values: Answers, prompt: Question) => string);
initial?: any | ((prev: any, values: Answers, prompt: Question) => any);
format?: (val: any, values: Answers) => any;
validate?: (val: any, values: Answers) => boolean | string;
onRender?: (kleur: any) => void;
onState?: (state: { value: any; aborted: boolean }) => void;
stdin?: NodeJS.ReadableStream;
stdout?: NodeJS.WritableStream;
}
interface PromptOptions {
onSubmit?: (prompt: Question, answer: any, answers: Answers) => boolean | Promise<boolean>;
onCancel?: (prompt: Question, answers: Answers) => boolean | Promise<boolean>;
}
type Answers = Record<string, any>;Text-based input prompts including standard text, password (masked), and invisible input modes.
// Text prompt
{
type: 'text',
name: 'value',
message: string,
initial?: string,
style?: 'default' | 'password' | 'invisible' | 'emoji',
validate?: (value: string) => boolean | string,
format?: (value: string) => any
}
// Password prompt (masked input)
{
type: 'password',
name: 'value',
message: string,
initial?: string
}
// Invisible prompt (like sudo)
{
type: 'invisible',
name: 'value',
message: string,
initial?: string
}Numeric and temporal input prompts with validation, increment/decrement controls, and locale support.
// Number prompt
{
type: 'number',
name: 'value',
message: string,
initial?: number,
max?: number,
min?: number,
float?: boolean,
round?: number,
increment?: number,
style?: 'default' | 'password' | 'invisible' | 'emoji'
}
// Date prompt
{
type: 'date',
name: 'value',
message: string,
initial?: Date,
locales?: DateLocales,
mask?: string,
validate?: (date: Date) => boolean | string
}Interactive selection prompts for single choice, multiple choice, and searchable selection from predefined options.
// Single selection
{
type: 'select',
name: 'value',
message: string,
choices: Choice[],
initial?: number,
hint?: string,
warn?: string
}
// Multiple selection
{
type: 'multiselect',
name: 'value',
message: string,
choices: Choice[],
max?: number,
min?: number,
hint?: string,
warn?: string,
instructions?: string | boolean,
optionsPerPage?: number
}
// Autocomplete selection
{
type: 'autocomplete',
name: 'value',
message: string,
choices: Choice[],
suggest?: (input: string, choices: Choice[]) => Promise<Choice[]>,
limit?: number,
style?: string,
initial?: string | number,
clearFirst?: boolean,
fallback?: string
}
interface Choice {
title: string;
value?: any;
description?: string;
disabled?: boolean;
selected?: boolean; // for multiselect only
}Binary choice prompts for yes/no confirmations and toggle switches with custom labels.
// Confirmation prompt
{
type: 'confirm',
name: 'value',
message: string,
initial?: boolean
}
// Toggle prompt
{
type: 'toggle',
name: 'value',
message: string,
initial?: boolean,
active?: string,
inactive?: string
}
// List prompt
{
type: 'list',
name: 'value',
message: string,
initial?: string,
separator?: string
}Confirmation and Toggle Prompts
Functions for programmatic testing, response injection, and pre-answering questions during automated testing scenarios.
function inject(answers: any[]): void;
function override(answers: Record<string, any>): void;All prompt types support these common properties:
Dynamic properties can be functions with signature (prev, values, prompt) => value where:
prev: Value from the previous promptvalues: All collected responses so farprompt: Previous prompt objectPrompts can be canceled by users with Esc, Ctrl+C, or Ctrl+D. When canceled, the promise resolves with partial answers collected so far. Use the onCancel callback to handle cancellation events.
The validate function should return true for valid input or an error message string for invalid input. If false is returned, a default error message is shown.