CommonJS vs ES Modules
Module Systems: CommonJS vs. ECMAScript Modules
Node.js currently navigates a transitionary period between two major module specifications, each dictating how code is encapsulated and exported.
CommonJS (CJS)
The historic default for Node.js. It utilizes require() and module.exports. CommonJS resolution is synchronous, making it highly dynamic—you can conditionally require modules within if blocks based on runtime variables.
ECMAScript Modules (ESM)
The official JavaScript standard supported natively by browsers and newer versions of Node.js. It uses import and export syntax. ESM is fundamentally asynchronous and statically analyzable, which permits build tools (like Webpack or Rollup) to perform aggressive Tree Shaking (eliminating dead code).
ESM strictly supports Top-Level Await, fundamentally changing how asynchronous initializations are handled at module borders.