CCA Prep

Lesson 2.4Lesson 10 of 24

Using modules

Given a JavaScript module, give examples of how to use the module.

Using modulesES modulesexport / importstatic, top level onlyimport() for lazy loadingstrict mode by defaultCommonJS (Node.js)module.exports / requirerequire anywhere, synchronousexports is a shortcut tomodule.exportsthe classic Node.js format

Key points

  1. 1Default export: import name from without braces, any name. Named exports: import { name } with the exact exported name, renamed with as.
  2. 2import * as ns gives a namespace object; its default export is ns.default.
  3. 3Imports are live, read-only bindings. Static imports sit at the top level; import() loads later and returns a promise.
  4. 4In Node.js CommonJS, require returns module.exports. Reassigning module.exports breaks the link to exports.
  5. 5In the browser a script needs type="module" to use import.
What the exam guide checks here

Objective wording from the exam guide (English).

  • 2.4.K1ES modules: named and default exports and imports, import * as, re-exports
  • 2.4.K2Dynamic import(); CommonJS require and module.exports in Node.js
  • 2.4.S1Write the import that uses a given module correctly

Lock it in

Answer scenario questions on this topic. Every option comes with an explanation.

Practise 2.4 · 12 questions