Sort and group JavaScript import statements

Paste the top of a JavaScript or TypeScript file and the import block comes back sorted A–Z inside clear groups, from Node built-ins down to files in the same folder.

Live demo · real result from this tool

Sorted imports

import './styles.css'; import fs from 'node:fs'; import axios from 'axios';import { useEffect, useState } from 'react'; import { api } from '@/lib/api'; import { formatDate } from '../utils/date'; import Button from './Button'; export function App() {} 
Imports sorted7
Groups
Side-effect imports (order kept) · Node built-ins · Packages · Internal aliases · Parent directories · Same directory

Processed on your device — nothing is uploaded.

How to use the JS import sorter

  1. Paste your code into the JavaScript box. The import statements must be at the top, after any hashbang, comments or directives.
  2. Keep Blank line between groups ticked to separate groups visually.
  3. Keep Sort names inside { } ticked to order the named imports in each statement.
  4. Copy Sorted imports. The rest of the file follows the imports unchanged.
  5. The stats show how many imports were sorted and which groups were found.

What it does and when to use it

Import blocks grow in the order features were added, which makes them hard to scan and causes merge conflicts when two people add imports at the same spot. A fixed order fixes both. Use this tool on a file before a commit, when you tidy up old code, or to clean an example before you publish it.

It works on the import block only. Format the whole file with the JavaScript formatter, convert require calls first with the CommonJS to ESM converter, and check the result with the JavaScript validator.

How it works

The tool tokenizes the file and reads import statements from the top until it reaches the first other statement. Each import is placed in a group by its module path:

Group Paths that match
Side-effect imports import 'x' with no names (order kept)
Node built-ins node:fs, fs, path, crypto and other core modules
Packages react, axios, @scope/pkg
Internal aliases @/lib/api, ~/utils, #config, /abs/path
Parent directories ../utils/date
Same directory ./Button

Within a group, imports are sorted by path, ignoring case and comparing numbers naturally, so v2 comes before v10. With Sort names inside { }, names in braces are sorted too, ignoring a leading type. Import attributes such as with { type: 'json' } stay with their statement. Dynamic import() and import.meta end the block.

Worked examples

A React component. The default sample:

import './styles.css';

import fs from 'node:fs';

import axios from 'axios';
import { useEffect, useState } from 'react';

import { api } from '@/lib/api';

import { formatDate } from '../utils/date';

import Button from './Button';

Seven imports were sorted into all six groups, and useEffect now comes before useState.

A Node script with a hashbang. With Blank line between groups off, the hashbang and 'use strict' stay on top. The // config loader comment moves with its import, and { z, a, type B } becomes { a, type B, z }.

Code before the imports. If const x = 1; comes first, the tool reports “No import statements found at the top of the file.” and changes nothing.

Limits and tips

  • Imports further down the file are not moved or sorted.
  • Unused imports are not removed; a linter or your editor can do that.
  • Long multi-line imports are kept, and their names are re-printed one per line when sorted.

Frequently asked questions

Why are side-effect imports not sorted?
An import with no names, such as import './polyfills', runs code when it loads. Moving it could change what runs first, for example a polyfill or a CSS reset. The tool keeps these in their original order, in a group of their own at the top.
Where do scoped packages like @scope/pkg go?
In the Packages group with other npm packages. Only paths that start with @/, ~/ or
What happens to comments above an import?
A comment directly above an import moves with that import. A hashbang line, comments at the very top of the file and directives such as 'use strict' stay at the top.
Does it handle require() calls?
No, only static import declarations. Convert CommonJS first with the CommonJS to ESM converter, then sort.
Can I make it match my ESLint or Prettier plugin?
The groups here are fixed. If your project uses a sorting plugin with its own order, run that plugin instead so the result matches your lint checks.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close