import/export

Disallow exporting the same name twice, and export * from a file with no named exports

Also called import-x/export

If two files both export format and you re-export both with export *, JavaScript silently leaves format out, and import { format } fails somewhere else. If your own file exports format too, yours wins and the other one is hidden.

The rule reads the file behind each export * and reports every name that's exported more than once. It also reports an export * from a file that has no named exports. That line does nothing, because export * never passes on a default export.

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.

In TypeScript, function overloads are allowed, and so are a type and a value with the same name, and a namespace merged with a function, class or enum.

Configure#

Off by default. To turn it on:

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

Examples#

Reported:

dates.js
export function format(date) {
  return date.toISOString();
}
numbers.js
export function format(number) {
  return number.toFixed(2);
}
example.js2 problems
export * from "./dates.js";
export * from "./numbers.js";
logger.js
export default function log(message) {
  console.log(message);
}
example.js1 problem
export * from "./logger.js";
example.ts2 problems
const fallback = "en";

export const locale = "de";
export { fallback as locale };

Not reported:

dates.js
export function format(date) {
  return date.toISOString();
}
numbers.js
export function format(number) {
  return number.toFixed(2);
}
example.jsNo problems
export { format as formatDate } from "./dates.js";
export * from "./numbers.js";
logger.js
export default function log(message) {
  console.log(message);
}
example.jsNo problems
export { default as log } from "./logger.js";
example.tsNo problems
export function parse(input: string): number;
export function parse(input: number): number;
export function parse(input: string | number): number {
  return Number(input);
}

export namespace parse {
  export const radix = 10;
}

export type Level = "info" | "error";
export const Level = { info: "info", error: "error" } as const;