Skip to content

noNewRequire (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noNewRequire": "error"
}
}
}
}

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.

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();
  • If you want to disallow require entirely, see noCommonJs.