noNewRequire (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Rule available since:
v2.6.3 - Diagnostic Category:
lint/nursery/noNewRequire - This rule doesn’t have a fix.
- The default severity of this rule is warning.
- Sources:
- Same as
no-new-require - Same as
n/no-new-require
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noNewRequire": "error" } } }}Description
Section titled “Description”Disallow calling require with the new operator.
Code like new require("app-header") looks like it loads a module and creates a new
object from what the module exports, all in one step. It doesn’t. new applies to
require itself, not to the value that require returns. The result is whatever the
module exports, such as a class, instead of a new object created from that class.
Store the result of require in a variable first, then use new on that variable.
The rule doesn’t report require when it’s a variable, parameter, or function declared
in your own code.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”const appHeader = new require("app-header");code-block.js:1:19 lint/nursery/noNewRequire ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Unexpected use of new with require.
> 1 │ const appHeader = new require("app-header");
│ ^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Here new applies to require itself, so the result is whatever the module exports, not a new object created from it.
ℹ Store the result of require in a variable first, then use new on that variable.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
const AppHeader = require("app-header");const appHeader = new AppHeader();See Also
Section titled “See Also”- If you want to disallow
requireentirely, seenoCommonJs.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.