Advertisement
Intermediate JavaScript, ES Modules

What's the difference between ES Modules and CommonJS (require/module.exports)?

Short answer

ES Modules use static import/export syntax analyzed at parse time (enabling tree-shaking), run in strict mode by default, and support top-level await. CommonJS's require() is dynamic and synchronous, resolved at runtime, and is Node.js's traditional module system.

Save JavaScript mock interview
Advertisement
AModel answer
ES Modules use static import/export syntax analyzed at parse time (enabling tree-shaking), run in strict mode by default, and support top-level await. CommonJS's require() is dynamic and synchronous, resolved at runtime, and is Node.js's traditional module system.
// ES Module
import { readFile } from 'fs/promises';

// CommonJS
const { readFileSync } = require('fs');
Real-world example
Migrating an older Node.js codebase from require() to import to enable modern bundler optimizations like tree-shaking.
Likely follow-ups
  • How do you use CommonJS modules from within an ES Module file, or vice versa?
Related topics