Course contentsShow
← JavaScript
Lesson 1424 of 2,72331. ModulesFree lesson

Accessing Named Exports

Destructuring named exports from the resolved module namespace object after dynamic import.

Accessing Named Exports

What you'll learn: How to extract specific named exports from a dynamically imported module using destructuring.

When you use dynamic import(), it returns a promise that resolves to a module namespace object. This object contains all the exports from that module as properties. To access named exports from this object, you use destructuring—just like you would with any JavaScript object.

The Pattern

After awaiting a dynamic import, the result is an object where each named export becomes a property:

// math.js exports:
// export const add = (a, b) => a + b;
// export const subtract = (a, b) => a - b;

// Accessing named exports
const mathModule = await import('./math.js');
const result = mathModule.add(5, 3); // 8

Destructuring on Import

The cleaner approach is to destructure the named exports immediately:

const { add, subtract } = await import('./math.js');
const sum = add(10, 5);      // 15
const diff = subtract(10, 5); // 5

This works exactly like destructuring any object—you extract only the exports you need by name.

Think of It This Way

Imagine ordering a meal kit (dynamic import). When it arrives, you get a box (module namespace object) containing labeled containers (named exports). You can either reach into the box each time you need something (mathModule.add), or unpack the specific containers you'll use onto your counter (const { add } = ...).

Key Takeaway: After a dynamic import resolves, use destructuring to cleanly extract named exports from the module namespace object, making your code more readable and concise.