Lightweight, beautiful and user-friendly interactive CLI prompts library with promise-based API
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Text-based input prompts for gathering string input from users, including standard text input, password (masked) input, and invisible input modes. All text prompts support validation, formatting, and autocomplete to initial value.
const prompts = require('prompts');Standard text input prompt for free text entry. Users can type freely and use Tab to autocomplete to the initial value when provided.
{
/** Prompt type identifier */
type: 'text',
/** Property name for response object */
name: string,
/** Display message for user */
message: string,
/** Default string value. User can Tab to autocomplete to this value */
initial?: string,
/** Render style controlling input visibility */
style?: 'default' | 'password' | 'invisible' | 'emoji',
/** Custom formatter for user input */
format?: (value: string, values: Answers) => any,
/** Input validation function */
validate?: (value: string, values: Answers) => boolean | string,
/** Render callback with kleur styling */
onRender?: (kleur: any) => void,
/** State change callback */
onState?: (state: { value: string; aborted: boolean }) => void,
/** Input stream */
stdin?: NodeJS.ReadableStream,
/** Output stream */
stdout?: NodeJS.WritableStream
}Usage Examples:
const prompts = require('prompts');
// Basic text input
const response = await prompts({
type: 'text',
name: 'username',
message: 'What is your GitHub username?'
});
// Text input with validation and formatting
const response = await prompts({
type: 'text',
name: 'email',
message: 'Enter your email address:',
initial: 'user@example.com',
validate: value => {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(value) || 'Please enter a valid email address';
},
format: value => value.toLowerCase().trim()
});
// Text input with custom styling
const response = await prompts({
type: 'text',
name: 'message',
message: 'Enter a message:',
style: 'emoji',
onRender(kleur) {
this.msg = kleur.cyan('📝 Enter a message:');
}
});Password input prompt with masked display. Input characters are shown as asterisks or dots to hide sensitive information. This is equivalent to using text prompt with style: 'password'.
{
/** Prompt type identifier */
type: 'password',
/** Property name for response object */
name: string,
/** Display message for user */
message: string,
/** Default string value (not recommended for passwords) */
initial?: string,
/** Custom formatter for user input */
format?: (value: string, values: Answers) => any,
/** Input validation function */
validate?: (value: string, values: Answers) => boolean | string,
/** Render callback with kleur styling */
onRender?: (kleur: any) => void,
/** State change callback */
onState?: (state: { value: string; aborted: boolean }) => void,
/** Input stream */
stdin?: NodeJS.ReadableStream,
/** Output stream */
stdout?: NodeJS.WritableStream
}Usage Examples:
// Basic password input
const response = await prompts({
type: 'password',
name: 'password',
message: 'Enter your password:'
});
// Password with validation
const response = await prompts({
type: 'password',
name: 'newPassword',
message: 'Create a new password:',
validate: value => {
if (value.length < 8) return 'Password must be at least 8 characters';
if (!/[A-Z]/.test(value)) return 'Password must contain an uppercase letter';
if (!/[0-9]/.test(value)) return 'Password must contain a number';
return true;
}
});
// Password confirmation
const questions = [
{
type: 'password',
name: 'password',
message: 'Enter password:'
},
{
type: 'password',
name: 'confirmPassword',
message: 'Confirm password:',
validate: (value, values) =>
value === values.password || 'Passwords do not match'
}
];Invisible input prompt where user input is completely hidden, similar to sudo password prompts. This is equivalent to using text prompt with style: 'invisible'.
{
/** Prompt type identifier */
type: 'invisible',
/** Property name for response object */
name: string,
/** Display message for user */
message: string,
/** Default string value */
initial?: string,
/** Custom formatter for user input */
format?: (value: string, values: Answers) => any,
/** Input validation function */
validate?: (value: string, values: Answers) => boolean | string,
/** Render callback with kleur styling */
onRender?: (kleur: any) => void,
/** State change callback */
onState?: (state: { value: string; aborted: boolean }) => void,
/** Input stream */
stdin?: NodeJS.ReadableStream,
/** Output stream */
stdout?: NodeJS.WritableStream
}Usage Examples:
// Invisible input for sensitive data
const response = await prompts({
type: 'invisible',
name: 'secret',
message: 'Enter your secret key:'
});
// Invisible input with validation
const response = await prompts({
type: 'invisible',
name: 'apiKey',
message: 'Enter API key:',
validate: value => {
if (!value) return 'API key is required';
if (value.length !== 32) return 'API key must be 32 characters';
return true;
},
format: value => value.trim()
});List input prompt that splits user input into an array using a specified separator. The output is an array containing the trimmed string values.
{
/** Prompt type identifier */
type: 'list',
/** Property name for response object */
name: string,
/** Display message for user */
message: string,
/** Default string value */
initial?: string,
/** String separator for splitting input. Defaults to comma ',' */
separator?: string,
/** Input style */
style?: 'default' | 'password' | 'invisible' | 'emoji',
/** Custom formatter for the resulting array */
format?: (value: string[], values: Answers) => any,
/** Render callback with kleur styling */
onRender?: (kleur: any) => void,
/** State change callback */
onState?: (state: { value: string; aborted: boolean }) => void,
/** Input stream */
stdin?: NodeJS.ReadableStream,
/** Output stream */
stdout?: NodeJS.WritableStream
}Usage Examples:
// Basic list input with comma separator
const response = await prompts({
type: 'list',
name: 'keywords',
message: 'Enter keywords (comma separated):',
initial: 'javascript, node, cli'
});
// Result: { keywords: ['javascript', 'node', 'cli'] }
// List input with custom separator
const response = await prompts({
type: 'list',
name: 'paths',
message: 'Enter file paths (semicolon separated):',
separator: ';'
});
// List input with validation and formatting
const response = await prompts({
type: 'list',
name: 'emails',
message: 'Enter email addresses:',
validate: (values) => {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const invalid = values.find(email => !emailRegex.test(email));
return invalid ? `Invalid email: ${invalid}` : true;
},
format: (values) => values.map(email => email.toLowerCase())
});true for valid inputfalse for invalid input with default error messageInstall with Tessl CLI
npx tessl i tessl/npm-prompts