A module can have exactly **one default export**. It's typically used for the primary thing a module provides (a class, component, or function). The importer can give it any name — unlike named exports which must match. Common pattern: `export default class MyComponent`.
1Understanding Default Export
A module can have exactly one default export. It's typically used for the primary thing a module provides (a class, component, or function). The importer can give it any name — unlike named exports which must match. Common pattern: export default class MyComponent.
Default exports can't be tree-shaken as effectively as named exports. Prefer named exports for utility libraries.
// userService.js - one main concern
class UserService {
async getUser(id) { /* ... */ }
async updateUser(id, data) { /* ... */ }
}
export default new UserService(); // singleton
// ─────────────────────────
// main.js
import userService from './userService.js';
const user = await userService.getUser(1);2Practical Example
Here is a real-world application of Default Export showing how it is used in production JavaScript code.
// Named default export (best practice)
export default function fetchUser(id) {
return fetch(`/api/users/${id}`).then(r => r.json());
}
// vs anonymous (avoid)
export default (id) => fetch(`/api/users/${id}`); // no name in stack trace3Best Practices
Follow these guidelines when working with Default Export:
1. Use default for the one primary thing a module does
2. Prefer named exports for utility modules (better tree-shaking)
3. Avoid anonymous default exports — they hurt debugging
Tip: Default exports can't be tree-shaken as effectively as named exports. Prefer named exports for utility libraries.
// userService.js - one main concern
class UserService {
async getUser(id) { /* ... */ }
async updateUser(id, data) { /* ... */ }
}
export default new UserService(); // singleton
// ─────────────────────────
// main.js
import userService from './userService.js';
const user = await userService.getUser(1);