01 / 20

ES Modules
export / import

Learn how modern JavaScript splits code into reusable modules using export and import statements.

JavaScript

What Is a Module?

A module is a single JavaScript file that keeps its variables and functions private by default. Nothing leaks into the global scope unless you explicitly export it.

Why Modules Matter

★ Encapsulation
Each file has its own scope: no accidental variable collisions.
★ Reusability
Write a function once, import it in many files.
★ Maintainability
Small, focused files are far easier to debug and update.

The Big Picture

math.js function add(a, b) function multiply(a, b) export { add, multiply } EXPORTED import { add } app.js import { add } from './math.js'; console.log(add(2, 3)); SOURCE MODULE CONSUMING MODULE

Named Exports

A named export lets you expose any number of variables, functions, or classes by name. The importing file must use the exact same name (inside curly braces).

// utils.js export const PI = 3.14159; export function double(x) { return x * 2; }
// main.js import { PI, double } from './utils.js'; console.log(double(PI)); // 6.28318

Default Exports

A default export designates a single main value from a module. The importer can name it anything they like: no curly braces required.

// greet.js export default function greet(name) { return `Hello, ${name}!`; }
// app.js import sayHello from './greet.js'; console.log(sayHello('World')); // Hello, World!

Named vs Default

Named Export Default Export
Syntax (export) export const x = ... export default ...
Syntax (import) import { x } from ... import x from ...
Per module Unlimited Exactly one
Renaming Needs as keyword Free: any name works
Best for Utility libraries Single-purpose modules

Renaming with as

If two modules export a value with the same name, you can rename on import using the as keyword to avoid collisions.

// app.js import { render as renderHeader } from './header.js'; import { render as renderFooter } from './footer.js'; renderHeader(); renderFooter();

Re-exporting from a Barrel

A barrel file (often called index.js) collects exports from multiple modules into one convenient entry point.

// utils/index.js (barrel) export { add, subtract } from './math.js'; export { formatDate } from './date.js'; export { default as Logger } from './logger.js';
// app.js import { add, formatDate, Logger } from './utils/index.js';

How to Apply It

1
Create the source file. Write the functions or constants you want to share.
2
Add export keywords. Use named exports for multiple values or default for one main value.
3
Import in the target file. Match curly braces for named, no braces for default. Always include the file path.
4
Use the imported values. They behave exactly like locally defined variables.
5
Set type="module" in your HTML script tag, or use a bundler (Vite, Webpack) that handles module resolution.

Example 1: Named Exports

// temperature.js export function toFahrenheit(c) { return c * 9 / 5 + 32; } export function toCelsius(f) { return (f - 32) * 5 / 9; }
// weather.js import { toFahrenheit } from './temperature.js'; const bodyTemp = toFahrenheit(37); console.log(bodyTemp); // 98.6

Example 2: Default + Named

// user.js export default class User { constructor(name) { this.name = name; } } export const MAX_USERS = 100;
// app.js import User, { MAX_USERS } from './user.js'; const admin = new User('Alice'); console.log(admin.name, MAX_USERS); // Alice 100

Example 3: Namespace Import

Use import * as to pull every named export into a single namespace object. This is handy when a module exports many related items.

// colors.js export const RED = '#f87171'; export const GREEN = '#4ade80'; export const BLUE = '#818cf8';
// theme.js import * as Colors from './colors.js'; console.log(Colors.RED); // #f87171 console.log(Colors.GREEN); // #4ade80

Common Mistakes

✕ Missing Curly Braces
Writing import add instead of import { add } for a named export silently imports undefined.
✕ Two Default Exports
Placing export default twice in one file is a syntax error. Only one default per module.
✕ Missing File Extension
In native ES modules (browsers), you must include .js in the path. Bundlers may auto-resolve, but the browser will not.

Cheat Sheet

Pattern Export Syntax Import Syntax
Named (inline) export const x = ... import { x } from ...
Named (list) export { a, b } import { a, b } from ...
Default export default fn import fn from ...
Rename export { a as b } import { a as b } from ...
Namespace - import * as M from ...
Re-export export { x } from ... -

Question 1 of 5

Which syntax correctly imports a named export called sum from a module?

Question 2 of 5

How many default exports can a single JavaScript module have?

Question 3 of 5

Given export default function greet() {} in greet.js, which import is valid?

Question 4 of 5

What does import * as Utils from './utils.js'; do?

Question 5 of 5

You want to import a named export render from two different modules without a naming collision. Which approach works?