JavaScript · Middle · Question 36 of 75
How do ES modules differ from CommonJS?
- ✓Correct answer: Imports are static and analyzed ahead of time; exports are "live" bindings
- 2ES modules work only in Node.js, while CommonJS works only in the browser
- 3CommonJS supports tree-shaking, while ES modules don't
Explanation
import/export are known before execution, which lets bundlers remove unused code (tree-shaking). An imported value reflects the export's current value. require runs at runtime and copies module.exports. ESM is always in strict mode and supports top-level await.