Explain the differences between CommonJS modules and ES modules in JavaScript
By FrontendPro Editorial Team Updated 8/8/2026
Answer
In JavaScript, modules are reusable pieces of code that encapsulate functionality, making it easier to manage, maintain, and structure your applications. Modules allow you to break down your code into smaller, manageable parts, each with its own scope.
CommonJS is an older module system that was initially designed for server-side JavaScript development with Node.js. It uses the require() function to load modules and the module.exports or exports object to define the exports of a module.
// my-module.js
const value = 42;
module.exports = { value };
// main.js
const myModule = require("./my-module.js");
console.log(myModule.value); // 42
ES Modules (ECMAScript Modules) are the standardized module system introduced in ES6 (ECMAScript 2015). They use the import and export statements to handle module dependencies.
// my-module.js
export const value = 42;
// main.js
import { value } from "./my-module.js";
console.log(value); // 42
CommonJS vs ES modules
| Feature | CommonJS | ES modules |
|---|---|---|
| Module Syntax | require() for importing module.exports for exporting | import for importing export for exporting |
| Environment | Primarily used in Node.js for server-side development | Designed for both browser and server-side JavaScript (Node.js) |
| Loading | Synchronous loading of modules | Asynchronous loading of modules |
| Structure | Dynamic imports, can be conditionally called | ES modules use static top-level import/export statements, while dynamic loading is supported separately via the import() |
| File extensions | .js (default) | .mjs or .js (with type: "module" in package.json) |
| Browser support | Not natively supported in browsers | Natively supported in modern browsers |
| Optimization | Limited optimization due to dynamic nature | Allows for optimizations like tree-shaking due to static structure |
| Compatibility | Widely used in existing Node.js codebases and libraries | Newer standard, but gaining adoption in modern projects |
Read the detailed answer on GreatFrontEnd which allows progress tracking, contains more code samples, and useful resources.
