MΓ³dulo 03 Β· Modules & npm β LecciΓ³n 2 de 3 Β· ~8 min
ESM vs CommonJS (and the singleton hiding inside)
Node has two module systems β know both
You just wrote ES Modules (ESM): import / export. It's the modern standard and what this course uses. But Node ran for a decade on an older system, CommonJS (CJS): require() / module.exports. You'll meet CJS in older packages and legacy code, so you need to read it.
The difference an interviewer wants to hear:
- ESM is static β imports are resolved before the code runs, so tools can see the whole dependency graph. That enables tree-shaking (dropping unused exports from a bundle) and top-level await.
- CJS is dynamic and synchronous β require() is a function call that runs at that line and blocks until the module is loaded. Flexible, but nothing can analyse it ahead of time.