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.
npm install bare-eventsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create a typed event emitter, subscribe to events with `on` and `once`, emit events with `emit`, and properly clean up listeners using `off`.
Always pair `on` or `once` with a corresponding `off` call when the listener is no longer needed. Consider using `AbortController` (via `bare-abort-controller`) to manage listener lifecycles, or ensure components clean up their listeners on unmount/destroy.
Always add a listener for the `error` event on any `EventEmitter` instance that might emit errors. For example: `myEmitter.on('error', (err) => console.error('Caught:', err));`Use arrow functions (`(payload) => { /* ... */ }`) if you want `this` to refer to the surrounding lexical context. If you explicitly need `this` to be the `EventEmitter` instance, use a traditional `function (payload) { /* ... */ }` declaration or manually bind the function (e.g., `listener.bind(myEmitter)`).Update `import EventEmitter from 'bare-events'` to `import { EventEmitter } from 'bare-events'` in ES Module files. Ensure your build configuration correctly handles module resolution for both CJS and ESM if you have a mixed codebase.For ES Modules: `import { EventEmitter } from 'bare-events';`. For CommonJS: `const EventEmitter = require('bare-events');` (without destructuring if the top-level export is the constructor).Always register an error handler for any `EventEmitter` that might emit errors: `myEmitter.on('error', (err) => { console.error('Caught an error:', err); });`Switch to ES Module `import` syntax: `import { EventEmitter } from 'bare-events';`