oxc-project/oxc · error · OxcDiagnostic

Do not use "{name}".

Error message

Do not use "{name}".

What it means

Diagnostic from the unicorn/prefer-module lint rule. It fires when code references a CommonJS global — `exports`, `require`, `module`, `__filename`, or `__dirname` (the name appears in the message) — inside a file that is an ES module, where those identifiers do not exist and would be runtime `ReferenceError`s. The flagged input is the identifier reference to the CommonJS global.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/prefer_module.rs:25

    AstNode,
    context::{ContextHost, LintContext},
    rule::Rule,
};

const COMMON_JS_GLOBALS: [&str; 5] = ["exports", "require", "module", "__filename", "__dirname"];

fn use_strict_directive_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn(r#"Do not use "use strict" directive."#)
        .with_help("ES modules are always strict mode, so this directive is redundant.")
        .with_label(span)
}

fn global_return_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn(r#""return" should be used inside a function."#).with_label(span)
}

fn common_js_global_diagnostic(span: Span, name: &str) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!(r#"Do not use "{name}"."#))
        .with_help("Prefer ES modules over CommonJS globals.")
        .with_label(span)
}

#[derive(Debug, Default, Clone)]
pub struct PreferModule;

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Prefer JavaScript modules (ESM) over CommonJS.
    ///
    /// ### Why is this bad?
    ///
    /// CommonJS globals and patterns (`require`, `module`, `exports`, `__filename`, `__dirname`)
    /// make code harder to migrate and can block ESM-only features.
    ///
    /// ### Examples

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Replace `require()` with a static `import` or dynamic `import()`
  2. Replace `exports`/`module.exports` with `export` statements
  3. Replace `__dirname`/`__filename` with `import.meta.dirname`/`import.meta.filename`
  4. Apply the rule's auto-fix where available
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/prefer_module.rs:25 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/fd9e67b35e42eee0. Report an issue: GitHub.