{"record":{"id":"4983d39b15637327","repo":"oxc-project/oxc","slug":"expected-name-instead-of-actual","errorCode":null,"errorMessage":"Expected {name} instead of {actual}","messagePattern":"Expected (.+?) instead of (.+?)","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/import/no_commonjs.rs","lineNumber":19,"sourceCode":"use oxc_ast::{\n    AstKind,\n    ast::{Argument, Expression},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, 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};\n\nfn no_commonjs_diagnostic(span: Span, name: &str, actual: &str) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\"Expected {name} instead of {actual}\"))\n        .with_help(\"Do not use CommonJS `require` calls and `module.exports` or `exports.*`\")\n        .with_label(span)\n}\n\n#[derive(Debug, Clone, JsonSchema, Deserialize)]\n#[serde(rename_all = \"camelCase\", default, deny_unknown_fields)]\npub struct NoCommonjs {\n    /// If `allowPrimitiveModules` option is set to true, the following is valid:\n    ///\n    /// ```js\n    /// module.exports = \"foo\";\n    /// module.exports = function rule(context) {\n    ///   return { /* ... */ };\n    /// };\n    /// ```\n    ///\n    /// but this is still reported:\n    ///","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/import/no_commonjs.rs#L1-L37","documentation":"Diagnostic from the oxlint rule import/no-commonjs (restriction category). It enforces ESM syntax and reports CommonJS usage: `require()` calls produce \"Expected import instead of require\" and `module.exports`/`exports.*` usage produces \"Expected export instead of exports\". Options: `allowRequire` (default false), `allowPrimitiveModules` (default false), and `allowConditionalRequire` (default true — require inside if/try/logical/ternary is allowed).","triggerScenarios":"Top-level `var mod = require(\"fs\")`; `module.exports = {...}` or `module.exports = \"Hola\"` or `exports.sayHello = function(){}` (when the member expression object is exactly the identifier `module` with property `exports`). Conditional requires inside `if`/`try`/`&&`/ternary pass by default. Emitted from no_commonjs_diagnostic at crates/oxc_linter/src/rules/import/no_commonjs.rs:19.","commonSituations":"Migrating a CommonJS Node project to ESM (\"type\": \"module\") and cleaning up leftovers; mixed codebases where config files (jest.config.js, .eslintrc.js, gulpfile) are still CJS; linting scripts that reuse browser-incompatible CJS patterns.","solutions":["Convert the file to ESM: `require(x)` → `import ... from \"x\"`, `module.exports = v` → `export default v`, `exports.k = v` → `export const k = v`","Scope the rule off for genuinely-CJS files via config overrides (e.g. *.cjs, *.config.js) instead of editing the code","If requires must stay, relax with options: `{ \"allowRequire\": true, \"allowPrimitiveModules\": true }`","Keep intentional lazy loads inside if/try blocks, which `allowConditionalRequire` (default true) already permits"],"exampleFix":"// before\nvar mod = require(\"./mod\");\nmodule.exports = { hello: function () { return \"Hello\"; } };\n\n// after\nimport mod from \"./mod\";\nexport function hello() { return \"Hello\"; }","handlingStrategy":"validation","validationCode":"// fail fast in CI before lint: refuse new CJS in ESM sources\n// package.json script: \"check:cjs\": \"node -e \\\"const g=require('child_process').execSync;const r=g('rg -l \\\\\"module.exports|require\\\\\" src --type js -g \\\\\"!*.cjs\\\\\" || true').toString().trim();if(r){console.error('CJS found:\\\\n'+r);process.exit(1)}\\\"\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set \"type\": \"module\" in package.json early and convert entry points first","Keep genuinely-CJS files (*.cjs, jest.config.js) out of the rule via config overrides","Use `import type`/ESM in new code and put lazy loads inside if/try so allowConditionalRequire covers them"],"tags":["lint","oxlint","imports","commonjs","esm","module-system","migration"],"backgroundTag":"commonjs-to-esm-migration","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}