Developer tools
export default
A file's one "main" export, imported without braces and under any name.
Why it exists
Many modules are essentially one thing: a Button component, a Cart class, a formatPrice function. Writing import { Button } from "./Button.js" repeats the name twice. A default export says: "this file *is* that thing". The import is shorter, and you can pick a name that fits your code — handy when the project already has its own Button.
Where it's used
- React and Vue components:
export default function App() { … }andimport App from "./App.jsx". - Framework pages and configs: Next.js expects
export defaultin a page file, Vite invite.config.js. - A class module:
export default class Cart { … }— the file is the class. - Re-exporting from a folder:
export { default as Button } from "./Button.js"inindex.js, so the outside can import by name.
Syntax
// money.js
export default function formatPrice(n) { … }
// or: export default value;
// main.js
import formatPrice from "./money.js"; // any name
import price, { currency } from "./money.js"; // together with named ones
import { default as fp } from "./money.js"; // the same, by the name default
export { default as formatPrice } from "./money.js"; // re-export by nameExamples
// money.js: export default function formatPrice(n) { return "$" + n; }
// main.js: import price from "./money.js";
// A default export is an ordinary export named "default":
const moneyExports = { default: (n) => "$" + n };
const price = moneyExports.default;
price(250); // → "$250"// cart.js: export default class Cart { … }
// export const TAX = 0.2;
// main.js: import Cart, { TAX } from "./cart.js";
// Default and named exports live in one object:
const cartExports = { default: class Cart {}, TAX: 0.2 };
const { default: Cart, TAX } = cartExports;
Cart.name; // → "Cart"
TAX; // → 0.2// Two files gave the same default two different names:
// header.js: import formatPrice from "./money.js";
// footer.js: import price from "./money.js";
const moneyExports = { default: (n) => "$" + n };
const formatPrice = moneyExports.default;
const price = moneyExports.default;
formatPrice === price; // → true — it's one functionResults after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
Under the hood, the default export is an ordinary named export with the special name default. import x from "./m.js" is shorthand for import { default as x } from "./m.js". That's why the import name can be anything: you're just naming whatever sits under default. With import * as m it shows up as m.default.
export default is followed by a value: an expression, a function or a class. A variable declaration doesn't fit there — export default const x = 1 is a syntax error; write const x = 1; export default x;.
A file can have one default export and any number of named ones: import Cart, { TAX } from "./cart.js".
Traps
How it differs from…
- Modules:
importandexport A named export: one name across the whole project, imported in braces, any number per file. Default: one per file, no braces, any name. When in doubt, go with named.
When to pick something else
If a module has several equal functions (utils/format.js with formatPrice, formatDate, plural), none of them is the main one — use named exports. And big teams often ban default exports with a linter: consistent names across the project matter more than a short import.