Imports Sorting
Ce contenu n’est pas encore disponible dans votre langue.
Biome allows sorting import statements using natural ordering.
This feature is enabled by default but can be opted-out via configuration:
How imports are sorted
Section titled How imports are sortedImport statements are sorted by “distance”. Modules that are “farther” from the user are put on the top, modules “closer” to the user are put on the bottom:
- modules imported via
bun:
protocol. This is applicable when writing code run by Bun; - built-in Node.js modules that are explicitly imported using the
node:
protocol and common Node built-ins such asassert
; - modules imported via
npm:
protocol. This is applicable when writing code run by Deno; - modules that contain the protocol
:
. These are usually considered “virtual modules”, modules that are injected by your working environment, e.g.vite
; - modules imported via URL;
- modules imported from libraries;
- modules imported via absolute imports;
- modules imported from a name prefixed by
#
. This is applicable when using Node’s subpath imports; - modules imported via relative imports;
- modules that couldn’t be identified by the previous criteria;
For example, given the following code:
They will be sorted like this:
You can apply the sorting in two ways: via CLI or VSCode extension.
Grouped imports
Section titled Grouped importsIt’s widespread to have import statements in a certain order, primarily when you work on a frontend project, and you import CSS files:
Another common case is import polyfills or shim files, that needs to stay at the top file:
In these cases, Biome will sort all these three imports, and it might happen that the order will break your application.
To avoid this, create a “group” of imports. You create a “group” by adding a new line to separate the groups.
By doing so, Biome will limit the sorting only to the import statements that belong to the same group:
Side effect imports
Section titled Side effect importsSide effect imports are import statements that usually don’t import any name:
Since it is difficult to determine which side effects a module triggers, the import sorter assumes that each side effect import forms its own import group.
For example, the following imports form 4 import groups.
Each group is independently sorted as follows:
Import sorting via CLI
Section titled Import sorting via CLIUsing the command check
, with the option --write
. If you want only order the imports, you can use check like so:
Import sorting via VSCode extension
Section titled Import sorting via VSCode extensionThe Biome VS Code extension supports imports sorting through the “Organize Imports” code action. By default, this action can be run using the ⇧+Alt+O keyboard shortcut, or is accessible through the Command Palette (Ctrl/⌘+⇧+P) by selecting Organize Imports.
You can add the following to your editor configuration if you want the action to run automatically on save instead of calling it manually: