import/prefer-default-export
Require a default export in a file that exports a single name
Also called import-x/prefer-default-export
If a file exports one thing, that thing is what the file is for, and a default export says so. Anyone who imports it doesn't have to look up the name.
The rule skips a file if it has a default export, an export *, or an exported type or interface.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"import/prefer-default-export" = "error" # or "warn"Every option, set to its default:
bunfig.toml
[lint.rules]
"import/prefer-default-export" = ["error", {
target = "single", # "single" or "any"
}]Examples#
example.js1 problem
export function formatDate(date) {Prefer default export on a file with single export.
return date.toISOString();
}example.jsNo problems
export default function formatDate(date) {
return date.toISOString();
}example.jsNo problems
export function formatDate(date) {}
export function formatTime(date) {}example.tsNo problems
export type UserId = number;Options#
| Option | Default | What it does |
|---|---|---|
target | "single" | "single" reports a file with exactly one named export. "any" reports every file that has named exports and no default export. |
With "any", this is reported:
example.jswith
{ "target": "any" }1 problemexport function formatDate(date) {}
export function formatTime(date) {}Prefer default export to be present on every file that has export.When not to use it#
Named exports keep the same name everywhere, which helps auto-import and renaming. If your project prefers them, use import/no-default-export and leave this rule off.