noCommonJs
此内容尚不支持你的语言。
Diagnostic Category: lint/nursery/noCommonJs
Since: v1.9.0
Sources:
- Same as:
@typescript-eslint/no-require-imports
- Same as:
import/no-commonjs
Disallow use of CommonJs module system in favor of ESM style imports.
ESM-style import
s are modern alternative to CommonJS require
imports. Supported by all modern browsers and Node.js versions.
Tooling can more easily statically analyze and tree-shake ESM import
s compared to CommonJs.
Examples
Section titled ExamplesInvalid
Section titled Invalidcode-block.js:1:1 lint/nursery/noCommonJs ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use ESM imports instead of require.
> 1 │ require(‘node:fs
’);
│ ^^^^^^^^^^^^^^^^^^
2 │
ℹ ESM-style import statements are more easily statically analyzable and tree-shakable compared to CommonJs require.
code-block.js:1:1 lint/nursery/noCommonJs ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use ESM exports instead of module.exports.
> 1 │ module.exports = { a: ‘b’ }
│ ^^^^^^^^^^^^^^
2 │
ℹ ESM-style export statements are more easily statically analyzable and tree-shakable compared to CommonJs module.exports.
code-block.js:1:1 lint/nursery/noCommonJs ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use ESM exports instead of module.exports.
> 1 │ exports.a = ‘b’;
│ ^^^^^^^^^
2 │
ℹ ESM-style export statements are more easily statically analyzable and tree-shakable compared to CommonJs module.exports.
Valid
Section titled ValidCaveats
Section titled CaveatsRule is automatically disabled inside .cjs
and .cts
files, because they are explicitly CommonJs files.
This rule could be helpful if you are migrating from CommonJs to ESM, but if you wish to continue using CommonJs, you can safely disable it.