Skip to content

noDynamicRequire (JavaScript)

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

Disallow require() calls whose module name isn’t a fixed string.

require() accepts any value as the module name, including names built while the program runs, such as require("./locales/" + language). When the name is built this way, editors, bundlers, and other tools can’t tell which file gets loaded. They can’t check that the file exists, include it in a bundle, or find every place that uses it.

This rule reports calls to require() whose first argument is anything other than a plain value written in the code, such as a string or a template literal without ${} placeholders. Calls without arguments aren’t reported. Calls to a require function declared in your own code aren’t reported either.

require(name);
code-block.js:1:9 lint/nursery/noDynamicRequire ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ The module name passed to require() isn't a fixed string.

> 1 │ require(name);
│ ^^^^
2 │

ℹ When the module name is built while the program runs, tools can't tell which file gets loaded, so they can't check that it exists or find where it's used.

ℹ Write the module name as a string, such as "./module".

ℹ 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.

require("../" + name);
code-block.js:1:9 lint/nursery/noDynamicRequire ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ The module name passed to require() isn't a fixed string.

> 1 │ require("../" + name);
│ ^^^^^^^^^^^^
2 │

ℹ When the module name is built while the program runs, tools can't tell which file gets loaded, so they can't check that it exists or find where it's used.

ℹ Write the module name as a string, such as "./module".

ℹ 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.

require(`../${name}`);
code-block.js:1:9 lint/nursery/noDynamicRequire ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ The module name passed to require() isn't a fixed string.

> 1 │ require(`../${name}`);
│ ^^^^^^^^^^^^
2 │

ℹ When the module name is built while the program runs, tools can't tell which file gets loaded, so they can't check that it exists or find where it's used.

ℹ Write the module name as a string, such as "./module".

ℹ 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.

require(getName());
code-block.js:1:9 lint/nursery/noDynamicRequire ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ The module name passed to require() isn't a fixed string.

> 1 │ require(getName());
│ ^^^^^^^^^
2 │

ℹ When the module name is built while the program runs, tools can't tell which file gets loaded, so they can't check that it exists or find where it's used.

ℹ Write the module name as a string, such as "./module".

ℹ 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.

require("../name");
require(`../name`);

Default: false

When true, the rule also reports import() calls whose module name isn’t a fixed string.

biome.json
{
"linter": {
"rules": {
"nursery": {
"noDynamicRequire": {
"level": "on",
"options": {
"checkDynamicImports": true
}
}
}
}
}
}
import(`./locales/${language}.js`);
code-block.js:1:8 lint/nursery/noDynamicRequire ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ The module name passed to import() isn't a fixed string.

> 1 │ import(`./locales/${language}.js`);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

ℹ When the module name is built while the program runs, tools can't tell which file gets loaded, so they can't check that it exists or find where it's used.

ℹ Write the module name as a string, such as "./module".

ℹ 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.

import("./locales/en.js");
  • If you want to disallow require() entirely, see noCommonJs.