SkillAgentSearch skills...

Prompts

❯ Lightweight, beautiful and user-friendly interactive prompts

Install / Use

/learn @terkelg/Prompts
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

<p align="center"> <img src="https://github.com/terkelg/prompts/raw/master/prompts.png" alt="Prompts" width="500" /> </p> <h1 align="center">❯ Prompts</h1> <p align="center"> <a href="https://npmjs.org/package/prompts"> <img src="https://img.shields.io/npm/v/prompts.svg" alt="version" /> </a> <a href="https://github.com/terkelg/prompts/actions/workflows/test.yml"> <img src="https://github.com/terkelg/prompts/actions/workflows/test.yml/badge.svg" alt="test" /> </a> <a href="https://npmjs.org/package/prompts"> <img src="https://img.shields.io/npm/dm/prompts.svg" alt="downloads" /> </a> <a href="https://licenses.dev/npm/prompts"> <img src="https://licenses.dev/b/npm/prompts" alt="licenses" /> </a> <!--- <a href="https://packagephobia.now.sh/result?p=prompts"> <img src="https://packagephobia.now.sh/badge?p=prompts" alt="install size" /> </a> ---> </p> <p align="center"> <b>Lightweight, beautiful and user-friendly interactive prompts</b><br /> <sub>>_ Easy to use CLI prompts to enquire users for information▌</sub> </p> <br />
  • 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.

split

❯ Install

$ npm install --save prompts

This package supports Node 14 and above

split

❯ 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.js for more options.

split

❯ 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);
})();

split

❯ 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' ] }
})();

split

❯ 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

View on GitHub
GitHub Stars9.3k
CategoryDevelopment
Updated1d ago
Forks320

Languages

JavaScript

Security Score

100/100

Audited on Apr 4, 2026

No findings