Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/sindresorhus/eslint-plugin-unicorn/llms.txt

Use this file to discover all available pages before exploring further.

Prefer the native JavaScript module (ESM) format over the legacy CommonJS format. Enforcing ESM across a project helps the ecosystem converge on a single, interoperable module system.
Recommendedrecommended · ☑️ unopinionated
Fixable🔧 Auto-fixable
Suggestions💡 Has suggestions
.cjs files are completely ignored by this rule.
This rule flags five CommonJS-specific patterns:
  1. 'use strict' directive — ESM is always in strict mode; the directive is redundant.
  2. Top-level return — a CommonJS-only feature with no ESM equivalent.
  3. __dirname and __filename — not available in ESM. Use import.meta.dirname / import.meta.filename (Node.js ≥ 20.11) or derive them from import.meta.url.
  4. require(…) — replaced by import or import(…).
  5. exports and module.exports — replaced by export declarations.

Examples

// Strict mode directive
'use strict';

// Top-level return (CommonJS only)
if (foo) {
  return;
}

// CommonJS globals
const file = path.join(__dirname, 'foo.js');
const content = fs.readFileSync(__filename, 'utf8');

// require() imports
const {fromPairs} = require('lodash');

// CommonJS exports
module.exports = foo;
exports.foo = foo;
For Node.js versions before 20.11, derive __dirname and __filename from import.meta.url:
import {fileURLToPath} from 'node:url';
import path from 'node:path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(fileURLToPath(import.meta.url));
In many cases you can pass a URL directly to Node.js APIs without constructing a path at all:
const foo = new URL('foo.js', import.meta.url);

Build docs developers (and LLMs) love