src/content/docs/linter/rules/no-common-js.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v1.9.0` - Diagnostic Category: [`lint/style/noCommonJs`](/reference/diagnostics#diagnostic-category) - This rule isn't recommended, so you need to enable it. - This rule doesn't have a fix. - The default severity of this rule is [**warning**](/reference/diagnostics#warning). - Sources: - Same as [`@typescript-eslint/no-require-imports`](https://typescript-eslint.io/rules/no-require-imports) - Same as [`@typescript-eslint/no-var-requires`](https://typescript-eslint.io/rules/no-var-requires) - Same as [`import/no-commonjs`](https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-commonjs.md){
"linter": {
"rules": {
"style": {
"noCommonJs": "error"
}
}
}
}
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');
module.exports = { a: 'b' }
exports.a = 'b';
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.