# prefer-module

Rule `unicorn/prefer-module`. Prefer JavaScript modules (ESM) over CommonJS.

- Source: unicorn
- Technologies: JavaScript
- Kind: correctness
- Needs type information: no
- Autofix: yes, in at least one linter
- Reach: 12M weekly downloads of presets that enable it
- Trend: +40.1% last quarter
- Rank: #756 by reach, #952 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `unicorn/prefer-module` | no | safe | package `eslint-plugin-unicorn` | [docs](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v80.0.0/docs/rules/prefer-module.md) |
| Oxlint | `unicorn/prefer-module` | no | no | plugin `unicorn`, category restriction | [docs](https://oxc.rs/docs/guide/usage/linter/rules/unicorn/prefer-module.html) |
| Biome | `correctness/noGlobalDirnameFilename` | no | safe | group correctness, since 2.0.0 | [docs](https://biomejs.dev/linter/rules/no-global-dirname-filename/) |

## Code examples

### Incorrect: the rule reports this

```js
'use strict';

// …
```

```js
if (foo) {
  return;
}

// …
```

```js
const file = path.join(__dirname, 'foo.js');
```

### Correct: the rule accepts this

```js
// (Remove the 'use strict' directive. JavaScript modules are in strict mode by default)

// …
```

```js
function run() {
  if (foo) {
    return;
  }

  // …
}

run();
```

```js
const file = path.join(import.meta.dirname, 'foo.js');
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
import unicorn from "eslint-plugin-unicorn";

export default [
  {
    plugins: { unicorn },
    rules: {
      "unicorn/prefer-module": "error",
    },
  },
];
```

### Oxlint: `.oxlintrc.json`

```json
{
  "rules": {
    "unicorn/prefer-module": "error"
  }
}
```

### Biome: `biome.json`

```json
{
  "linter": {
    "rules": {
      "correctness": {
        "noGlobalDirnameFilename": "error"
      }
    }
  }
}
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [Unicorn recommended](https://lintradar.dev/presets/unicorn-recommended.md) | ESLint | `eslint-plugin-unicorn` | 340 | 9.5M | +30.3% |
| [Unicorn unopinionated](https://lintradar.dev/presets/unicorn-unopinionated.md) | ESLint | `eslint-plugin-unicorn` | 236 | 9.5M | +30.3% |
| [Ultracite for Biome](https://lintradar.dev/presets/ultracite-biome.md) | Biome | `ultracite` | 616 | 2.1M | +151.7% |
| [Ultracite for Oxlint](https://lintradar.dev/presets/ultracite-oxlint.md) | Oxlint | `ultracite` | 475 | 2.1M | +151.7% |
| [XO](https://lintradar.dev/presets/xo.md) | ESLint | `eslint-config-xo` | 618 | 413K | −5.0% |

## Related rules

- [unicorn/prefer-at](https://lintradar.dev/rules/unicorn/prefer-at.md): Prefer `.at()` method for index access and `String#charAt()`.
- [unicorn/prefer-regexp-test](https://lintradar.dev/rules/unicorn/prefer-regexp-test.md): Prefer `RegExp#test()` over `String#match()`, `String#search()`, and `RegExp#exec()`.
- [unicorn/prefer-array-some](https://lintradar.dev/rules/unicorn/prefer-array-some.md): Prefer `.some(…)` over `.filter(…).length` check and `.{find,findLast,findIndex,findLastIndex}(…)`.
- [unicorn/prefer-string-slice](https://lintradar.dev/rules/unicorn/prefer-string-slice.md): Prefer `String#slice()` over `String#substr()` and `String#substring()`.
- [unicorn/numeric-separators-style](https://lintradar.dev/rules/unicorn/numeric-separators-style.md): Enforce the style of numeric separators by correctly grouping digits.
- [unicorn/prefer-string-trim-start-end](https://lintradar.dev/rules/unicorn/prefer-string-trim-start-end.md): Prefer `String#trimStart()` / `String#trimEnd()` over `String#trimLeft()` / `String#trimRight()`.

---

Web page: https://lintradar.dev/rules/unicorn/prefer-module. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
