Skip to content

QuizNot attempted

CommonJS vs ESM

Which statements about CommonJS vs ES modules are true?

Select all that apply.

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 import is 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: require is 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 can require() synchronous ESM).