cli-prompt: Tiny CLI Prompter
cli-prompt is a lightweight utility designed for creating interactive command-line prompts in Node.js applications. It provides a simple, callback-based API for collecting user input, including basic text prompts, hidden password input, and multi-question sequences. Despite its functionality, the package is considered abandoned, with its last known publication being `0.6.0` over 10 years ago and no significant updates or active maintenance since 2013. This project distinguishes itself through its minimal footprint and straightforward API, contrasting with more modern, promise-based or feature-rich prompt libraries that offer advanced validation, styling, and asynchronous patterns. Due to its abandonment, developers should consider its lack of ongoing support and potential compatibility or security issues.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use `require('cli-prompt')` in an ES Module (ESM) context.fixThis package is CommonJS-only. Either switch your project to CommonJS, use dynamic `import('cli-prompt')` (if feasible), or migrate to a modern, ESM-compatible CLI prompting library. -
Error: STDIN has ended
cause The `cli-prompt` library attempted to read input from `stdin`, but the input stream was already closed or not available (e.g., in a non-interactive script or piped input).fixEnsure the application is run in an interactive terminal. Provide an `onError` callback to handle this gracefully, or check `process.stdin.isTTY` before attempting to prompt. -
Error: password must be at least 5 characters long
cause Custom validation function for a `password` type in `prompt.multi` threw an error because the input did not meet the specified length requirement.fixThe user input for the password did not satisfy the validation rules. Correct the input or adjust the `validate` function logic if the rules are too strict.
Warnings
- breaking This package is effectively abandoned, with its last update over a decade ago. It does not receive security patches, bug fixes, or new feature development. Using it in production environments is not recommended due to potential unpatched vulnerabilities and lack of modern JavaScript support.
- gotcha The package is CommonJS-only and relies on `require()`. It will not work directly in ES Module (ESM) environments without a bundler or compatibility layer. Attempting to `import` it will result in an error like 'require is not defined' or 'ERR_REQUIRE_ESM'.
- gotcha The `prompt` functions are callback-based and do not return Promises. This makes them incompatible with modern async/await patterns without manual Promise wrapping. The API is entirely synchronous in its execution flow, handling input via callbacks.
- gotcha cli-prompt will not work if STDIN has already ended. If `onError` is not provided, this condition might lead to uncaught errors or silent failures in non-interactive environments.
Install
-
npm install cli-prompt -
yarn add cli-prompt -
pnpm add cli-prompt
Imports
- prompt
const prompt = require('cli-prompt'); - prompt.password
const prompt = require('cli-prompt'); prompt.password('...', cb); - prompt.multi
const prompt = require('cli-prompt'); prompt.multi([...], cb);
Quickstart
const prompt = require('cli-prompt');
console.log('Welcome! Please answer a few questions.');
prompt.multi([
{
key: 'username',
label: 'Enter your desired username',
default: 'guest_user'
},
{
label: 'Enter your password (min 5 chars)',
key: 'password',
type: 'password',
validate: function (val) {
if (val.length < 5) {
throw new Error('Password must be at least 5 characters long');
}
}
},
{
label: 'How many pets do you have?',
key: 'pets',
type: 'number',
default: function () {
// Example of dynamic default based on previous input
return this.password ? this.password.length : 0;
}
},
{
label: 'Do you agree to the terms? (yes/no)',
key: 'agree',
type: 'boolean'
}
], function (result) {
console.log('\nThank you for your input:');
console.log(JSON.stringify(result, null, 2));
process.exit(0);
}, function (err) {
console.error('\nAn error occurred during prompting: ' + err);
process.exit(1);
});