Skip to content
โ† Back to rules

import/max-dependencies Pedantic

What it does โ€‹

Forbid modules to have too many dependencies (import statements only).

Why is this bad? โ€‹

This is a useful rule because a module with too many dependencies is a code smell, and usually indicates the module is doing too much and/or should be broken up into smaller modules.

NOTE: This rule only counts import statements, and does not count dependencies from CommonJS require() statements. This is a difference from the original eslint-import-plugin rule.

Examples โ€‹

Given { "max": 2 }

Examples of incorrect code for this rule:

javascript
import a from "./a";
import b from "./b";
import c from "./c"; // Too many dependencies: 3 (max: 2)

Examples of correct code for this rule:

javascript
import a from "./a";
import b from "./b"; // Allowed: 2 dependencies (max: 2)

Configuration โ€‹

This rule accepts a configuration object with the following properties:

ignoreTypeImports โ€‹

type: boolean

default: false

Whether to ignore type imports when counting dependencies.

ts
// Neither of these count as dependencies if `ignoreTypeImports` is true:
import type { Foo } from "./foo";
import { type Foo } from "./foo";

max โ€‹

type: integer

default: 10

Maximum number of dependencies allowed in a file.

How to use โ€‹

To enable this rule using the config file or in the CLI, you can use:

json
{
  "plugins": ["import"],
  "rules": {
    "import/max-dependencies": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["import"],
  rules: {
    "import/max-dependencies": "error",
  },
});
bash
oxlint --deny import/max-dependencies --import-plugin

References โ€‹