oxc-project/oxc · warning · OxcDiagnostic
Do not mix 'require' and other declarations.
Error message
Do not mix 'require' and other declarations.
What it means
Diagnostic from oxlint rule node/no-mixed-requires (style). Within a single var/let/const declaration statement, require-initialized declarators must not be mixed with plain or uninitialized ones. Node community convention keeps module-loading declarations visually separate from other initialization — the CommonJS analogue of ES module imports living apart from regular statements. This base diagnostic is emitted regardless of the rule's options; grouping/allowCall only affect the second diagnostic.
Source
Thrown at crates/oxc_linter/src/rules/node/no_mixed_requires.rs:19
use schemars::JsonSchema;
use serde::Deserialize;
use oxc_ast::{
AstKind,
ast::{Expression, VariableDeclarator},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use crate::{
AstNode,
context::LintContext,
rule::{DefaultRuleConfig, Rule},
};
fn no_mix_require_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Do not mix 'require' and other declarations.").with_label(span)
}
fn no_mix_core_module_file_computed_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Do not mix core, module, file and computed requires.").with_label(span)
}
#[derive(Debug, Default, Clone, Copy, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields, default)]
struct NoMixedRequiresOptions {
grouping: bool,
allow_call: bool,
}
#[derive(Debug, Clone, Deserialize, JsonSchema)]
#[serde(untagged)]
enum NoMixedRequiresConfig {
Grouping(bool),
Options(NoMixedRequiresOptions),View on GitHub (pinned to e1e7af627c)
Solutions
- Split the statement: requires in their own declaration, other variables in another
- Adopt one-declarator-per-line const/let style so mixing becomes structurally impossible
- Note no option disables this specific check — only code restructuring (or turning the rule off) silences it
Example fix
// before
var fs = require('fs'),
i = 0;
// after
var fs = require('fs');
var i = 0; Defensive patterns
Strategy: validation
Validate before calling
// .oxlintrc.json
"rules": { "node/no-mixed-requires": "error" }
npx oxlint -c .oxlintrc.json --deny-warning . Prevention
- One declarator per statement (const a = require('a');) makes mixing structurally impossible
- Keep a dedicated requires block at the top of the module, separate from logic declarations
- Fix appends to legacy comma-chained var statements by splitting, not extending
When it happens
Trigger: One VariableDeclaration whose declarators include at least one whose init is a require() call (callee is the identifier 'require'; member access like require('events').EventEmitter still counts, and with allowCall, call-chains like require('diagnostics')('x') too) AND at least one declarator that is uninitialized (var foo;) or initialized with a non-require expression. Trigger example: var fs = require('fs'), i = 0;
Common situations: ES5-era comma-chained var statements; refactors that appended a variable to an existing require line; enabling the node preset in shared configs surfaces these in old code immediately.
Related errors
- Unexpected require().
- Do not mix core, module, file and computed requires.
- Split requires to be separated into a single block.
- Unexpected access to `exports`.
- Unexpected access to `module.exports`.
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/cb082c892202f165.
Report an issue: GitHub.