What is the difference between CommonJS and ES Modules?

Answer

Two module systems exist in JavaScript. CommonJS (CJS): used in Node.js, require()/module.exports. Synchronous loading — modules are loaded and executed on demand. Exports are cached after first load. Dynamic — can use require in conditionals. The entire module object is exported. ES Modules (ESM): the standard for browsers and modern Node.js, import/export. Static — all imports are resolved at parse time (enables tree-shaking and static analysis). Asynchronous loading (important for browsers). Named and default exports. Live bindings — imported values reflect changes in the exporting module. "type": "module" in package.json or .mjs extension for ESM in Node. Interop issues: ESM can import CJS (sort of), but CJS cannot use ESM without workarounds. In browsers: <script type="module">. The ecosystem is converging on ESM — modern packages publish both CJS and ESM ("dual package" exports in package.json). Tree-shaking only works with ESM.