Skip to main content

consistent-import

Enforce consistent import declaration style.

Targeted pattern scopeโ€‹

This rule analyzes ImportDeclaration nodes and compares import specifier shape for each module source string.

It distinguishes five styles:

  • side-effect: import "x";
  • default: import x from "x";
  • named: import { x } from "x";
  • mixed: import x, { y } from "x";
  • namespace: import * as x from "x";

What this rule reportsโ€‹

This rule reports imports whose style does not match the expected style.

  • Without options, the first import style seen for a given module source becomes the expected style for subsequent imports from that same source.
  • With style, all imports must match the configured style globally.

Why this rule existsโ€‹

Mixing import styles for the same module creates unnecessary visual noise and can hide duplicate or redundant imports. A consistent style improves readability and keeps import blocks predictable.

โŒ Incorrectโ€‹

import fs from "node:fs";
import { readFileSync } from "node:fs";
// ^^^^^^^^^^^^^^ second import style differs for same source

โœ… Correctโ€‹

import fs from "node:fs";
import path from "node:path";
import { readFileSync } from "node:fs";
import { join } from "node:path";

Behavior and migration notesโ€‹

This rule has no autofix because changing import style can require coordinated identifier updates.

Adopt safely by selecting one preferred style and applying codemods (or manual cleanup) before promoting from warn to error.

Optionsโ€‹

type Options = [
{
style?: "default" | "mixed" | "named" | "namespace" | "side-effect";
},
];
  • style (optional): enforce one style for all import declarations.

Default configurationโ€‹

[{}];

When style is omitted, the first seen import style for each module source is treated as that source's expected style.

Example:

"etc-misc/consistent-import": ["error", { style: "named" }];

Additional examplesโ€‹

import "reflect-metadata";
import "reflect-metadata";
// Consistent side-effect style for the same source.

ESLint flat config exampleโ€‹

import etcMisc from "eslint-plugin-etc-misc";

export default [
{
plugins: { "etc-misc": etcMisc },
rules: {
"etc-misc/consistent-import": "error",
},
},
];

When not to use itโ€‹

Disable this rule if your codebase intentionally mixes import styles for the same module (for example, staged migration from default to named exports).

Package documentationโ€‹

Rule catalog ID: R007

Further readingโ€‹

Adoption resourcesโ€‹

  • Start at warning level in CI, then move to error after cleanup.
  • Use focused codemods/autofix batches per package or directory.