import/max-dependencies

Enforce a maximum number of modules that a file can import

Also called import-x/max-dependencies

A file that imports from many modules usually does too many things, and a change in any of those modules can break it. This rule reports a file that depends on more than max modules, 10 by default.

Each module counts once, no matter how many statements import from it. import, export ... from, import() and require() all count.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"import/max-dependencies" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"import/max-dependencies" = ["error", {
  max = 10,
  ignoreTypeImports = false,
}]

Examples#

example.jswith { "max": 2 }1 problem
import { readFile } from "node:fs/promises";
export { parse } from "./parser.js";

const { render } = await import("./render.js");
example.jswith { "max": 2 }No problems
import { readFile } from "node:fs/promises";
import { writeFile } from "node:fs/promises";
import { join, resolve } from "node:path";

Options#

OptionDefaultWhat it does
max10The number of modules a file can depend on.
ignoreTypeImportsfalseDon't count import type statements.

If you pass options, always include max. The default of 10 only applies when there are no options at all. With { "ignoreTypeImports": true } alone, nothing is reported.

example.tswith { "max": 1 }1 problem
import { parse } from "./parser";
import type { Token } from "./tokens";
example.tswith { "max": 1, "ignoreTypeImports": true }No problems
import { parse } from "./parser";
import type { Token } from "./tokens";