import/first
Require all imports to come before every other statement in the file
Imports are hoisted. The imported modules load and run before any line of the file that imports them, no matter where the import is written. An import below other code looks like it runs later, but it doesn't.
Directives such as "use client" can come before the imports.
--fix moves the imports to the top, after the ones that are already there.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"import/first" = "error" # or "warn"Every option, set to its default:
bunfig.toml
[lint.rules]
# "absolute-first" or "disable-absolute-first"
"import/first" = ["error", "disable-absolute-first"]Examples#
Reported:
import { loadConfig } from "./config.js";
loadConfig(); // runs after "./server.js" has loaded, not before
import { startServer } from "./server.js";
startServer();Not reported:
"use client";
import { loadConfig } from "./config.js";
import { startServer } from "./server.js";
loadConfig();
startServer();Options#
| Option | What it does |
|---|---|
"disable-absolute-first" (default) | Imports can be in any order. |
"absolute-first" | Also report an import of a package, or any path that doesn't start with ., after a relative import. |
import { loadConfig } from "./config.js";
import express from "express";import express from "express";
import { loadConfig } from "./config.js";