Bare Events
Bare Events is a lightweight and minimal event emitter library for JavaScript, designed for both Node.js and browser environments. It provides a simple API for event-driven programming, allowing developers to subscribe to and emit custom events efficiently. The package is actively maintained, with version 2.8.2 being the latest stable release as of late 2025/early 2026, and new versions released as needed. As part of the Holepunch ecosystem, it emphasizes a 'bare-bones' approach, contrasting with more feature-rich alternatives by focusing solely on core event emitting functionality. It ships with built-in TypeScript type declarations, making it well-suited for modern JavaScript and TypeScript projects that prioritize minimal overhead and explicit control over event handling.
Common errors
-
TypeError: EventEmitter is not a constructor
cause Attempting to instantiate `EventEmitter` when it was incorrectly imported as a default export in an ES Module context or when destructuring a CommonJS `require` call incorrectly.fixFor ES Modules: `import { EventEmitter } from 'bare-events';`. For CommonJS: `const EventEmitter = require('bare-events');` (without destructuring if the top-level export is the constructor). -
Unhandled 'error' event. ([Error object details])
cause An `error` event was emitted by an `EventEmitter` instance, but no listener was registered to handle it. In Node.js, this typically leads to a process crash.fixAlways register an error handler for any `EventEmitter` that might emit errors: `myEmitter.on('error', (err) => { console.error('Caught an error:', err); });` -
ReferenceError: require is not defined in ES module scope
cause Attempting to use `require()` in an ES Module (ESM) file (e.g., a `.mjs` file or a `.js` file in a package with `"type": "module"` in `package.json`).fixSwitch to ES Module `import` syntax: `import { EventEmitter } from 'bare-events';`
Warnings
- gotcha Failing to remove event listeners, especially for long-lived emitters or frequently created components, can lead to memory leaks. This is a common pitfall with all event emitter implementations.
- breaking In Node.js, if an `error` event is emitted and no listeners are attached to it, the process will crash. This default behavior can be unexpected for new users.
- gotcha The `this` context within event listener functions can be inconsistent depending on how the function is defined and called. Traditional `function` declarations will have `this` bound to the `EventEmitter` instance, but arrow functions retain the `this` from their lexical scope.
- gotcha When migrating from CommonJS `require()` to ES Modules `import`, ensure you use named imports (`import { EventEmitter } from 'bare-events'`) rather than default imports, as `bare-events` does not provide a default export for its main class.
Install
-
npm install bare-events -
yarn add bare-events -
pnpm add bare-events
Imports
- EventEmitter
import EventEmitter from 'bare-events'
import { EventEmitter } from 'bare-events' - EventEmitter
const EventEmitter = require('bare-events') - EventEmitter (type)
import type { EventEmitter } from 'bare-events'
Quickstart
import { EventEmitter } from 'bare-events';
interface MyEvents {
'data': (payload: string) => void;
'ready': () => void;
'error': (err: Error) => void;
}
// Create a new EventEmitter instance, typed for better safety
const myEmitter = new EventEmitter<MyEvents>();
// Subscribe to the 'data' event
myEmitter.on('data', (payload: string) => {
console.log(`Received data: ${payload}`);
});
// Subscribe to the 'ready' event, only once
myEmitter.once('ready', () => {
console.log('Emitter is ready!');
});
// Emit events
myEmitter.emit('data', 'Hello, world!');
myEmitter.emit('ready');
myEmitter.emit('data', 'Another piece of data!');
// Demonstrate removing a listener
const errorHandler = (err: Error) => console.error('Caught error:', err.message);
myEmitter.on('error', errorHandler);
myEmitter.emit('error', new Error('Something went wrong!'));
myEmitter.off('error', errorHandler); // Remove the specific listener
myEmitter.emit('error', new Error('This error will crash the process if no other listener is present.')); // No listener, will crash in Node.js