A module can have one default export: its main thing.
export default class Logger {
log(message) {
console.log("[log]", message);
}
}
Import it without curly braces, and name it however you like:
import Logger from "./Logger.js";
import Log from "./Logger.js"; // same thing, different local name
new Logger().log("hello");
Other forms
export default function greet(name) { return "Hi " + name; } // function
export default 42; // a value
export default { theme: "dark" }; // an object
Mixing default and named
export default function request(url) { /* ... */ }
export const BASE_URL = "https://api.example.com";
import request, { BASE_URL } from "./api.js";
Try it
<script type="module">
const source = "data:text/javascript," + encodeURIComponent(`
export default function greet(name) { return "Hello, " + name; }
export const VERSION = 2;
`);
const module = await import(source);
console.log(module.default("Maya")); // → Hello, Maya
console.log(module.VERSION); // → 2
</script>
A default export appears as the default property of the module object, which is how dynamic import() exposes it.
Default vs named
| Named export | Default export | |
|---|---|---|
| Per module | many | one |
| Import syntax | { name } | any name |
| Renaming | explicit (as) | free, in every file |
| Editor and search friendliness | better | worse (names can differ in each file) |
| Typical use | utilities, constants, many functions | a class or component that is the whole point of the file |
Which should I use?
Both are valid. Many teams prefer named exports because the name is the same everywhere, it is easy to search for, and typos in imports are caught. Default exports are common for classes and UI components, and some frameworks require them for specific files (for example, page components). Pick one convention for your project, and follow it.
Braces mismatch
import { Logger } from "./Logger.js" fails for a default export, and import Logger from ... fails for a named export. If you see “does not provide an export named”, check which kind you used.