Feature | CommonJS | ES Modules |
Syntax | require , module.exports | import , export |
File Extension | .js (or .cjs for Node.js) | .js (or .mjs for Node.js) |
Loading | Synchronous | Asynchronous (in browsers) |
Usage | Primarily used in Node.js | Supported in both Node.js and browsers |
Default Export | module.exports = value; | export default value; |
Named Exports | exports.name = value; | export const name = value; |
Circular Dependencies | Allowed but may cause issues | Handled more gracefully |
Importing | const module = require('module'); | import module from 'module'; |
Dynamic Import | Not natively supported | Supported via import() syntax |
Tree Shaking | Not optimized for tree shaking | Optimized for tree shaking |
Standardization | Non-standard, Node.js specific | ECMAScript standard |
CommonJS Example
In CommonJS, modules are loaded using require
and exported using module.exports
or exports
.
File: math.js
// Exporting in CommonJS
const add = (a, b) => a + b;
const subtract = (a, b) => a - b;
module.exports = { add, subtract };
// Importing in CommonJS
const math = require('./math');
console.log(math.add(2, 3)); // Output: 5
console.log(math.subtract(5, 2)); // Output: 3
ES Modules Example
In ES Modules, modules are imported using import
and exported using export
or export default
.
File: math.mjs
// Exporting in ES Modules
export const add = (a, b) => a + b;
export const subtract = (a, b) => a - b;
// Or using default export
// export default { add, subtract };
// Dynamic Import in ES Modules
import('./math.mjs').then((math) => {
console.log(math.add(2, 3)); // Output: 5
console.log(math.subtract(5, 2)); // Output: 3
});
Conclusion: CommonJS vs ES Modules
Both CommonJS and ES Modules serve the purpose of module management in JavaScript, but they have distinct differences that affect their usage.
CommonJS:
Synchronous Loading: Suitable for server-side applications, primarily used in Node.js. Modules are loaded synchronously, which can block execution.
Syntax: Uses
require()
for importing andmodule.exports
for exporting.Dynamic Loading: Allows conditional loading of modules using
require()
, but lacks true asynchronous loading.Non-standard: Although widely adopted in Node.js, CommonJS is not part of the ECMAScript standard.
ES Modules:
Asynchronous Loading: Designed for both client-side and server-side applications, supporting asynchronous loading, which improves performance in web browsers.
Syntax: Uses
import
for importing andexport
for exporting. Supports both named exports and default exports.Dynamic Imports: Provides native support for dynamic imports using
import()
, enabling loading of modules at runtime.Standardized: Part of the ECMAScript specification, making it more compatible across various environments, including modern browsers and Node.js.
Summary:
Use CommonJS when working primarily in Node.js applications that require synchronous module loading.
Use ES Modules for new projects, especially those intended to run in both browsers and Node.js, due to their standardization and support for asynchronous loading.