oxc-project/oxc · warning · OxcDiagnostic
Expected "import" statement instead of "require" call
Error message
Expected "import" statement instead of "require" call
What it means
Oxlint's no-require-imports flags CommonJS require() calls and expects ESM import statements (warn at no_require_imports.rs:24-27). The config's allow list compiles strings into u-flag regexes matched against the imported path; the doc comment at no_require_imports.rs:31-33 notes package.json as the classic exception, since it often lives outside the TS root.
Source
Thrown at crates/oxc_linter/src/rules/typescript/no_require_imports.rs:23
ast::{Argument, TSModuleReference},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::IsGlobalReference;
use oxc_span::Span;
use oxc_str::static_ident;
use schemars::JsonSchema;
use serde::Deserialize;
use crate::{
AstNode,
context::LintContext,
rule::{DefaultRuleConfig, Rule},
utils::deserialize_regex_vec,
};
fn no_require_imports_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Expected \"import\" statement instead of \"require\" call")
.with_help("Do not use CommonJS `require` calls")
.with_label(span)
}
#[derive(Debug, Default, Clone, Deserialize)]
pub struct NoRequireImports(Box<NoRequireImportsConfig>);
#[derive(Debug, Default, Clone, JsonSchema, Deserialize)]
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
pub struct NoRequireImportsConfig {
/// These strings will be compiled into regular expressions with the u flag and be used to test against the imported path.
/// A common use case is to allow importing `package.json`. This is because `package.json` commonly lives outside of the TS root directory,
/// so statically importing it would lead to root directory conflicts, especially with `resolveJsonModule` enabled.
/// You can also use it to allow importing any JSON if your environment doesn't support JSON modules, or use it for other cases where `import` statements cannot work.
///
/// With `{ allow: ['/package\\.json$'] }`:
///
/// Examples of **correct** code for this rule:View on GitHub (pinned to e1e7af627c)
Solutions
- Convert to `import fs from 'node:fs'` (and JSON imports to ESM form)
- Add the path to the rule's allow list, e.g. `\\.json$` or `\\/package.json$`
- Inline disable for tooling-only files that must stay CJS
Example fix
// before
const fs = require('node:fs');
// after
import fs from 'node:fs'; Defensive patterns
Strategy: validation
Validate before calling
{
"rules": {
"typescript/no-require-imports": [
"error",
{ "allow": ["\\/package\\.json$", "\\.json$"] }
]
}
}
# CI gate
npx oxlint --deny-warnings . Prevention
- Set "type": "module" in package.json early so tooling rejects stray require calls
- Keep JSON loads in dedicated, allow-listed files instead of scattering requires
- Convert with a codemod (ts-node cjs to esm tools) rather than by hand
- Exclude or disable the rule in config-generation scripts that must stay CJS
When it happens
Trigger: `const fs = require('node:fs')`, `const pkg = require('./package.json')` when no allow pattern matches, `require('./data.json')` in linted sources.
Common situations: CommonJS-to-ESM migrations, JSON and config loading, scripts converted from Node CJS, dual-mode packages where lint runs over ESM sources.
Related errors
- Expected {name} instead of {actual}
- Type imports from declaration files must use top-level `impo
- Prefer using inline type specifiers instead of a top-level t
- Prefer using a top-level type-only import instead of inline
- Prefer using a top-level type-only import instead of inline
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/b50f85f0a6973950.
Report an issue: GitHub.