Prompts
❯ Lightweight, beautiful and user-friendly interactive prompts
Install / Use
/learn @terkelg/PromptsREADME
- 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 independent and can be used on their own.
- Testable: provides a way to submit answers programmatically.
- Unified: consistent experience across all prompts.

❯ Install
$ npm install --save prompts
This package supports Node 14 and above

❯ Usage
<img src="https://github.com/terkelg/prompts/raw/master/media/example.gif" alt="example prompt" width="499" height="103" />const prompts = require('prompts');
(async () => {
const response = await prompts({
type: 'number',
name: 'age',
message: 'How old are you?',
validate: value => value < 18 ? `Nightclub is 18+ only` : true
});
console.log(response); // => { age: 24 }
})();
See
example.jsfor more options.

❯ Examples
Single Prompt
Prompt with a single prompt object. Returns an object with the response.
const prompts = require('prompts');
(async () => {
const response = await prompts({
type: 'text',
name: 'meaning',
message: 'What is the meaning of life?'
});
console.log(response.meaning);
})();
Prompt Chain
Prompt with a list of prompt objects. Returns an object with the responses.
Make sure to give each prompt a unique name property to prevent overwriting values.
const prompts = require('prompts');
const questions = [
{
type: 'text',
name: 'username',
message: 'What is your GitHub username?'
},
{
type: 'number',
name: 'age',
message: 'How old are you?'
},
{
type: 'text',
name: 'about',
message: 'Tell something about yourself',
initial: 'Why should I?'
}
];
(async () => {
const response = await prompts(questions);
// => response => { username, age, about }
})();
Dynamic Prompts
Prompt properties can be functions too.
Prompt Objects with type set to falsy values are skipped.
const prompts = require('prompts');
const questions = [
{
type: 'text',
name: 'dish',
message: 'Do you like pizza?'
},
{
type: prev => prev == 'pizza' ? 'text' : null,
name: 'topping',
message: 'Name a topping'
}
];
(async () => {
const response = await prompts(questions);
})();

❯ API
prompts(prompts, options)
Type: Function<br>
Returns: Object
Prompter function which takes your prompt objects and returns an object with responses.
prompts
Type: Array|Object<br>
Array of prompt objects. These are the questions the user will be prompted. You can see the list of supported prompt types here.
Prompts can be submitted (<kbd>return</kbd>, <kbd>enter</kbd>) or canceled (<kbd>esc</kbd>, <kbd>abort</kbd>, <kbd>ctrl</kbd>+<kbd>c</kbd>, <kbd>ctrl</kbd>+<kbd>d</kbd>). No property is being defined on the returned response object when a prompt is canceled.
options.onSubmit
Type: Function<br>
Default: () => {}
Callback that's invoked after each prompt submission.
Its signature is (prompt, answer, answers) where prompt is the current prompt object, answer the user answer to the current question and answers the user answers so far. Async functions are supported.
Return true to quit the prompt chain and return all collected responses so far, otherwise continue to iterate prompt objects.
Example:
(async () => {
const questions = [{ ... }];
const onSubmit = (prompt, answer) => console.log(`Thanks I got ${answer} from ${prompt.name}`);
const response = await prompts(questions, { onSubmit });
})();
options.onCancel
Type: Function<br>
Default: () => {}
Callback that's invoked when the user cancels/exits the prompt.
Its signature is (prompt, answers) where prompt is the current prompt object and answers the user answers so far. Async functions are supported.
Return true to continue and prevent the prompt loop from aborting.
On cancel responses collected so far are returned.
Example:
(async () => {
const questions = [{ ... }];
const onCancel = prompt => {
console.log('Never stop prompting!');
return true;
}
const response = await prompts(questions, { onCancel });
})();
override
Type: Function
Preanswer questions by passing an object with answers to prompts.override.
Powerful when combined with arguments of process.
Example
const prompts = require('prompts');
prompts.override(require('yargs').argv);
(async () => {
const response = await prompts([
{
type: 'text',
name: 'twitter',
message: `What's your twitter handle?`
},
{
type: 'multiselect',
name: 'color',
message: 'Pick colors',
choices: [
{ title: 'Red', value: '#ff0000' },
{ title: 'Green', value: '#00ff00' },
{ title: 'Blue', value: '#0000ff' }
],
}
]);
console.log(response);
})();
inject(values)
Type: Function<br>
Programmatically inject responses. This enables you to prepare the responses ahead of time. If any injected value is found the prompt is immediately resolved with the injected value. This feature is intended for testing only.
values
Type: Array
Array with values to inject. Resolved values are removed from the internal inject array.
Each value can be an array of values in order to provide answers for a question asked multiple times.
If a value is an instance of Error it will simulate the user cancelling/exiting the prompt.
Example:
const prompts = require('prompts');
prompts.inject([ '@terkelg', ['#ff0000', '#0000ff'] ]);
(async () => {
const response = await prompts([
{
type: 'text',
name: 'twitter',
message: `What's your twitter handle?`
},
{
type: 'multiselect',
name: 'color',
message: 'Pick colors',
choices: [
{ title: 'Red', value: '#ff0000' },
{ title: 'Green', value: '#00ff00' },
{ title: 'Blue', value: '#0000ff' }
],
}
]);
// => { twitter: 'terkelg', color: [ '#ff0000', '#0000ff' ] }
})();

❯ Prompt Objects
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
format: Function | Async Function,
onRender: Function
onState: Function
stdin: Readable
stdout: Writeable
}
Each property be of type function and will be invoked right before prompting the user.
The function signature is (prev, values, prompt), where prev is the value from the previous prompt,
values is the response object with all values collected so far and prompt is the previous prompt object.
Function example:
{
type: prev => prev > 3 ? 'confirm' : null,
name: 'confirm',
message: (prev, values) => `Please confirm that you eat ${values.dish} times ${prev} a day?`
}
The above prompt will be skipped if the value of the previous prompt is less than 3.
type
Type: String|Function
Defines the type of prompt to display. See the list of prompt types for valid values.
If type is a falsy value the prompter will skip that question.
{
type: null,
name: 'forgetme',
message: `I'll never be shown anyway`,
}
name
Type: String|Function
The response will be saved under this key/property in the returned response object. In case you have multiple prompts with the same name only the latest response will be stored.
Make sure to give prompts unique names if you don't want to overwrite previous values.
message
Type: String|Function
The message to be displayed to the user.
initial
Type: String|Function
Optional default prompt value. Async functions are supported too.
format
Type: Function
Receive the user input and return the formatted value to be used inside the program. The value returned will be added to the response object.
The function signature is (val, values), where val is the valu
Related Skills
node-connect
348.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
108.9kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
348.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
348.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
