Skip to content

Prompts Reference

Coverage License NPM Version Open Issues Size

⁉️ CLI interactive prompts. Can be used to wrap anything that matches the interface of node:readline/promises.

Usage

📦 Node

Install @lou.codes/prompts as a dependency:

Terminal window
pnpm add @lou.codes/prompts
# or
npm install @lou.codes/prompts
# or
yarn add @lou.codes/prompts

Import it and use it:

import { question } from "@lou.codes/prompts";
import { createInterface } from "node:readline/promises";
const exampleQuestion = question(
createInterface({
input: process.stdin,
output: process.stdout,
}),
);
exampleQuestion({
format: value => parseInt(value, 18),
query: "How old are you?",
validate: value => (value < 18 ? "You must be at least 18 years old." : ""),
})
.then(console.log)
.catch(console.error)
.finally(() => readlineInterface.close());

🦕 Deno

Import @lou.codes/prompts using the npm: prefix, and use it directly with the native prompt:

import { question } from "npm:@lou.codes/prompts";
const exampleQuestion = question({
question: query => Promise.resolve(prompt(query)),
});
exampleQuestion({
format: value => parseInt(value, 18),
query: "How old are you?",
validate: value => (value < 18 ? "You must be at least 18 years old." : ""),
})
.then(console.log)
.catch(console.error);

🌎 Browser

Import @lou.codes/prompts using esm.sh, and use it directly with the native prompt:

import { question } from "https://esm.sh/@lou.codes/prompts";
const exampleQuestion = question({
question: query => Promise.resolve(prompt(query)),
});
exampleQuestion({
format: value => parseInt(value, 18),
query: "How old are you?",
validate: value => (value < 18 ? "You must be at least 18 years old." : ""),
})
.then(console.log)
.catch(console.error);

Type Aliases

QuestionObject

Ƭ QuestionObject: Object

Question object.

Remarks

Small abstraction layer on top the question method of Node’s readline module.

Type declaration

NameTypeDescription
question(query: string) => Awaitable<string>Question function.

View source


QuestionOptions

Ƭ QuestionOptions<FormattedValue>: Object

Options object for the question function.

Type parameters

NameType
FormattedValuestring

Type declaration

NameTypeDescription
format (optional)Unary<string, FormattedValue>Function to format the question’s answer given by the user. Example typescript { format: value => parseInt(value, 18), } Param The question’s answer given by the user.
querystringQuery to show to the user.
retry (optional)booleanWhether to retry the question if the answer is invalid.
validate (optional)Unary<FormattedValue, Maybe<string>>Function to validate the question’s answer given by the user (after formatting). Returning an string will be used to reject with an error, while an empty string or undefined is considered valid. Example typescript { validate: value => value % 2 === 0, } Param The question’s answer given by the user.

View source

Functions

question

question(questionObject): <FormattedValue>(options: QuestionOptions<FormattedValue>) => Promise<FormattedValue>

Interactive question.

Parameters

NameTypeDescription
questionObjectQuestionObjectObject with a question function that returns a promise.

Returns

fn

Curried function with questionObject set in context.

▸ <FormattedValue>(options): Promise<FormattedValue>

Interactive question with questionObject set in context.

Type parameters
NameTypeDescription
FormattedValuestringResult of the value after formatting.
Parameters
NameTypeDescription
optionsQuestionOptions<FormattedValue>Options object for the question.
Returns

Promise<FormattedValue>

Promise with the question’s answer.

See

question

Remarks

Small abstraction layer on top the question method of Node’s readline module.

Example

import { createInterface } from "node:readline/promises";
const exampleQuestion = question(
createInterface({
input: process.stdin,
output: process.stdout,
}),
);
exampleQuestion({
format: value => parseInt(value, 18),
query: "How old are you?",
validate: value => (value < 18 ? "You must be at least 18 years old." : ""),
})
.then(console.log)
.catch(console.error);

See

View source