>_ Easy to use CLI prompts to inquire users for information ▌
- Simple: prompts has no big dependencies nor is it broken into a dozen tiny modules that only work well together.
- User friendly: prompt uses layout and colors to create beautiful cli interfaces.
- Promised: Uses promises and
async/await. No callback hell. - Flexible: All prompts are independet and can be used on their own.
$ npm install --save prompts
const prompts = require('prompts');
let response = await prompts({
type: 'number',
name: 'age',
message: 'How old are you?'
});
console.log(response);Prompt with a single prompt object. Returns object with repsonse.
const prompts = require('prompts');
let response = await prompts({
type: 'text',
name: 'meaning',
message: 'What is the meaning of life?'
});
// response => { name }Prompt with a list of prompt objects. Returns object with response.
Make sure to give each prompt a unique name property to prevent overwriting values.
const prompt = require('prompts');
let questions = [
{
type: 'text',
name: 'username',
message: 'What is your GitHub username?'
},
{
type: 'age',
name: 'age',
message: 'How old are you?'
},
{
type: 'text',
name: 'about',
message: 'Tell somethign about yourself',
initial: 'Why should I?'
}
];
let response = await prompts(questions);
// => response => { username, age, about }Prompt properties can be functions too.
Prompt Objects with type set to null are skipped.
const prompts = require('prompts');
let questions = [
{
type: 'text',
name: 'dish',
message: 'Do you like pizza?'
},
{
type: prev => prev.type == 'pizza' ? 'text' : null,
name: 'topping',
message: 'Name a topping'
}
];
let response = await prompts(questions);Type: Function
Returns: Object
Prompter function which takes your prompt objects and returns an object with answers
Type: Array|Object
Array of prompt objects. These are the qustions the user will be prompted. You can see the list of supported prompt types here.
Type: Function
Default: () => {}
Callback that's invoked after each prompt submission.
Its signature is (prompt, answer) where prompt is the current prompt object.
Return true to quit the prompt loop and return all collected answers so far, otherwise continue to iterate prompt objects.
Example:
let questions = [{ ... }];
let onSubmit = (prompt, answer) => console.log(`Thanks I got ${answer} from ${prompt.name}`);
let response = await prompts(questions, { onSubmit });
Type: Function
Default: () => {}
Callback that's invoked after when the user cancel/exit the prompt.
Its signature is (prompt) where prompt is the current prompt object.
Return true to quit the prompt loop and return all collected answers so far, otherwise continue to iterate prompt objects.
Example:
let questions = [{ ... }];
let onCancel = prompt => {
console.log('Lets stop prompting');
return true;
}
let response = await prompts(questions, { onCancel });
Prompts Objects are JavaScript objects that define the "questions" and the type of prompt. Almost all prompt objects have the following properties:
{
type: String || Function,
name: String || Function,
message: String || Function,
initial String || Function || Async Function
}If type is null the prompter will skip that qustion.
{
type: null,
name: 'forgetme',
message: 'I\'ll never be shown anyway',
}Each property can also be of type function and will be invoked right before prompting the user.
{
type: prev => prev >= 3 ? 'confirm' : null,
name: 'confirm',
message: (prev, values) => `Please confirm that you eat ${values.dish} times ${prev} a day?`
}Text prompt for free text input.
{
type: 'text',
name: 'dish',
message: `What's your twitter handle?`,
style: 'default',
initial: ''
}| Param | Type | Default | Description |
|---|---|---|---|
| message | string |
Prompt message to display | |
| initial | string |
'' |
Default string value |
| style | string |
'default' |
Render style (default, password, invisible) |
Password prompt with masked input.
This prompt is a similar to a prompt of type 'text' with style set to 'password'.
{
type: 'password',
name: 'secret',
message: 'Tell me a secret',
initial '',
}| Param | Type | Description |
|---|---|---|
| message | string |
Prompt message to display |
| initial | string |
Default string value |
Prompts user for invisible text input.
This prompt is similar to what you know from sudo.
This prompt is a simular to a prompt of type 'text' with style set to 'invisible'.
{
type: 'invisible',
name: 'secret',
message: 'Enter password',
initial: ''
}| Param | Type | Description |
|---|---|---|
| message | string |
Prompt message to display |
| initial | string |
Default string value |
Prompts user for number input.
You can use up/down to increase/decrease the value.
Only numbers are allowed as input. Default resolve value is null.
{
type: 'number'
name: 'count',
message: 'How old are you?',
initial: 0,
style: 'default',
min: 2,
max: 10
}| Param | Type | Default | Description |
|---|---|---|---|
| message | string |
Prompt message to display | |
| initial | number |
null |
Default number value |
| max | number |
Infinity |
Max value |
| min | number |
-infinity |
Min value |
| style | string |
'default' |
Render style (default, password, invisible) |
Classic yes/no prompt.
Hit y or n to confirm/reject.
{
type: 'confirm'
name: 'favorite',
message: 'Can you confirm?',
initial: true
}| Param | Type | Default | Description |
|---|---|---|---|
| message | string |
Prompt message to display | |
| initial | boolean |
false |
Default value |
List prompt that return an array.
Similar to the text prompt, but the output is an Array containing the
string separated by separator.
{
type: 'list'
name: 'keywords',
message: 'Enter keywords',
initial: '',
separator: ', '
}| Param | Type | Default | Description |
|---|---|---|---|
| message | string |
Prompt message to display | |
| initial | boolean |
false |
Default value |
| seperator | string |
", " |
String seperator |
Interactive toggle/switch prompt.
Use tab or arrow keys to switch between options.
{
type: 'toggle'
name: 'bacon',
message: 'Can you confirm?',
initial: true,
active: 'yes',
inactive: 'no'
}| Param | Type | Default | Description |
|---|---|---|---|
| message | string |
Prompt message to display | |
| initial | boolean |
false |
Default value |
| active | string |
'on' |
Text for active state |
| inactive | string |
'off' |
Text for inactive state |
Interactive select prompt.
Use space to select/unselect and arrow keys to navigate the list.
{
type: 'select',
name: 'actor',
message: 'Pick a color',
choices: [
{ title: 'Red', value: '#ff0000' },
{ title: 'Green', value: '#00ff00' },
{ title: 'Blue', value: '#0000ff' }
],
initial: 1
}| Param | Type | Description |
|---|---|---|
| message | string |
Prompt message to display |
| initial | number |
Index of default value |
| choices | Array |
Array of choices objects [{ title, value }, ...] |
Interactive multi-select prompt.
Use space to select/unselect and arrow keys to navigate the list.
By default this prompt returns an array containing the values of the selected items - not their display title.
{
type: 'multiselect',
name: 'colors',
message: 'Pick colors',
choices: [
{ title: 'Red', value: '#ff0000' },
{ title: 'Green', value: '#00ff00' },
{ title: 'Blue', value: '#0000ff', selected: true }
],
initial: 1,
max: 2,
hint: '- Space to select. Return to submit'
}| Param | Type | Description |
|---|---|---|
| message | string |
Prompt message to display |
| choices | Array |
Array of choices objects [{ title, value, [selected] }, ...] |
| max | number |
Max select |
| hint | string |
Hint to display user |
This is one of the few prompts that don't take a initial value.
If you want to predefine selected values, give the choice object an selected property of true.
Interactive auto complete prompt.
The prompt will list options based on user input.
The default suggets/filter function is based on the title property.
You can overwrite this by passing your own filter function.
{
type: 'autocomplete',
name: 'actor',
message: 'Pick your favorite color',
choices: [
{ title: 'Brad Pitt' },
{ title: 'George Clooney', value: 'silver-fox' },
{ title: 'Ana de Armas' },
{ title: 'Arnold' },
{ title: 'Felicity Jones' },
]
}| Param | Type | Default | Description |
|---|---|---|---|
| message | string |
Prompt message to display | |
| choices | Array |
Array of auto-complete choices objects [{ title, value }, ...] |
|
| suggest | function |
By title string |
Filter function. Defaults to stort by title property |
| limit | number |
10 |
Max number of results to show |
| style | string |
'default' |
Render style (default, password, invisible) |
Example on what a suggest function might look like:
const suggestByTitle = (input, choices) =>
Promise.resolve(choices.filter(i => i.title.slice(0, input.length) === input))Many of the prompts are based on the work of derhuerst.
MIT © Terkel Gjervig










