noDynamicRequire (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.8 - Diagnostic Category:
lint/nursery/noDynamicRequire - This rule doesn’t have a fix.
- The default severity of this rule is warning.
- Sources:
- Same as
import/no-dynamic-require
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noDynamicRequire": "error" } } }}Description
Section titled “Description”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.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”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`);Options
Section titled “Options”checkDynamicImports
Section titled “checkDynamicImports”Default: false
When true, the rule also reports import() calls whose module name isn’t a fixed string.
{ "linter": { "rules": { "nursery": { "noDynamicRequire": { "level": "on", "options": { "checkDynamicImports": true } } } } }}Invalid
Section titled “Invalid”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");See Also
Section titled “See Also”- If you want to disallow
require()entirely, seenoCommonJs.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.