termprompt

input

Free text input with optional validation.

Preview

Typing

input
Project name?
my-app

Validation error

input
Project name?
Name is required

Usage

import { input, isCancel } from 'termprompt';

const name = await input({
  message: 'Project name?',
  placeholder: 'my-app',
});

if (isCancel(name)) process.exit(0);
console.log(name);

Props

PropTypeDefaultDescription
messagestringrequiredQuestion text
placeholderstring?noneGray text shown when empty
initialValuestring""Pre-filled value
validate(value: string) => true | stringnoneReturn true or an error message

Validation

The validate function runs on submit. If it returns a string, that string is shown as an error and the prompt stays open.

const port = await input({
  message: 'Port number?',
  validate(value) {
    const n = Number(value);
    if (isNaN(n) || n < 1 || n > 65535) return 'Enter a valid port (1-65535)';
    return true;
  },
});

Keyboard

KeyAction
Left / RightMove cursor
Home / Ctrl+AGo to start
End / Ctrl+EGo to end
Ctrl+UClear line
Ctrl+WDelete word back
BackspaceDelete character
DeleteDelete forward
EnterSubmit
EscCancel

On this page