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:
[lint.rules]
"no-duplicate-imports" = "error" # or "warn"Every option, set to its default:
[lint.rules]
"no-duplicate-imports" = ["error", {
includeExports = false,
allowSeparateTypeImports = false,
}]Examples#
import { readFile } from "node:fs/promises";
import { join } from "node:path";
import { writeFile } from "node:fs/promises";node:fs/promises import is duplicated.import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";import * as path from "node:path";
import { join } from "node:path";Options#
| Option | Default | What it does |
|---|---|---|
includeExports | false | Also look at export ... from statements. |
allowSeparateTypeImports | false | Allow 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:
{ "includeExports": true }1 problemimport { parse } from "./parser.js";
export { tokenize } from "./parser.js";./parser.js export is duplicated as import.export * from can't be merged with a named import, so it's still allowed:
{ "includeExports": true }No problemsimport { parse } from "./parser.js";
export * from "./parser.js";Without allowSeparateTypeImports, types and values from one module belong in one statement:
import type { User } from "./user";
import { loadUser } from "./user";./user import is duplicated.import { loadUser, type User } from "./user";With it, the two statements are allowed:
{ "allowSeparateTypeImports": true }No problemsimport type { User } from "./user";
import { loadUser } from "./user";