{"id":11913,"library":"read","title":"read - User Input from Stdin","description":"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.","status":"active","version":"5.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/npm/read","tags":["javascript","typescript"],"install":[{"cmd":"npm install read","lang":"bash","label":"npm"},{"cmd":"yarn add read","lang":"bash","label":"yarn"},{"cmd":"pnpm add read","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Used internally to manage stream muting, particularly for the `silent` and `replace` options.","package":"mute-stream","optional":false}],"imports":[{"note":"Since v3.0.0, `read` is primarily an ES Module (ESM) package. While `require` might work due to Node.js interoperability, `import` is the idiomatic and recommended way to use it in modern Node.js environments.","wrong":"const read = require('read')","symbol":"read","correct":"import { read } from 'read'"},{"note":"TypeScript types for the options object are shipped with the package since v3.0.0.","symbol":"ReadOptions","correct":"import type { ReadOptions } from 'read'"}],"quickstart":{"code":"import { read } from 'read';\n\nasync function getUserInput() {\n  try {\n    const username = await read({ prompt: 'Username: ' });\n    console.log(`Hello, ${username}!`);\n\n    const password = await read({\n      prompt: 'Password: ',\n      silent: true,\n      replace: '*',\n      timeout: 15000 // 15 seconds\n    });\n    console.log('Password received (not echoed).');\n\n    const favoriteColor = await read({\n      prompt: 'What is your favorite color? ',\n      default: 'blue',\n      edit: true,\n    });\n    console.log(`Your favorite color is: ${favoriteColor}`);\n\n  } catch (error) {\n    if (error instanceof Error && error.message === 'timed out') {\n      console.error('Input timed out. Please try again.');\n    } else {\n      console.error('An error occurred:', error);\n    }\n    process.exit(1);\n  }\n}\n\ngetUserInput();","lang":"typescript","description":"This example demonstrates prompting for username, a silent password input with timeout and character replacement, and an editable default value for favorite color, using the `read` function and its options."},"warnings":[{"fix":"Upgrade your Node.js version to meet the specified range.","message":"Node.js engine requirement updated. The package now requires Node.js `^20.17.0 || >=22.9.0`.","severity":"breaking","affected_versions":">=5.0.0"},{"fix":"Upgrade your Node.js version to meet the specified range or downgrade `read` to a compatible version.","message":"Node.js engine requirement updated. The package now requires Node.js `^18.17.0 || >=20.5.0`.","severity":"breaking","affected_versions":">=4.0.0 <5.0.0"},{"fix":"Update import statements from `const { read } = require('read')` to `import { read } from 'read'`. Ensure your project's `package.json` is configured for ESM (e.g., setting `\"type\": \"module\"`) or use appropriate tooling for ESM/CJS interop.","message":"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`).","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Refactor code to use `async/await` or `.then().catch()` with the returned Promise. Remove any `isDefault` checks from the resolved value.","message":"The API was refactored to be Promise-only. Callback-based usage is no longer supported. The Promise resolution no longer includes the `isDefault` boolean.","severity":"breaking","affected_versions":">=2.0.0 <3.0.0"},{"fix":"Ensure `read` is used in an interactive terminal environment for `silent` mode to function as intended. For non-interactive scripts, consider if `silent` is appropriate or if alternative input methods are more suitable.","message":"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.","severity":"gotcha","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"For CommonJS, use named destructuring: `const { read } = require('read')`. For ESM, use named import: `import { read } from 'read'`.","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.","error":"TypeError: read is not a function"},{"fix":"Change your import statement from `const { read } = require('read')` to `import { read } from 'read'`.","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`).","error":"ERR_REQUIRE_ESM"},{"fix":"Wrap your `await` call within an `async` function, or use `.then().catch()` with the Promise returned by `read`.","cause":"Using `await read(...)` outside of an `async` function.","error":"ReferenceError: await is not defined"}],"ecosystem":"npm"}