import/named

Require each named import to match a named export in the imported file

Also called import-x/named

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:

bunfig.toml
[lint.rules]
"import/named" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"import/named" = ["error", {
  commonjs = false,
}]

Examples#

Reported:

dates.js
export function formatDate(date) {
  return date.toISOString().slice(0, 10);
}
example.js2 problems
import { fromatDate } from "./dates.js";
export { parseDate } from "./dates.js";

console.log(fromatDate(new Date()));

Not reported:

dates.js
export function formatDate(date) {
  return date.toISOString().slice(0, 10);
}
index.js
export * from "./dates.js";
example.jsNo problems
import { formatDate } from "./dates.js";
import { formatDate as format } from "./index.js";

console.log(formatDate(new Date()), format(new Date()));

Options#

OptionDefaultWhat it does
commonjsfalseAlso check names that are destructured from a require() call.
dates.js
export function formatDate(date) {
  return date.toISOString().slice(0, 10);
}
example.jswith { "commonjs": true }1 problem
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.