Objects, Functions, and Classes25% of the exam
Modules: import, export and CommonJS
How to import and export from an ES module (named, default, namespace, aliases, re-exports and dynamic import), why imports are hoisted read-only live bindings evaluated once, what script type=module changes, and how CommonJS require/module.exports and Node's module-type rules differ.
Given a JavaScript module, give examples of how to use the module.
~7 min read · 13 practice questions ·
What the exam expects
- Given a module's exports, write the import that reaches them: default, named, renamed with
as, namespace (* as), or a mix. - Spot invalid
exportandimportsyntax, and fix a barrel file that re-exports. - Predict output that depends on live bindings, read-only imports, hoisted imports and the evaluate-once rule.
- Load a module on demand with
import()and explain what<script type="module">changes. - Use CommonJS
require/module.exportscorrectly and decide whether Node treats a file as ESM or CommonJS. - Key terms: named export (exported under its own name; imported in braces); default export (one per module; imported without braces under any name); namespace object (
import * as ns; one property per export); live binding (an import that always shows the exporter's current value); module specifier (the string afterfrom).
Key facts
Default exports
- Detail
- One per module; imported without braces under any name
Default plus named
- Detail
import def, { a } fromwith the default first
Namespace object
- Detail
- Keys are export names; default is under
default
export *- Detail
- Re-exports named exports, not the default
Imported bindings
- Detail
- Live and read-only; object properties stay mutable
Import placement
- Detail
- Top level only; hoisted above the module's code
import()- Detail
- Returns a promise for the namespace object
Module scripts
- Detail
- Deferred, strict, module-scoped, fail from
file://
Top-level
this in a module- Detail
undefined
Reassigning
exports- Detail
- Breaks the link to
module.exports
require cache- Detail
- Same object on every call; code runs once
.js with no "type"- Detail
- Run as CommonJS; retried as ESM only if ES module syntax is found
ESM relative specifiers
- Detail
- File extension mandatory
Sources for this section (8)
- exportdeveloper.mozilla.org (opens in a new tab)
- importdeveloper.mozilla.org (opens in a new tab)
- import()developer.mozilla.org (opens in a new tab)
- JavaScript modulesdeveloper.mozilla.org (opens in a new tab)
- thisdeveloper.mozilla.org (opens in a new tab)
- Modules: CommonJS modules (Node.js)nodejs.org (opens in a new tab)
- Modules: Packages (Node.js)nodejs.org (opens in a new tab)
- Modules: ECMAScript modules (Node.js)nodejs.org (opens in a new tab)
- Why does `typeof Pricing.basePrice` log `undefined` when `basePrice` is the default export?
- When should you use `import()` instead of a static import?
- Why does a top-level `var` in a module script not appear on `window`?
- What does `require('./x')` return the second time it is called?
- How does Node treat a `.js` file whose nearest package.json has `"type": "module"`?
The questions are free to read. Answers with their sources and self-grading are included with the pack.
Keep reading with the JavaScript Developer pack
- Core concepts
- Exam traps
- Worked scenario
The pack includes:
- All 24 lessons of the JavaScript Developer guide, with reading progress
- Interactive decision walks and step-by-step sequence steppers
- Inline practice questions and one-click topic practice (13 questions for this lesson)
- An official source cited for every claim
- An AI tutor that answers from the guide
- Unlimited timed practice exams