EventEmitter
wolfy87-eventemitter is a JavaScript event emitter library, primarily designed for use in web browsers but also usable in Node.js environments. It aims to replicate the event-driven paradigm found in Node.js, focusing on a lightweight footprint and fast execution. The library underwent a significant API remapping in its fourth major rewrite to improve clarity and extensibility. While npm shows `5.2.9` as the latest version, the project's GitHub repository has had no activity since 2016, with `v4.2.11` being the last officially tagged release. This indicates the project is no longer actively maintained, making `v4.x` the last stable and documented version.
Common errors
-
TypeError: EventEmitter is not a constructor
cause Attempting to instantiate `EventEmitter` without correctly importing or accessing it, often due to CommonJS/ESM module mismatches or incorrect global access in the browser.fixEnsure you are using the correct import/require syntax for your environment. For modern ESM environments: `import EventEmitter from 'wolfy87-eventemitter';`. For CommonJS: `const EventEmitter = require('wolfy87-eventemitter');`. In browsers (script tag), it might be globally available as `window.EventEmitter`. -
TypeError: Cannot read properties of undefined (reading 'on')
cause Attempting to call methods like `on` or `emit` on an `EventEmitter` instance that is undefined or null, usually due to incorrect initialization or scope issues.fixVerify that your `EventEmitter` instance (`emitter` in examples) is properly initialized with `new EventEmitter()` before attempting to use its methods. -
Uncaught TypeError: Android 2.2 RegExp type check issue
cause A bug in older Android 2.2 browsers related to `RegExp` type checking prevented proper functionality.fixUpgrade to `v4.2.7` or later, which includes a fix for this specific Android 2.2 browser bug. Note that this is for a very old browser version.
Warnings
- breaking The project is abandoned. While `npm` lists `5.2.9`, the GitHub repository has seen no activity since 2016, and `v4.2.11` is the last official release tag. No documentation or migration guides exist for v5.
- breaking Starting with `v4.2.11`, the project license changed from MIT to The Unlicense. This is a significant shift in licensing terms, moving to public domain dedication.
- gotcha The `EventEmitter` API underwent a significant remapping and redesign in its fourth major rewrite (v4.x). If upgrading from very old versions (pre-4.x), many method names and behaviors will have changed.
- gotcha The `removeAllListeners` method was added as an alias to `removeEvent` in `v4.2.4` to align with the Node.js API. While `removeEvent` still functions, `removeAllListeners` is the more consistent and recommended method name.
- gotcha An infinite recursion bug was present in `addOnceListener` in versions prior to `v4.2.3` under specific conditions.
Install
-
npm install wolfy87-eventemitter -
yarn add wolfy87-eventemitter -
pnpm add wolfy87-eventemitter
Imports
- EventEmitter
const EventEmitter = require('wolfy87-eventemitter');import EventEmitter from 'wolfy87-eventemitter';
- EventEmitter
import EventEmitter from 'wolfy87-eventemitter';
const EventEmitter = require('wolfy87-eventemitter'); - typeof EventEmitter
import type EventEmitter from 'wolfy87-eventemitter';
Quickstart
import EventEmitter from 'wolfy87-eventemitter';
const emitter = new EventEmitter();
// Register a listener for the 'data' event
emitter.on('data', (payload: string) => {
console.log('Received data:', payload);
});
// Register a 'once' listener for the 'init' event
emitter.once('init', () => {
console.log('Application initialized.');
});
// Emit the 'init' event
emitter.emit('init');
// This will not log again as 'once' listeners are removed after first emission
emitter.emit('init');
// Emit the 'data' event with a payload
emitter.emit('data', 'Hello Event World!');
// Remove a specific listener
const anotherListener = (num: number) => console.log('Number received:', num);
emitter.on('number', anotherListener);
emitter.emit('number', 42);
emitter.off('number', anotherListener);
emitter.emit('number', 100); // This will not log
// Remove all listeners for a specific event
emitter.on('cleanup', () => console.log('Cleaning up 1'));
emitter.on('cleanup', () => console.log('Cleaning up 2'));
emitter.emit('cleanup');
emitter.removeAllListeners('cleanup');
emitter.emit('cleanup'); // No output