npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@inquirer/prompts

v5.0.2

Published

Inquirer prompts, combined in a single package

Downloads

994,363

Readme

Inquirer

npm FOSSA Status

A collection of common interactive command line user interfaces.

List prompt

Give it a try in your own terminal!

npx @inquirer/demo@latest

Installation

npm install @inquirer/prompts

yarn add @inquirer/prompts

Inquirer recently underwent a rewrite from the ground up to reduce the package size and improve performance. The previous version of the package is still maintained (though not actively developed), and offered hundreds of community contributed prompts that might not have been migrated to the latest API. If this is what you're looking for, the previous package is over here.

Usage

import { input } from '@inquirer/prompts';

const answer = await input({ message: 'Enter your name' });

Prompts

Input

Input prompt

import { input } from '@inquirer/prompts';

See documentation for usage example and options documentation.

Select

Select prompt

import { select } from '@inquirer/prompts';

See documentation for usage example and options documentation.

Checkbox

Checkbox prompt

import { checkbox } from '@inquirer/prompts';

See documentation for usage example and options documentation.

Confirm

Confirm prompt

import { confirm } from '@inquirer/prompts';

See documentation for usage example and options documentation.

Password

Password prompt

import { password } from '@inquirer/prompts';

See documentation for usage example and options documentation.

Expand

Expand prompt closed Expand prompt expanded

import { expand } from '@inquirer/prompts';

See documentation for usage example and options documentation.

Editor

Launches an instance of the users preferred editor on a temporary file. Once the user exits their editor, the content of the temporary file is read as the answer. The editor used is determined by reading the $VISUAL or $EDITOR environment variables. If neither of those are present, the OS default is used (notepad on Windows, vim on Mac or Linux.)

import { editor } from '@inquirer/prompts';

See documentation for usage example and options documentation.

Raw List

Raw list prompt

import { rawlist } from '@inquirer/prompts';

See documentation for usage example and options documentation.

Create your own prompts

The API documentation is over here, and our testing utilities here.

Advanced usage

All inquirer prompts are a function taking 2 arguments. The first argument is the prompt configuration (unique to each prompt). The second is providing contextual or runtime configuration.

The context options are:

| Property | Type | Required | Description | | ----------------- | ----------------------- | -------- | ------------------------------------------------------------ | | input | NodeJS.ReadableStream | no | The stdin stream (defaults to process.stdin) | | output | NodeJS.WritableStream | no | The stdout stream (defaults to process.stdout) | | clearPromptOnDone | boolean | no | If true, we'll clear the screen after the prompt is answered |

Example:

import { confirm } from '@inquirer/prompts';

const allowEmail = await confirm(
  { message: 'Do you allow us to send you email?' },
  {
    output: new Stream.Writable({
      write(chunk, _encoding, next) {
        // Do something
        next();
      },
    }),
    clearPromptOnDone: true,
  },
);

Canceling prompt

All prompt functions are returning a cancelable promise. This special promise type has a cancel method that'll cancel and cleanup the prompt.

On calling cancel, the answer promise will become rejected.

import { confirm } from '@inquirer/prompts';

const answer = confirm(...); // note: for this you cannot use `await`

answer.cancel();

Recipes

Get answers in an object

When asking many questions, you might not want to keep one variable per answer everywhere. In which case, you can put the answer inside an object.

import { input, confirm } from '@inquirer/prompts';

const answers = {
  firstName: await input({ message: "What's your first name?" }),
  allowEmail: await confirm({ message: 'Do you allow us to send you email?' }),
};

console.log(answers.firstName);

Ask a question conditionally

Maybe some questions depend on some other question's answer.

import { input, confirm } from '@inquirer/prompts';

const allowEmail = await confirm({ message: 'Do you allow us to send you email?' });

let email;
if (allowEmail) {
  email = await input({ message: 'What is your email address' });
}

Get default value after timeout

import { setTimeout } from 'node:timers/promises';
import { input } from '@inquirer/prompts';

const ac = new AbortController();
const prompt = input({
  message: 'Enter a value (timing out in 5 seconds)',
});

prompt
  .finally(() => {
    ac.abort();
  })
  // Silencing the cancellation error.
  .catch(() => {});

const defaultValue = setTimeout(5000, 'timeout', { signal: ac.signal }).then(() => {
  prompt.cancel();
  return 'Timed out!';
});

const answer = await Promise.race([defaultValue, prompt]);

Using as pre-commit/git hooks, or scripts

By default scripts ran from tools like husky/lint-staged might not run inside an interactive shell. In non-interactive shell, Inquirer cannot run, and users cannot send keypress events to the process.

For it to work, you must make sure you start a tty (or "interactive" input stream.)

If those scripts are set within your package.json, you can define the stream like so:

  "precommit": "my-script < /dev/tty"

Or if in a shell script file, you'll do it like so: (on Windows that's likely your only option)

#!/bin/sh
exec < /dev/tty

node my-script.js

Wait for config

Maybe some question configuration require to await a value.

import { confirm } from '@inquirer/prompts';

const answer = await confirm({ message: await getMessage() });

Community prompts

If you created a cool prompt, send us a PR adding it to the list below!

Interactive List Prompt Select a choice either with arrow keys + Enter or by pressing a key associated with a choice.

? Choose an option:
>   Run command (D)
    Quit (Q)

Action Select Prompt Choose an item from a list and choose an action to take by pressing a key.

? Choose a file Open <O> Edit <E> Delete <X>
❯ image.png
  audio.mp3
  code.py

Table Multiple Prompt Select multiple answer from a table display.

Choose between choices? (Press <space> to select, <Up and Down> to move rows,
<Left and Right> to move columns)

┌──────────┬───────┬───────┐
│ 1-2 of 2 │ Yes?  │ No?   |
├──────────┼───────┼───────┤
│ Choice 1 │ [ ◯ ] │   ◯   |
├──────────┼───────┼───────┤
│ Choice 2 │   ◯   │   ◯   |
└──────────┴───────┴───────┘

Toggle Prompt Confirm with a toggle. Select a choice with arrow keys + Enter.

? Do you want to continue? no / yes

License

Copyright (c) 2023 Simon Boudrias (twitter: @vaxilart) Licensed under the MIT license.