{"record":{"id":"be46a0223ea07903","repo":"oxc-project/oxc","slug":"unexpected-require","errorCode":null,"errorMessage":"Unexpected require().","messagePattern":"Unexpected require\\(\\)\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/node/global_require.rs","lineNumber":12,"sourceCode":"use oxc_ast::{\n    AstKind,\n    ast::{Expression, IdentifierReference},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::Span;\n\nuse crate::{AstNode, context::LintContext, rule::Rule};\n\nfn global_require_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Unexpected require().\")\n        .with_label(span)\n        .with_help(\"Move require() to top-level module scope\")\n}\n\n#[derive(Debug, Default, Clone)]\npub struct GlobalRequire;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Require `require()` calls to be placed at top-level module scope.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// In Node.js, module dependencies are included using the `require()` function, such as:\n    /// ```js\n    /// var fs = require(\"fs\");\n    /// ```","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/node/global_require.rs#L1-L30","documentation":"Diagnostic from oxlint rule node/global-require (style). It requires every require() call to sit at top-level module scope. Nested requires execute lazily and possibly conditionally, hide the module's true dependency graph from tooling and readers, and push load cost into hot paths; keeping them at the top mirrors how ES import declarations are hoisted and statically visible.","triggerScenarios":"A call to the bare identifier require() that is not at module top level — inside a function body, an if/try block, a loop, or a listener callback. Example trigger: function loadConfig() { const fs = require('fs'); return fs.readFileSync(p); }.","commonSituations":"Lazy-loading to speed up CLI startup; workarounds for circular dependencies; try/catch requires for optional dependencies; test helpers requiring fixtures per invocation; teams inheriting a strict shared oxlint config that promotes the rule from warn to error.","solutions":["Hoist the require() to the top of the module next to the other requires","For optional dependencies, require at top level and feature-detect on the result, or isolate the lazy load in a tiny wrapper module that callers require once","Break the circular dependency by moving shared code into a third module","If the lazy load is deliberate and profiled, silence it locally with // oxlint-disable-next-line node/global-require or scope the rule off for that directory"],"exampleFix":"// before\nfunction getFs() {\n  const fs = require('fs');\n  return fs;\n}\n\n// after\nconst fs = require('fs');\n\nfunction getFs() {\n  return fs;\n}","handlingStrategy":"validation","validationCode":"// .oxlintrc.json\n\"rules\": { \"node/global-require\": \"error\" }\n\nnpx oxlint -c .oxlintrc.json --deny-warning .","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep all require() calls in the module's require block at the top, mirroring how imports are hoisted in ESM","When fighting circular dependencies, restructure modules instead of hiding requires inside functions","For optional deps, require once at top level and branch on the result rather than requiring inside try/catch"],"tags":["node","commonjs","require","style","oxlint"],"backgroundTag":"require-inside-function","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"}