read - User Input from Stdin
The `read` package provides a utility for securely and flexibly reading user input from `stdin` in Node.js applications. It serves as an enhanced alternative to Node's built-in `readline.question()` method, offering additional features such as silent input (e.g., for passwords), character replacement during silent input, input timeouts, default values, editable default values, terminal forcing, and auto-completion via a `completer` option. As of version 5.0.1, the package is actively maintained by the npm team and targets modern Node.js environments, requiring `^20.17.0 || >=22.9.0`. Major releases (e.g., v2.0.0, v3.0.0, v4.0.0, v5.0.0) typically introduce breaking changes related to Node.js version compatibility or API shifts, with minor releases adding new features like the `history` parameter in v4.1.0. The library primarily uses a Promise-based API for handling input, reflecting modern JavaScript asynchronous patterns.
Common errors
-
TypeError: read is not a function
cause Attempting to destructure `read` from a default `require` import (e.g., `const read = require('read'); read({ prompt: '...' });`) or trying to use it as a default import when it's a named export.fixFor CommonJS, use named destructuring: `const { read } = require('read')`. For ESM, use named import: `import { read } from 'read'`. -
ERR_REQUIRE_ESM
cause Attempting to `require()` the `read` package in a Node.js environment where it is treated as an ES Module (e.g., in a project with `"type": "module"` in `package.json`).fixChange your import statement from `const { read } = require('read')` to `import { read } from 'read'`. -
ReferenceError: await is not defined
cause Using `await read(...)` outside of an `async` function.fixWrap your `await` call within an `async` function, or use `.then().catch()` with the Promise returned by `read`.
Warnings
- breaking Node.js engine requirement updated. The package now requires Node.js `^20.17.0 || >=22.9.0`.
- breaking Node.js engine requirement updated. The package now requires Node.js `^18.17.0 || >=20.5.0`.
- breaking The package was converted to TypeScript and ES Modules (ESM). This change primarily affects how the package is imported and consumed, particularly for projects using CommonJS (`require`).
- breaking The API was refactored to be Promise-only. Callback-based usage is no longer supported. The Promise resolution no longer includes the `isDefault` boolean.
- gotcha Using the `silent: true` option with non-TTY input streams (e.g., redirected input or pipes) will not truly silence input and may behave unexpectedly as raw mode cannot be reliably set. This option is most effective in interactive terminal environments.
Install
-
npm install read -
yarn add read -
pnpm add read
Imports
- read
const read = require('read')import { read } from 'read' - ReadOptions
import type { ReadOptions } from 'read'
Quickstart
import { read } from 'read';
async function getUserInput() {
try {
const username = await read({ prompt: 'Username: ' });
console.log(`Hello, ${username}!`);
const password = await read({
prompt: 'Password: ',
silent: true,
replace: '*',
timeout: 15000 // 15 seconds
});
console.log('Password received (not echoed).');
const favoriteColor = await read({
prompt: 'What is your favorite color? ',
default: 'blue',
edit: true,
});
console.log(`Your favorite color is: ${favoriteColor}`);
} catch (error) {
if (error instanceof Error && error.message === 'timed out') {
console.error('Input timed out. Please try again.');
} else {
console.error('An error occurred:', error);
}
process.exit(1);
}
}
getUserInput();