JavaScript theorytheory 0/50 · 0%
Structure · easy

10. Modules

import/export, named vs default, and CommonJS.

ES modules use `import`/`export`, are always strict mode, and are evaluated once and cached. Node's older CommonJS uses `require`/`module.exports`.

// math.js
export function add(a, b) { return a + b; }
export default class Client {}

// app.js
import Client, { add } from "./math.js";

Named exports are easier to refactor and tree-shake; prefer them over default exports.

Check your understanding

  1. 1. How many times is a module body evaluated?

  2. 2. Which is the CommonJS import form?

  3. 3. Why prefer named exports?