{"id":10559,"library":"bare-events","title":"Bare Events","description":"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.","status":"active","version":"2.8.2","language":"javascript","source_language":"en","source_url":"https://github.com/holepunchto/bare-events","tags":["javascript","typescript"],"install":[{"cmd":"npm install bare-events","lang":"bash","label":"npm"},{"cmd":"yarn add bare-events","lang":"bash","label":"yarn"},{"cmd":"pnpm add bare-events","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency, likely used for advanced event cancellation mechanisms, providing a standard way to signal cancellation to event listeners or asynchronous operations.","package":"bare-abort-controller","optional":false}],"imports":[{"note":"Bare Events uses named exports for its primary `EventEmitter` class, even in ESM contexts. Direct default imports will result in `undefined` or an error.","wrong":"import EventEmitter from 'bare-events'","symbol":"EventEmitter","correct":"import { EventEmitter } from 'bare-events'"},{"note":"This is the standard CommonJS import pattern, as shown in the package's documentation.","symbol":"EventEmitter","correct":"const EventEmitter = require('bare-events')"},{"note":"When using TypeScript, prefer `import type` for type-only imports to ensure they are stripped from the compiled JavaScript, preventing runtime overhead and potential issues.","symbol":"EventEmitter (type)","correct":"import type { EventEmitter } from 'bare-events'"}],"quickstart":{"code":"import { EventEmitter } from 'bare-events';\n\ninterface MyEvents {\n  'data': (payload: string) => void;\n  'ready': () => void;\n  'error': (err: Error) => void;\n}\n\n// Create a new EventEmitter instance, typed for better safety\nconst myEmitter = new EventEmitter<MyEvents>();\n\n// Subscribe to the 'data' event\nmyEmitter.on('data', (payload: string) => {\n  console.log(`Received data: ${payload}`);\n});\n\n// Subscribe to the 'ready' event, only once\nmyEmitter.once('ready', () => {\n  console.log('Emitter is ready!');\n});\n\n// Emit events\nmyEmitter.emit('data', 'Hello, world!');\nmyEmitter.emit('ready');\nmyEmitter.emit('data', 'Another piece of data!');\n\n// Demonstrate removing a listener\nconst errorHandler = (err: Error) => console.error('Caught error:', err.message);\nmyEmitter.on('error', errorHandler);\nmyEmitter.emit('error', new Error('Something went wrong!'));\nmyEmitter.off('error', errorHandler); // Remove the specific listener\nmyEmitter.emit('error', new Error('This error will crash the process if no other listener is present.')); // No listener, will crash in Node.js\n","lang":"typescript","description":"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`."},"warnings":[{"fix":"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.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"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));`","message":"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.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"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)`).","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"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.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"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).","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.","error":"TypeError: EventEmitter is not a constructor"},{"fix":"Always register an error handler for any `EventEmitter` that might emit errors: `myEmitter.on('error', (err) => { console.error('Caught an error:', err); });`","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.","error":"Unhandled 'error' event. ([Error object details])"},{"fix":"Switch to ES Module `import` syntax: `import { EventEmitter } from 'bare-events';`","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`).","error":"ReferenceError: require is not defined in ES module scope"}],"ecosystem":"npm"}