import { formatDate } from "./dates" fails when the module loads if ./dates has no export with that name. The usual causes are a typo, a rename that missed a file, or braces around something that's really the default export.
The rule reads the imported file and follows its export * from lines. It also checks export { name } from. 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. Type-only imports aren't checked.
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:
[lint.rules]
"import/named" = "error" # or "warn"
Every option, set to its default:
[lint.rules]
"import/named" = ["error", {
commonjs = false,
}]
Examples
Reported:
export function formatDate(date) {
return date.toISOString().slice(0, 10);
}
import { fromatDate } from "./dates.js";
export { parseDate } from "./dates.js";
console.log(fromatDate(new Date()));
Not reported:
export function formatDate(date) {
return date.toISOString().slice(0, 10);
}
export * from "./dates.js";
import { formatDate } from "./dates.js";
import { formatDate as format } from "./index.js";
console.log(formatDate(new Date()), format(new Date()));
Options
| Option | Default | What it does |
|---|---|---|
commonjs | false | Also check names that are destructured from a require() call. |
export function formatDate(date) {
return date.toISOString().slice(0, 10);
}
const { parseDate } = require("./dates.js");
When not to use it
TypeScript already reports an import of a name that isn't exported. In a type-checked project, this rule doesn't find anything new.