{"record":{"id":"b50f85f0a6973950","repo":"oxc-project/oxc","slug":"expected-import-statement-instead-of-require-c","errorCode":null,"errorMessage":"Expected \"import\" statement instead of \"require\" call","messagePattern":"Expected \"import\" statement instead of \"require\" call","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/typescript/no_require_imports.rs","lineNumber":23,"sourceCode":"    ast::{Argument, TSModuleReference},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_semantic::IsGlobalReference;\nuse oxc_span::Span;\nuse oxc_str::static_ident;\nuse schemars::JsonSchema;\nuse serde::Deserialize;\n\nuse crate::{\n    AstNode,\n    context::LintContext,\n    rule::{DefaultRuleConfig, Rule},\n    utils::deserialize_regex_vec,\n};\n\nfn no_require_imports_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Expected \\\"import\\\" statement instead of \\\"require\\\" call\")\n        .with_help(\"Do not use CommonJS `require` calls\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone, Deserialize)]\npub struct NoRequireImports(Box<NoRequireImportsConfig>);\n\n#[derive(Debug, Default, Clone, JsonSchema, Deserialize)]\n#[serde(rename_all = \"camelCase\", default, deny_unknown_fields)]\npub struct NoRequireImportsConfig {\n    /// These strings will be compiled into regular expressions with the u flag and be used to test against the imported path.\n    /// A common use case is to allow importing `package.json`. This is because `package.json` commonly lives outside of the TS root directory,\n    /// so statically importing it would lead to root directory conflicts, especially with `resolveJsonModule` enabled.\n    /// 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.\n    ///\n    /// With `{ allow: ['/package\\\\.json$'] }`:\n    ///\n    /// Examples of **correct** code for this rule:","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/typescript/no_require_imports.rs#L5-L41","documentation":"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.","triggerScenarios":"`const fs = require('node:fs')`, `const pkg = require('./package.json')` when no allow pattern matches, `require('./data.json')` in linted sources.","commonSituations":"CommonJS-to-ESM migrations, JSON and config loading, scripts converted from Node CJS, dual-mode packages where lint runs over ESM sources.","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"],"exampleFix":"// before\nconst fs = require('node:fs');\n\n// after\nimport fs from 'node:fs';","handlingStrategy":"validation","validationCode":"{\n  \"rules\": {\n    \"typescript/no-require-imports\": [\n      \"error\",\n      { \"allow\": [\"\\\\/package\\\\.json$\", \"\\\\.json$\"] }\n    ]\n  }\n}\n\n# CI gate\nnpx oxlint --deny-warnings .","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["typescript","lint","commonjs","imports","esm"],"backgroundTag":"no-require-imports","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}