Skip to content
NewBun 1.4.5 is out. Release notes
AphrodyBun

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 * as imports, 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:

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

Every option, set to its default:

bunfig.toml
[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

OptionDefaultWhat it does
ignoreCasefalseIgnore case when comparing names.
ignoreDeclarationSortfalseDon't check the order of declarations, only the names inside { ... }.
ignoreMemberSortfalseDon't check the names inside { ... }.
memberSyntaxSortOrder["none", "all", "multiple", "single"]The order of the syntax groups. All four names must be present.
allowSeparatedGroupsfalseStart 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";