liveBook · Manning

Default export

As explained in Secrets of the JavaScript Ninja, Third Edition

A module export designated as the value available through a default import.

An ECMAScript module can export one default value, either directly or alongside named exports. The importer chooses the local name for that value; an existing variable or function keeps its name hidden from the importer. section 13.1.1

Listing 13.5 Importing a default value along with named values
import hero, { #A
  tsundere, #A
  yandere, #A
} from "./4-default-export.ts"; #A

console.assert(
  hero === "Ranma",
  "Ranma is the hero",
);
console.assert(
  tsundere === "Akane",
  "Akane is the tsundere",
);
console.assert(
  yandere === "Shampoo",
  "Shampoo is the yandere",
);

The import binds the default value to `hero` and named exports to their matching names. section 13.1.1

A default import is not the whole module: namespace imports expose it as `default`. CommonJS interop can complicate this distinction: Node.js `require()` of ESM returns a namespace object, while ESM importing TypeScript compiled to CommonJS may receive the whole exports object as its default; the actual value is then at `.default`. section 13.1.1 section 13.2.1

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Traces default-import naming and namespace access through TypeScript-to-CommonJS output and ESM interop behavior.
Examples
Uses a module exporting Ranma as default alongside Akane and Shampoo as named values, then contrasts CommonJS import behavior.
What you can do after
Evaluate how default-import syntax and CommonJS interop affect access to a module’s exported value.
How it connects
Connects default exports to named exports, namespace imports, CommonJS interop, and TypeScript compilation.

Also explained in

Compare how 4 books explain default export →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.