no-duplicate-imports

Disallow importing from the same module more than once in a file

Two import statements for the same module split one list of names over two places. You have to find both to know what the file takes from that module, and the next person who adds a name has to pick one.

Only statements that can be merged are reported. import * as path and import { join } can't be written as one statement, so they're allowed side by side.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"no-duplicate-imports" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"no-duplicate-imports" = ["error", {
  includeExports = false,
  allowSeparateTypeImports = false,
}]

Examples#

example.js1 problem
import { readFile } from "node:fs/promises";
import { join } from "node:path";
import { writeFile } from "node:fs/promises";
example.jsNo problems
import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
example.jsNo problems
import * as path from "node:path";
import { join } from "node:path";

Options#

OptionDefaultWhat it does
includeExportsfalseAlso look at export ... from statements.
allowSeparateTypeImportsfalseAllow an import type statement next to a normal import from the same module.

With includeExports, an export ... from is reported if the file already imports or exports from that module:

example.jswith { "includeExports": true }1 problem
import { parse } from "./parser.js";
export { tokenize } from "./parser.js";

export * from can't be merged with a named import, so it's still allowed:

example.jswith { "includeExports": true }No problems
import { parse } from "./parser.js";
export * from "./parser.js";

Without allowSeparateTypeImports, types and values from one module belong in one statement:

example.ts1 problem
import type { User } from "./user";
import { loadUser } from "./user";
example.tsNo problems
import { loadUser, type User } from "./user";

With it, the two statements are allowed:

example.tswith { "allowSeparateTypeImports": true }No problems
import type { User } from "./user";
import { loadUser } from "./user";