QuizNot attempted
CommonJS vs ESM
1–4 to choose, Enter to submit
Show answer & explanation
Correct answers:
- ESM imports are static and hoisted, which enables tree-shaking; require() is a runtime function call
- ESM exports are live read-only bindings; CommonJS exports a snapshot of the value
- ESM supports top-level await
- ESM
importis static and hoisted (so bundlers can tree-shake), its exports are live read-only bindings, and it supports top-level await require()is an ordinary runtime call and CJS consumers get a copy of what was exported at that moment.- Not CJS loads async:
requireis synchronous, which is exactly why it can't load async ESM. - In Node mark ESM with
"type": "module"or.mjs - ESM can import CJS, CJS needs
import()(newer Node canrequire()synchronous ESM).