JavaScript theorytheory 0/50 · 0%
Tooling · medium

31. Modules

import, export and why CommonJS still appears.

ES modules use `import`/`export`, are statically analysable (enabling tree shaking) and always run in strict mode. Node's older CommonJS uses `require`/`module.exports` and resolves at runtime.

export const RPC = "https://sepolia.base.org";
export default function client() { /* ... */ }
import client, { RPC } from "./client.js";

Prefer named exports: they refactor and autocomplete better than a default.

Check your understanding

  1. 1. Why can ES modules be tree-shaken?

  2. 2. What is the CommonJS import form?

  3. 3. Which export style is easier to refactor?