Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
keypress
✓ const keypress = require('keypress');
✗ import keypress from 'keypress';
This package is CommonJS-only, as it was released prior to widespread ESM adoption in Node.js.
keypress function call
✓ keypress(process.stdin);
✗ const handler = new keypress.Handler();
The default export is a function that must be called with a ReadableStream to enable keypress events. It does not expose a class or other properties.
keypress type (TypeScript)
✓ import keypress = require('keypress');
✗ import type { Keypress } from 'keypress';
For TypeScript, a CommonJS-style import is required. There are no built-in type definitions, and `@types/keypress` refers to a different library. The package lacks official TypeScript support.
This example demonstrates how to enable and listen for "keypress" events on `process.stdin`, handling basic input and a Ctrl+C exit condition.
const keypress = require('keypress');
// make `process.stdin` begin emitting "keypress" events
keypress(process.stdin);
// enable raw mode for direct key input without waiting for Enter
if (process.stdin.isTTY) {
process.stdin.setRawMode(true);
}
// listen for the "keypress" event
process.stdin.on('keypress', function (ch, key) {
console.log('got "keypress" event:', ch, key);
if (key && key.ctrl && key.name === 'c') {
console.log('Ctrl+C pressed. Exiting.');
process.stdin.pause(); // Stop listening for input
process.exit(); // Terminate the process
}
});
console.log('Press any key (Ctrl+C to exit)');
process.stdin.resume(); // Start listening for input
Debug
Known issues
breakingThis package was designed primarily for Node.js v0.8.x and earlier versions. Its behavior and necessity are highly inconsistent or deprecated in modern Node.js environments (Node.js 10+).fixFor modern Node.js applications, use `readline.emitKeypressEvents(process.stdin)` and `process.stdin.setRawMode(true)` from the built-in `readline` module instead.
affects: >=1.0.0 (Node.js versions)
gotchaThe `keypress` function must be explicitly called with a `ReadableStream` (e.g., `process.stdin`) to initiate keypress event emission. Merely requiring the module does not activate the functionality.fixEnsure you call `keypress(yourReadableStream)` after requiring the module to enable events.
affects: *
deprecatedThe package is unmaintained, with its last release (0.2.1) dating back over a decade (circa 2012-2013). Using abandoned software introduces significant security risks due to unpatched vulnerabilities and lack of modern best practices.fixMigrate to Node.js's native `readline` module for keypress event handling or consider a actively maintained alternative like `emit-keypress` if additional features are needed.
affects: *
gotchaTo receive individual keypress events without waiting for the Enter key, the input stream (e.g., `process.stdin`) must be set to raw mode using `process.stdin.setRawMode(true)`.fixAdd `process.stdin.setRawMode(true);` and `process.stdin.resume();` to your script before listening for keypress events. Remember to handle `process.exit()` or `process.stdin.pause()` to exit raw mode gracefully.
affects: *
Errors
Common errors & fixes
Error: require() is not defined in ES module scope
Attempting to use the CommonJS `require()` statement in a Node.js project configured to use ES Modules (e.g., via `"type": "module"` in `package.json` or `.mjs` files).
fixConvert your project to CommonJS, or use a dynamic import workaround if absolutely necessary: `const keypress = await import('keypress'); keypress.default(process.stdin);`. However, migrating to `readline.emitKeypressEvents` is strongly recommended for ESM projects. TypeError: keypress is not a function
The `keypress` module's default export is a function, not an object with methods. This error occurs if you try to access properties (e.g., `keypress.someMethod()`) on the imported `keypress` function.
fixCall `keypress` directly as a function with your `ReadableStream` (e.g., `keypress(process.stdin)`) to enable keypress events. The events are then emitted on the stream itself, not the `keypress` object.
No 'keypress' event emitted on `process.stdin`
The `keypress` module was imported but its main function was not called with `process.stdin` to activate the event emitter, or `process.stdin.setRawMode(true)` was not enabled.
fixEnsure both `keypress(process.stdin);` and `process.stdin.setRawMode(true); process.stdin.resume();` are called in your script before attaching an event listener to `process.stdin`.
Audit
Dependencies
No dependency data recorded yet.