Bare Events

2.8.2 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

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`.

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

view raw JSON →