API Reference

Prompts

Lightweight, dependency-free CLI prompt library supporting text, select, multiselect, and confirm question types.

Official Documentation ↗
CompatibilityThis documentation covers version v2. Check the official docs for the latest API surface.

Installation

npmnpm install prompts
pnpmpnpm add prompts

Methods

Overview

Prompts is a lightweight CLI prompt library supporting text, select, multiselect, and confirm questions without heavy dependencies. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.

Core APIs

  • prompts — Runs one or more questions and resolves with the answers keyed by each question name.
  • select question — Renders a single-choice list and returns the selected value or its name.
  • onCancel — Hooks into cancellation so scripts can abort gracefully when the user presses Ctrl+C.

Example

const prompts = require('prompts');

const res = await prompts({
  type: 'text',
  name: 'project',
  message: 'Project name?',
});
console.log(res.project);

Notes

Cancelled prompts yield undefined answers, so always check before using them. Provide onCancel for a graceful exit.

Verification

Run a text prompt with an answer and confirm the resolved object contains that value under the question name.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/terkelg/prompts@latest/readme.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:51:53.629Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

Security notes

Quoted verbatim from the official documentation; this is not a complete threat model. 来源:https://cdn.jsdelivr.net/gh/terkelg/prompts@latest/readme.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:51:53.629Z.

FAQ

How do I cancel prompts programmatically?
Pass an AbortSignal to the prompt via its options, or rely on the onCancel handler when the user interrupts.
Can I validate answers before accepting?
Set a validate function on a question; returning a string shows an error message and re-prompts.
Why are answers undefined after cancelling?
Cancelling leaves answer keys undefined; check for that and abort the flow instead of proceeding.