Manuals / JavaScript / Chapter 9
D · Modules & Testing · intermediate · Week 6 · Chapter 9 of 14
ES modules & project structure
ES modules (import/export) replace script-tag soup. Named exports for utilities, default export for main component. Split by responsibility: api.js, render.js, main.js. Vite or native Node "type": "module" for local dev without bundler pain.
Path progress59%
Step 1 of 5
Named exports
export function foo() {} and export const BAR = 1. Import with import { foo, BAR } from "./utils.js". Extensions required in browser and Node ESM.
Example
// utils.js
export function titleCase(str) { /* ... */ }
export function slugify(str) { /* ... */ }
// main.js
import { titleCase, slugify } from "./utils.js"Do this now
Move your utility functions to utils.js. Import them in main.js.
Was this step clear?
Chapter learning outcomes
- export / import
- Default vs named exports
- Module scope
- Vite setup
- Barrel files lite
Clear these before you leave
Side quest
Module refactor
Take any script >100 lines. Split into 3+ modules with clear boundaries. No circular imports.