What is prompts?
The 'prompts' npm package is a lightweight, beautiful, and user-friendly command-line prompts library. It helps developers create interactive command-line interfaces (CLIs) with ease. It supports various types of input, validation, and conditional prompts.
What are prompts's main functionalities?
Text Input
This feature allows for simple text input. The user is prompted to enter their name, and the input is then greeted.
const prompts = require('prompts');
(async () => {
const response = await prompts({
type: 'text',
name: 'name',
message: 'What is your name?'
});
console.log(`Hello, ${response.name}!`);
})();
Password Input
This feature is for password input where the input is masked. The user is prompted to enter a password without displaying it on the screen.
const prompts = require('prompts');
(async () => {
const response = await prompts({
type: 'password',
name: 'password',
message: 'Enter your password'
});
console.log(`Your password is ${response.password}`);
})();
Number Input
This feature allows for number input. The user is prompted to enter their age, and the input is then confirmed.
const prompts = require('prompts');
(async () => {
const response = await prompts({
type: 'number',
name: 'age',
message: 'How old are you?'
});
console.log(`You are ${response.age} years old.`);
})();
Confirmation
This feature is for yes/no confirmation prompts. The user is asked to confirm, and the response is a boolean.
const prompts = require('prompts');
(async () => {
const response = await prompts({
type: 'confirm',
name: 'confirm',
message: 'Can you confirm?'
});
console.log(`You ${response.confirm ? 'confirmed' : 'did not confirm'}.`);
})();
Choice Selection
This feature allows users to select from a list of choices. The user is prompted to pick a color from a list, and the selected value is then displayed.
const prompts = require('prompts');
(async () => {
const response = await prompts({
type: 'select',
name: 'color',
message: 'Pick a color',
choices: [
{ title: 'Red', value: '#ff0000' },
{ title: 'Green', value: '#00ff00' },
{ title: 'Blue', value: '#0000ff' }
]
});
console.log(`You picked ${response.color}`);
})();
Multiple Selection
This feature allows users to select multiple items from a list. The user is prompted to select fruits, and the selected values are then displayed.
const prompts = require('prompts');
(async () => {
const response = await prompts({
type: 'multiselect',
name: 'fruits',
message: 'Select fruits',
choices: [
{ title: 'Apple', value: 'apple' },
{ title: 'Orange', value: 'orange' },
{ title: 'Pear', value: 'pear' }
]
});
console.log(`You selected ${response.fruits.join(', ')}`);
})();
Other packages similar to prompts
inquirer
Inquirer.js is a common interactive command-line user interfaces library. It is more feature-rich and extensible than 'prompts', offering a wider variety of question types and more complex interactions, but it is also heavier.
enquirer
Enquirer is a stylish CLI prompts library. It is similar to 'prompts' but offers more prompt types and is more customizable. It is designed with performance in mind and is suitable for applications requiring a high degree of user interaction.
readline-sync
Readline-sync is a synchronous readline for interactively running to have a conversation with the user via a console(TTY). It's simpler and only supports synchronous operations, unlike 'prompts' which is designed around promises and async/await.