import/default
Require a default import to match a default export in the imported file
Also called import-x/default
import save from "./storage" asks for the default export of ./storage. If that file only has named exports, the import fails when the module loads, or a bundler gives you undefined. The usual cause is missing braces.
The rule reads the imported file. It doesn't report anything if the file can't be found, or if it has no import or export at all, like a CommonJS module.
It reads JavaScript and TypeScript files, including JSX. With an ESLint config it only reads .js, .mjs and .cjs files, unless settings["import/extensions"] lists others.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"import/default" = "error" # or "warn"Examples#
Reported:
storage.js
export function save(item) {
localStorage.setItem(item.id, JSON.stringify(item));
}example.js1 problem
import saveNo default export found in imported module ./storage.js. from "./storage.js";
save({ id: "draft" });Not reported:
storage.js
export function save(item) {
localStorage.setItem(item.id, JSON.stringify(item));
}example.jsNo problems
import { save } from "./storage.js";
save({ id: "draft" });storage.js
export default function save(item) {
localStorage.setItem(item.id, JSON.stringify(item));
}example.jsNo problems
import save from "./storage.js";
save({ id: "draft" });