A collection of common interactive command line user interfaces.
Installations
npm install @inquirer/core
Releases
@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
Developer
SBoudrias
Developer Guide
Module System
ESM
Min. Node Version
>=18
Typescript Support
Yes
Node Version
22.9.0
NPM Version
lerna/8.1.9/node@v22.9.0+arm64 (darwin)
Statistics
20,326 Stars
1,625 Commits
1,305 Forks
148 Watching
8 Branches
207 Contributors
Updated on 27 Nov 2024
Languages
TypeScript (91.38%)
JavaScript (8.62%)
Total Downloads
Cumulative downloads
Total Downloads
161,124,023
Last day
-0.4%
1,304,537
Compared to previous day
Last week
4.2%
6,878,270
Compared to previous week
Last month
31.8%
28,289,407
Compared to previous month
Last year
1,963.9%
153,276,989
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Inquirer
A collection of common interactive command line user interfaces.
Give it a try in your own terminal!
1npx @inquirer/demo@latest
Installation
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.
Usage
1import { input } from '@inquirer/prompts'; 2 3const answer = await input({ message: 'Enter your name' });
Prompts
Input
1import { input } from '@inquirer/prompts';
See documentation for usage example and options documentation.
Select
1import { select } from '@inquirer/prompts';
See documentation for usage example and options documentation.
Checkbox
1import { checkbox } from '@inquirer/prompts';
See documentation for usage example and options documentation.
Confirm
1import { confirm } from '@inquirer/prompts';
See documentation for usage example and options documentation.
Search
1import { search } from '@inquirer/prompts';
See documentation for usage example and options documentation.
Password
1import { password } from '@inquirer/prompts';
See documentation for usage example and options documentation.
Expand
1import { 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.)
1import { editor } from '@inquirer/prompts';
See documentation for usage example and options documentation.
Number
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.
Raw List
1import { 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 |
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);
Canceling prompt
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();
Recipes
Handling ctrl+c
gracefully
When 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});
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.
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);
Ask a question conditionally
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}
Get default value after timeout
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});
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:
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
Using with nodemon
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/
Wait for config
Maybe some question configuration require to await a value.
1import { confirm } from '@inquirer/prompts'; 2 3const 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.
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
License
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
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
security policy file detected
Details
- Info: security policy file detected: SECURITY.md:1
- Info: Found linked content: SECURITY.md:1
- Warn: One or no descriptive hints of disclosure, vulnerability, and/or timelines in security policy
- Info: Found text in security policy: SECURITY.md:1
Reason
SAST tool detected but not run on all commits
Details
- Info: SAST configuration detected: CodeQL
- Warn: 18 commits out of 22 are checked with a SAST tool
Reason
1 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp
Reason
Found 4/15 approved changesets -- score normalized to 2
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Info: jobLevel 'actions' permission set to 'read': .github/workflows/codeql-analysis.yml:23
- Info: jobLevel 'contents' permission set to 'read': .github/workflows/codeql-analysis.yml:24
- Warn: no topLevel permission defined: .github/workflows/codeql-analysis.yml:1
- Warn: no topLevel permission defined: .github/workflows/main.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:34: update your workflow using https://app.stepsecurity.io/secureworkflow/SBoudrias/Inquirer.js/codeql-analysis.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:37: update your workflow using https://app.stepsecurity.io/secureworkflow/SBoudrias/Inquirer.js/codeql-analysis.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:48: update your workflow using https://app.stepsecurity.io/secureworkflow/SBoudrias/Inquirer.js/codeql-analysis.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:91: update your workflow using https://app.stepsecurity.io/secureworkflow/SBoudrias/Inquirer.js/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:93: update your workflow using https://app.stepsecurity.io/secureworkflow/SBoudrias/Inquirer.js/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:99: update your workflow using https://app.stepsecurity.io/secureworkflow/SBoudrias/Inquirer.js/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:115: update your workflow using https://app.stepsecurity.io/secureworkflow/SBoudrias/Inquirer.js/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:117: update your workflow using https://app.stepsecurity.io/secureworkflow/SBoudrias/Inquirer.js/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:123: update your workflow using https://app.stepsecurity.io/secureworkflow/SBoudrias/Inquirer.js/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:159: update your workflow using https://app.stepsecurity.io/secureworkflow/SBoudrias/Inquirer.js/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:161: update your workflow using https://app.stepsecurity.io/secureworkflow/SBoudrias/Inquirer.js/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:167: update your workflow using https://app.stepsecurity.io/secureworkflow/SBoudrias/Inquirer.js/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:17: update your workflow using https://app.stepsecurity.io/secureworkflow/SBoudrias/Inquirer.js/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:19: update your workflow using https://app.stepsecurity.io/secureworkflow/SBoudrias/Inquirer.js/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:25: update your workflow using https://app.stepsecurity.io/secureworkflow/SBoudrias/Inquirer.js/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:39: update your workflow using https://app.stepsecurity.io/secureworkflow/SBoudrias/Inquirer.js/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:41: update your workflow using https://app.stepsecurity.io/secureworkflow/SBoudrias/Inquirer.js/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:47: update your workflow using https://app.stepsecurity.io/secureworkflow/SBoudrias/Inquirer.js/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:70: update your workflow using https://app.stepsecurity.io/secureworkflow/SBoudrias/Inquirer.js/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:72: update your workflow using https://app.stepsecurity.io/secureworkflow/SBoudrias/Inquirer.js/main.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/main.yml:78: update your workflow using https://app.stepsecurity.io/secureworkflow/SBoudrias/Inquirer.js/main.yml/main?enable=pin
- Info: 0 out of 20 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
Score
6.2
/10
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