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.