{"record":{"id":"7296d6ab51f223b1","repo":"oxc-project/oxc","slug":"unexpected-use-of-new-operator-with-require","errorCode":null,"errorMessage":"Unexpected use of `new` operator with `require`","messagePattern":"Unexpected use of `new` operator with `require`","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/node/no_new_require.rs","lineNumber":9,"sourceCode":"use oxc_ast::AstKind;\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::Span;\n\nuse crate::{AstNode, context::LintContext, rule::Rule};\n\nfn no_new_require(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Unexpected use of `new` operator with `require`\")\n        .with_help(\"Separate `require()` from `new` operator\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct NoNewRequire;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Warn about calling `new` on `require`.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// The `require` function is used to include modules and might return a constructor. As this\n    /// is not always the case this can be confusing.\n    ///\n    /// ### Examples","sourceCodeStart":1,"sourceCodeEnd":27,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/node/no_new_require.rs#L1-L27","documentation":"Diagnostic from oxlint rule node/no-new-require (restriction category). It flags `new require(...)`: using the require function as a constructor reads as a mistake and behaves surprisingly — the construct invocation runs require with a fresh `this`, and only 'works' when module.exports is an object (constructor-return override); when the module exports a primitive you silently get an unrelated empty object instead of the export.","triggerScenarios":"A NewExpression whose callee is the bare identifier require — e.g. const store = new require('./store');. Note the frequently-seen form new (require('events')).EventEmitter() does NOT fire because the callee there is a parenthesized member expression, not the require identifier itself.","commonSituations":"Copy-paste from old snippets that wrote new require('events').EventEmitter (missing parens); beginners assuming require is a class constructor; intent was usually 'require the exported class, then construct it'.","solutions":["Require first, construct second: 'const Store = require(\"./store\"); const s = new Store();'","For namespaced exports: 'const EventEmitter = require(\"events\").EventEmitter; const e = new EventEmitter();'","If the pattern is truly intentional and vetted, disable inline: // oxlint-disable-next-line node/no-new-require"],"exampleFix":"// before\nconst EventEmitter = new require('events').EventEmitter;\n\n// after\nconst EventEmitter = require('events').EventEmitter;","handlingStrategy":"validation","validationCode":"// .oxlintrc.json\n\"rules\": { \"node/no-new-require\": \"error\" }\n\nnpx oxlint -c .oxlintrc.json --deny-warning .","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Separate 'require the export' from 'construct the export' onto distinct statements","When you see `new` immediately before `require` in review, stop — it is either a typo or a code smell","Prefer destructuring at the require site (const { EventEmitter } = require('events')) to make intent explicit"],"tags":["node","commonjs","require","constructor","oxlint"],"backgroundTag":"new-require-misuse","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"}