oxc-project/oxc · error · OxcDiagnostic
Prefer `EventTarget` over `EventEmitter`.
Error message
Prefer `EventTarget` over `EventEmitter`.
What it means
Diagnostic from the unicorn/prefer-event-target lint rule. It fires when `EventEmitter` (from `events` or a non-ignored package) is instantiated or used as a base class in code that is not Node-specific; the DOM-standard `EventTarget` is cross-environment and preferred. Packages in the rule's ignore list (e.g. `@angular/core`, `eventemitter3`) are exempt. It is a lint suggestion, not a runtime error.
Source
Thrown at crates/oxc_linter/src/rules/unicorn/prefer_event_target.rs:17
use oxc_ast::{
AstKind,
ast::{Argument, BindingPattern, Expression, IdentifierReference},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use crate::{
AstNode, ast_util::get_declaration_of_variable, context::LintContext, rule::Rule,
utils::is_import_from_module,
};
const IGNORED_PACKAGES: [&str; 2] = ["@angular/core", "eventemitter3"];
fn prefer_event_target_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Prefer `EventTarget` over `EventEmitter`.")
.with_help("Change `EventEmitter` to `EventTarget`. EventEmitters are only available in Node.js, while EventTargets are also available in browsers.")
.with_note("https://developer.mozilla.org/en-US/docs/Web/API/EventTarget")
.with_label(span)
}
#[derive(Debug, Default, Clone)]
pub struct PreferEventTarget;
declare_oxc_lint!(
/// ### What it does
///
/// Prefers `EventTarget` over `EventEmitter`.
///
/// This rule reduces the bundle size and makes your code more cross-platform friendly.
///
/// See the [differences](https://nodejs.org/api/events.html#eventtarget-and-event-api) between `EventEmitter` and `EventTarget`.
///
/// ### Why is this bad?View on GitHub (pinned to e1e7af627c)
Solutions
- Change `class X extends EventEmitter` to `extends EventTarget`
- Switch `new EventEmitter()` to `new EventTarget()` and use `addEventListener`/`dispatchEvent` instead of `on`/`emit`
- Remove the now-unused `events` import
- Add a package to the rule's ignore list if `EventEmitter` is genuinely required
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/prefer_event_target.rs:17 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/24450369aff63e9a.
Report an issue: GitHub.