Course contentsShow
← JavaScript
Lesson 1543 of 2,72333. JavaScript in Node.jsFree lesson

Named Exports and Default Exports in ESM

Use ES module export syntax for both named and default exports in Node.js applications.

Named Exports and Default Exports in ESM

You'll learn how to export values from ES modules using both named and default export syntax in Node.js.

ES modules give you two ways to share code from a file: named exports and default exports. Think of named exports like items on a menu with specific names, while a default export is like the "house special" — the main thing a module offers.

Named Exports

Named exports let you export multiple values from a module, each with its own identifier:

// math.js
export const PI = 3.14159;
export function add(a, b) {
  return a + b;
}
export function subtract(a, b) {
  return a - b;
}

You can also export after declaration:

// math.js
const PI = 3.14159;
function add(a, b) { return a + b; }

export { PI, add };

Import them by name:

// app.js
import { PI, add } from './math.js';
console.log(add(5, 3)); // 8

Default Exports

A module can have one default export — its primary value:

// calculator.js
export default function calculate(x, y) {
  return x * y;
}

Import it with any name you choose:

// app.js
import calc from './calculator.js';
console.log(calc(4, 5)); // 20

Combining Both

You can use named and default exports together:

// utils.js
export default function main() { }
export const helper = () => { };
import main, { helper } from './utils.js';

Key Takeaway: Named exports let you export multiple values with specific identifiers, while default exports define a module's primary export — use named exports for utility collections and default exports for a module's main functionality.