JavaScript Modules
ES modules with import and export, named vs default exports, and dynamic imports.
What you'll learn
- Exporting with named and default exports
- Importing named and default bindings
- Renaming imports with the as keyword
- Dynamic import() for lazy loading
- Module scope, live bindings, and the import meta
Concept
Why Modules?
Before ES modules, JavaScript had no built-in module system. Developers used patterns like IIFEs, CommonJS (require/module.exports), or AMD. ES modules (ESM) are the native standard: each file is a module with its own scope, and you explicitly declare dependencies with import and export. This enables tree-shaking, static analysis, and clear dependency graphs.
Named Exports
A named export declares a specific binding that importers must reference by name. You can export declarations inline (export function foo()) or export a list at the bottom (export { foo, bar }). Named exports are preferred for libraries because they are explicit, support autocomplete, and allow importing only what you need.
Default Exports
A module can have at most one default export, imported with any name the importer chooses: import MyComponent from './file'. Default exports are convenient for a module's primary purpose (a main class or component) but can reduce refactoring safety because the import name is arbitrary. Many style guides now prefer named exports exclusively.
Renaming and Aggregation
Use as to rename on both sides: export { foo as bar } and import { foo as bar } from '...'. A barrel file re-exports from many modules in one place (export * from './users'), giving consumers a single import path. Be cautious with export * — it can create name collisions.
Dynamic Import
import() is a function that returns a promise, loading a module on demand at runtime. This is the basis of code splitting and lazy loading in modern bundlers. Use it to load heavy features only when needed, reducing the initial bundle size. The statically declared import at the top is still preferred for dependencies needed at startup because it enables static analysis and tree-shaking.
Example
// --- math-utils.js (named exports) ---
export const PI = 3.14159;
export function area(radius) {
return PI * radius ** 2;
}
export function circumference(radius) {
return 2 * PI * radius;
}
// --- logger.js (default export) ---
export default class Logger {
constructor(prefix) {
this.prefix = prefix;
}
log(msg) {
console.log(`[${this.prefix}] ${msg}`);
}
}
// --- main.js (consuming the modules) ---
// Named import — must match the export names
import { area, circumference } from "./math-utils.js";
// Default import — any name works
import Logger from "./logger.js";
// Renaming a named import
import { PI as TAU_HALF } from "./math-utils.js";
console.log(area(5)); // 78.54
console.log(TAU_HALF); // 3.14159
new Logger("App").log("started");
// Dynamic import — loads on demand, returns a promise
async function loadHeavyFeature() {
const { area: lazyArea } = await import("./math-utils.js");
console.log("Lazy area:", lazyArea(10));
}
loadHeavyFeature();
Shows named exports, a default export class, named and default imports, renaming with as, and a dynamic import() that loads a module on demand inside an async function.
Try it
- JSON Formatter
Module exports often return JSON-like data — format and inspect it.
- HTTP Request Tester
Dynamic imports are async like fetch — test async loading patterns.
Common mistakes
The mistake
Mixing up default and named import syntax
The fix
Default imports have no braces: import Logger from './logger'. Named imports use braces: import { area } from './math'. You can combine them: import Logger, { area } from './file' only if the file has both.
The mistake
Using circular imports that cause undefined values
The fix
If module A imports B and B imports A, one will see the other's exports as undefined during initialization. Break the cycle by extracting shared code into a third module, or defer the cross-access to a function call after initialization.
Related Resources
Related Cheatsheets
- JavaScript Cheatsheet
import/export syntax, named vs default, and dynamic import.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.