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 problemimport { readFile } from "node:fs/promises";
export { parse } from "./parser.js";
const { render } = await import("./render.js"Maximum number of dependencies (2) exceeded.);example.jswith
{ "max": 2 }No problemsimport { readFile } from "node:fs/promises";
import { writeFile } from "node:fs/promises";
import { join, resolve } from "node:path";Options#
| Option | Default | What it does |
|---|---|---|
max | 10 | The number of modules a file can depend on. |
ignoreTypeImports | false | Don'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 problemimport { parse } from "./parser";
import type { Token } from "./tokens"Maximum number of dependencies (1) exceeded.;example.tswith
{ "max": 1, "ignoreTypeImports": true }No problemsimport { parse } from "./parser";
import type { Token } from "./tokens";