Q25: What is the difference between ES Modules (ESM) and CommonJS (CJS)?

CommonJS (require/module.exports) is synchronous and evaluated dynamically at runtime. ES Modules (import/export) are asynchronous, statically analyzed at compile-time (enabling tree-shaking), and strict mode by default.

Key Takeaways
  • CJS loads modules dynamically at runtime using require().

  • ESM uses static import/export syntax allowing build optimization and tree-shaking.

Knowledge Check

Why does ES Module syntax enable Tree-Shaking optimization in bundlers?