Mixing Named and Default Exports
You'll learn: How to export and import both named and default values from the same module.
The Core Idea
A single module doesn't have to choose between named exports and a default export—you can use both in the same file. This is useful when a module has one primary thing to export (the default) plus some helpful utilities or constants (named exports).
Think of it like a toolbox: the default export is the main tool everyone reaches for first, while the named exports are specialized attachments that some users might need.
How It Works
In your module, you can mix export styles:
// utils.js
export const API_URL = 'https://api.example.com';
export const MAX_RETRIES = 3;
export default function fetchData(endpoint) {
return fetch(`${API_URL}/${endpoint}`);
}
When importing, you combine the syntax:
// app.js
import fetchData, { API_URL, MAX_RETRIES } from './utils.js';
console.log(API_URL); // 'https://api.example.com'
console.log(MAX_RETRIES); // 3
fetchData('users'); // calls the default function
Notice: The default import (fetchData) comes first, without braces. The named imports ({ API_URL, MAX_RETRIES }) follow, inside braces. They're separated by a comma.
When to Use This Pattern
This pattern shines when your module has one main function or class that most users need, plus some related constants or helper functions that provide flexibility. The default export says "this is the primary thing," while named exports offer supporting tools.
Key Takeaway: A module can export both a default and named exports simultaneously; import them together by placing the default name first, followed by named imports in braces.