跳转到内容

noCommonJs

Diagnostic Category: lint/nursery/noCommonJs

Since: v1.9.0

Sources:

Disallow use of CommonJs module system in favor of ESM style imports.

ESM-style imports 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 imports compared to CommonJs.

require('node:fs');
code-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.

module.exports = { a: 'b' }
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.

exports.a = 'b';
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.

import fs from 'node:fs';
import('node:fs')
export const a = 'b';
export default { a: 'b' };

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