Course contentsShow
← JavaScript
Lesson 1450 of 2,72331. ModulesFree lesson

CJS and ESM: Two Module Systems

Understanding the fundamental differences between CommonJS and ES Modules in syntax, loading, and execution.

CJS and ESM: Two Module Systems

You'll learn how CommonJS (CJS) and ES Modules (ESM) differ in syntax, loading behavior, and execution.

JavaScript now has two widely-used module systems living side-by-side: CommonJS (the original Node.js system) and ES Modules (the modern standard). Understanding their differences helps you work with both and choose the right one for your project.

Syntax Differences

CommonJS uses require() and module.exports:

// CJS
const helper = require('./helper');
module.exports = { doWork };

ES Modules use import and export:

// ESM
import helper from './helper.js';
export { doWork };

Loading Behavior

CommonJS loads synchronously — when you call require(), Node.js stops and reads the entire file immediately. This works well on servers where files are local.

ES Modules load asynchronously — the browser or runtime can fetch and parse multiple files in parallel before executing any code. This is why ESM needs the type="module" attribute in HTML.

Execution and Binding

CommonJS exports are copies — when you require a module, you get a snapshot of its exported values at that moment.

ES Modules use live bindings — imports stay connected to their source, so changes in the exporting module are reflected in all importing modules.

Practical Implications

  • CommonJS is still dominant in Node.js legacy codebases
  • ESM is the standard for modern browsers and newer Node.js projects
  • You can use dynamic import() to load ESM from anywhere
  • Mixing both systems requires understanding their interoperability rules

Key Takeaway: CommonJS and ES Modules differ fundamentally in syntax (require vs import), loading (synchronous vs asynchronous), and behavior (copied values vs live bindings) — both remain important in the JavaScript ecosystem.