eventClasses
Prefer EventTarget over EventEmitter for cross-platform compatibility.
✅ This rule is included in the node logical preset.
While EventEmitter is only available in Node.js, EventTarget is available in browsers, Deno, and modern Node.js (v14.5+).
Using EventTarget makes code more cross-platform friendly and can reduce bundle size in built packages.
Examples
Section titled “Examples”import { EventEmitter } from "events";
class MyEmitter extends EventEmitter {}
const emitter = new EventEmitter();class MyTarget extends EventTarget {}
const target = new EventTarget();Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If your code exclusively targets Node.js environments and you specifically need EventEmitter’s API features that are not available in EventTarget (for example, prependListener, once returning a cleanup function, or error events with special handling), you might choose not to enable this rule.
Further Reading
Section titled “Further Reading”- MDN: EventTarget
- Node.js: EventTarget and Event API
- Node.js: Differences between EventEmitter and EventTarget
Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
unicorn/prefer-event-target - Oxlint:
unicorn/prefer-event-target
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.