Linter · Rules
sort-imports
Enforce sorted import declarations and sorted names inside them
A sorted list of imports is quick to scan, and two people who add an import put it in the same place.
The rule checks three things:
- The names inside
{ ... }are in alphabetical order. - Declarations are grouped by syntax. The default order is imports with no names, then
* asimports, then imports of several names, then imports of one name. - Inside a group, declarations are sorted alphabetically by the first name they import. The rule compares the local name, not the module path.
Capital letters sort before lowercase letters unless you set ignoreCase.
--fix sorts the names inside { ... }, unless there are comments between them. It doesn't move declarations.
Configure
Off by default. To turn it on:
[lint.rules]
"sort-imports" = "error" # or "warn"
Every option, set to its default:
[lint.rules]
"sort-imports" = ["error", {
ignoreCase = false,
ignoreDeclarationSort = false,
ignoreMemberSort = false,
memberSyntaxSortOrder = ["none", "all", "multiple", "single"],
allowSeparatedGroups = false,
}]
Examples
Reported:
import { writeFile, readFile } from "node:fs/promises";
fs sorts before path:
import path from "node:path";
import fs from "node:fs";
An import of several names comes before an import of one name:
import path from "node:path";
import { readFile, writeFile } from "node:fs/promises";
Not reported:
import "./setup.js";
import * as z from "zod";
import { readFile, writeFile } from "node:fs/promises";
import fs from "node:fs";
import path from "node:path";
Options
| Option | Default | What it does |
|---|---|---|
ignoreCase | false | Ignore case when comparing names. |
ignoreDeclarationSort | false | Don't check the order of declarations, only the names inside { ... }. |
ignoreMemberSort | false | Don't check the names inside { ... }. |
memberSyntaxSortOrder | ["none", "all", "multiple", "single"] | The order of the syntax groups. All four names must be present. |
allowSeparatedGroups | false | Start over after a blank line or a line that isn't an import, so each block is sorted separately. |
ignoreDeclarationSort is useful when another tool orders your imports by module path:
import path from "node:path";
import { readFile, writeFile } from "node:fs/promises";
With allowSeparatedGroups, the blank line starts a new block:
import path from "node:path";
import fs from "node:fs";
With ignoreCase:
import { createElement, Fragment, useState } from "react";
With memberSyntaxSortOrder set to put single imports first:
import path from "node:path";
import { readFile, writeFile } from "node:fs/promises";
import "./setup.js";