Gathering detailed insights and metrics for @inquirer/confirm
Gathering detailed insights and metrics for @inquirer/confirm
Gathering detailed insights and metrics for @inquirer/confirm
Gathering detailed insights and metrics for @inquirer/confirm
inquirer-confirm
Simple confirm yes/no question for CLI node applications, returns a promise, built on top of inquirer
inquirer-confirm-validated
Inquirer confirm prompt with validation
@inquirer/prompts
Inquirer prompts, combined in a single package
inquirer-checkbox-plus-prompt
Checkbox with autocomplete and other additions for Inquirer
A collection of common interactive command line user interfaces.
npm install @inquirer/confirm
@inquirer/editor@4.1.0
Published on 11 Nov 2024
@inquirer/core@10.1.0
Published on 11 Nov 2024
@inquirer/prompts@7.0.1
Published on 26 Oct 2024
@inquirer/prompts@7.0.0
Published on 06 Oct 2024
inquirer@12.0.0
Published on 06 Oct 2024
inquirer@11.1.0
Published on 27 Sept 2024
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
20,326 Stars
1,625 Commits
1,305 Forks
148 Watching
8 Branches
207 Contributors
Updated on 27 Nov 2024
Minified
Minified + Gzipped
TypeScript (91.38%)
JavaScript (8.62%)
Cumulative downloads
Total Downloads
Last day
0.7%
972,993
Compared to previous day
Last week
6.4%
5,073,425
Compared to previous week
Last month
30.1%
20,547,302
Compared to previous month
Last year
3,557.3%
96,968,955
Compared to previous year
2
1
4
A collection of common interactive command line user interfaces.
Give it a try in your own terminal!
1npx @inquirer/demo@latest
npm | yarn |
---|---|
|
|
[!NOTE] 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.
1import { input } from '@inquirer/prompts'; 2 3const answer = await input({ message: 'Enter your name' });
1import { input } from '@inquirer/prompts';
See documentation for usage example and options documentation.
1import { select } from '@inquirer/prompts';
See documentation for usage example and options documentation.
1import { checkbox } from '@inquirer/prompts';
See documentation for usage example and options documentation.
1import { confirm } from '@inquirer/prompts';
See documentation for usage example and options documentation.
1import { search } from '@inquirer/prompts';
See documentation for usage example and options documentation.
1import { password } from '@inquirer/prompts';
See documentation for usage example and options documentation.
1import { expand } from '@inquirer/prompts';
See documentation for usage example and options documentation.
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.)
1import { editor } from '@inquirer/prompts';
See documentation for usage example and options documentation.
Very similar to the input
prompt, but with built-in number validation configuration option.
1import { number } from '@inquirer/prompts';
See documentation for usage example and options documentation.
1import { rawlist } from '@inquirer/prompts';
See documentation for usage example and options documentation.
The API documentation is over here, and our testing utilities here.
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 |
signal | AbortSignal | no | An AbortSignal to cancel prompts asynchronously |
Example:
1import { confirm } from '@inquirer/prompts'; 2 3const allowEmail = await confirm( 4 { message: 'Do you allow us to send you email?' }, 5 { 6 output: new Stream.Writable({ 7 write(chunk, _encoding, next) { 8 // Do something 9 next(); 10 }, 11 }), 12 clearPromptOnDone: true, 13 }, 14);
This can preferably be done with either an AbortController
or AbortSignal
.
1// Example 1: using built-in AbortSignal utilities 2import { confirm } from '@inquirer/prompts'; 3 4const answer = await confirm({ ... }, { signal: AbortSignal.timeout(5000) });
1// Example 1: implementing custom cancellation logic 2import { confirm } from '@inquirer/prompts'; 3 4const controller = new AbortController(); 5setTimeout(() => { 6 controller.abort(); // This will reject the promise 7}, 5000); 8 9const answer = await confirm({ ... }, { signal: controller.signal });
Alternatively, 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.
1import { confirm } from '@inquirer/prompts'; 2 3const promise = confirm(...); // Warning: for this pattern to work, `await` cannot be used. 4 5promise.cancel();
ctrl+c
gracefullyWhen a user press ctrl+c
to exit a prompt, Inquirer rejects the prompt promise. This is the expected behavior in order to allow your program to teardown/cleanup its environment. When using async/await
, rejected promises throw their error. When unhandled, those errors print their stack trace in your user's terminal.
ExitPromptError: User force closed the prompt with 0 null
at file://example/packages/core/dist/esm/lib/create-prompt.js:55:20
at Emitter.emit (file://example/node_modules/signal-exit/dist/mjs/index.js:67:19)
at #processEmit (file://example/node_modules/signal-exit/dist/mjs/index.js:236:27)
at #process.emit (file://example/node_modules/signal-exit/dist/mjs/index.js:187:37)
at process.callbackTrampoline (node:internal/async_hooks:130:17)
This isn't a great UX, which is why we highly recommend you to handle those errors gracefully.
First option is to wrap your scripts in try/catch
; like we do in our demo program. Or handle the error in your CLI framework mechanism; for example Clipanion catch
method.
Lastly, you could handle the error globally with an event listener and silence it.
1process.on('uncaughtException', (error) => { 2 if (error instanceof Error && error.name === 'ExitPromptError') { 3 console.log('👋 until next time!'); 4 } else { 5 // Rethrow unknown errors 6 throw error; 7 } 8});
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.
1import { input, confirm } from '@inquirer/prompts'; 2 3const answers = { 4 firstName: await input({ message: "What's your first name?" }), 5 allowEmail: await confirm({ message: 'Do you allow us to send you email?' }), 6}; 7 8console.log(answers.firstName);
Maybe some questions depend on some other question's answer.
1import { input, confirm } from '@inquirer/prompts'; 2 3const allowEmail = await confirm({ message: 'Do you allow us to send you email?' }); 4 5let email; 6if (allowEmail) { 7 email = await input({ message: 'What is your email address' }); 8}
1import { input } from '@inquirer/prompts'; 2 3const answer = await input( 4 { message: 'Enter a value (timing out in 5 seconds)' }, 5 { signal: AbortSignal.timeout(5000) }, 6).catch((error) => { 7 if (error.name === 'AbortPromptError') { 8 return 'Default value'; 9 } 10 11 throw error; 12});
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:
1 "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)
1#!/bin/sh 2exec < /dev/tty 3 4node my-script.js
When using inquirer prompts with nodemon, you need to pass the --no-stdin
flag for everything to work as expected.
1npx nodemon ./packages/demo/demos/password.mjs --no-stdin
Note that for most of you, you'll be able to use the new watch-mode built-in Node. This mode works out of the box with inquirer.
1# One of depending on your need 2node --watch script.js 3node --watch-path=packages/ packages/demo/
Maybe some question configuration require to await a value.
1import { confirm } from '@inquirer/prompts'; 2 3const answer = await confirm({ message: await getMessage() });
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.
1Choose between choices? (Press <space> to select, <Up and Down> to move rows, 2<Left and Right> to move columns) 3 4┌──────────┬───────┬───────┐ 5│ 1-2 of 2 │ Yes? │ No? | 6├──────────┼───────┼───────┤ 7│ Choice 1 │ [ ◯ ] │ ◯ | 8├──────────┼───────┼───────┤ 9│ Choice 2 │ ◯ │ ◯ | 10└──────────┴───────┴───────┘ 11
Toggle Prompt
Confirm with a toggle. Select a choice with arrow keys + Enter.
? Do you want to continue? no / yes
Sortable Checkbox Prompt
The same as built-in checkbox prompt, but also allowing to reorder choices using ctrl+up/down.
? Which PRs and in what order would you like to merge? (Press <space> to select, <a> to toggle all, <i> to invert selection, <ctrl+up> to move item up, <ctrl+down> to move item down, and <enter> to proceed)
❯ ◯ PR 1
◯ PR 2
◯ PR 3
An inquirer select that supports multiple selections and filtering/searching.
? Choose your OS, IDE, PL, etc. (Press <tab> to select/deselect, <backspace> to remove selected
option, <enter> to select option)
>> vue
>[ ] vue
[ ] vuejs
[ ] fuelphp
[ ] venv
[ ] vercel
(Use arrow keys to reveal more options)
File Selector Prompt
A file selector, you can navigate freely between directories, choose what type of files you want to allow and it is fully customizable.
1? Select a file: 2/main/path/ 3├── folder1/ 4├── folder2/ 5├── folder3/ 6├── file1.txt 7├── file2.pdf 8└── file3.jpg (not allowed) 9━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 10Use ↑↓ to navigate through the list 11Press <esc> to navigate to the parent directory 12Press <enter> to select a file or navigate to a directory
Copyright (c) 2023 Simon Boudrias (twitter: @vaxilart)
Licensed under the MIT license.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
30 commit(s) and 20 issue activity found in the last 90 days -- score normalized to 10
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
SAST tool detected but not run on all commits
Details
Reason
1 existing vulnerabilities detected
Details
Reason
Found 4/15 approved changesets -- score normalized to 2
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More