JavaScript Modules (ESM vs. CommonJS)
Mastering JavaScript modularity: ES Modules (import/export), CommonJS (require/module.exports), dynamic imports, and tree-shaking.
JavaScript Modules (ESM vs. CommonJS)
Modules allow code to be divided into separate, reusable, self-contained files with explicit import and export boundaries.
1. ES Modules (ESM) β The Modern Standard
ES Modules are static, asynchronous, and supported natively in all modern browsers and Node.js ("type": "module" in package.json or .mjs extension).
Named Exports
// math.js
export const PI = 3.14159;
export function add(a, b) {
return a + b;
}
export function subtract(a, b) {
return a - b;
}Named Imports
// app.js
import { PI, add } from "./math.js";
console.log(add(10, 20)); // 30Default Export (One per file)
// logger.js
export default function log(msg) {
console.log(`[LOG]: ${msg}`);
}
// Consuming default import:
import log from "./logger.js";
log("System initialized");2. CommonJS (CJS) β Legacy Node.js Standard
CommonJS is synchronous and historically used in Node.js (require and module.exports):
// utils.cjs
const formatName = name => name.trim().toUpperCase();
module.exports = { formatName };
// server.cjs
const { formatName } = require("./utils.cjs");3. Dynamic Imports (import())
Dynamic imports load modules on-demand at runtime, enabling code-splitting:
async function loadAnalytics() {
const { trackEvent } = await import("./analytics.js");
trackEvent("button_clicked");
}4. Tree-Shaking
Because ES Module imports and exports are static (declarations cannot appear inside if statements), bundlers like Rollup, Webpack, and Turbopack can analyze the AST at build time and discard ("tree-shake") unused exports, drastically shrinking client bundle sizes.
Related Topics
JavaScript Destructuring and Spread Operator
Mastering destructuring assignment: array unpacking, object property extraction, renaming, default values, and rest/spread syntax (...).
JavaScript Promises and Asynchronous State
Mastering JavaScript Promises: pending/fulfilled/rejected states, then/catch/finally chaining, Promise.all, Promise.allSettled, and Promise.race.