absolute/sort-exports
Enforce that top-level export declarations are sorted by exported name, optionally variables before functions.
#Rule Details
Enforces that contiguous blocks of top-level named exports are sorted by exported name, with an autofix that reorders the block. Consistent export order makes files scannable and keeps diffs small. Only blocks with at least minKeys exports are checked, and export type aliases break a block (they are ignored).
Sorting respects several toggles: order (asc/desc), caseSensitive, natural (numeric-aware), and variablesBeforeFunctions (non-function exports first). The rule is dependency-safe — it will not reorder when the current order has a forward reference between exports, nor when sorting would create one — and decorators move with their class.
#Examples
export const gamma = 3;
export const alpha = 1; // → reordered to alpha, beta, gamma
export const beta = 2;variablesBeforeFunctions
// options: [{ variablesBeforeFunctions: true }]
export function build() {}
export const config = {}; // → const sorts before functionSorted ascending
export const alpha = 1;
export const beta = 2;
export const gamma = 3;// A forward dependency is detected, so the block is left unsorted
export const doubled = base * 2;
export const base = 21;#Options
This rule accepts the following options:
| Option | Type | Default | Description |
|---|---|---|---|
order | 'asc' | 'desc' | 'asc' | Sort direction: "asc" or "desc". |
caseSensitive | boolean | false | When false, names are lowercased before comparison (case-insensitive sort). |
natural | boolean | false | Numeric-aware comparison so export2 sorts before export10. |
minKeys | integer | 2 | Minimum number of named exports in a contiguous block before it is checked (minimum 2). |
variablesBeforeFunctions | boolean | false | When true, non-function exports are sorted before function exports. |
{
rules: {
'absolute/sort-exports': [
'warn',
{ order: 'asc', natural: true, variablesBeforeFunctions: true }
]
}
}#Related Rules
#When Not To Use It
If you group exports semantically (by feature or by call order) rather than alphabetically, sorting will fight that structure — leave the rule off or raise minKeys.