JavaScript Runtime6h estimated

Modules & Bundling

Difficulty
Importance

The problem

As a codebase grows past one file, code needs a way to be split across files while still sharing functions and values between them in a controlled way — and browsers need that many-file codebase delivered efficiently as a small number of optimized network requests, not hundreds of individual files.

Why now

Functions and scope already established that a function's internals are hidden by default; modules apply that exact same hide-by-default, expose-deliberately principle at the level of an entire file, and bundling is the practical, network-aware step of combining many such files back into what a browser can load efficiently.

Mental model

A module is a file with its own private scope — nothing inside it is visible outside unless explicitly exported, the same encapsulation idea from functions-and-scope, just at file granularity instead of function granularity. A bundler is a build-time tool that walks the import graph (itself a dependency DAG) starting from an entry point and produces one or few optimized files for the browser to load instead of many small ones.

Requires

Helps

Unlocks

Used in

Projects

  • Split a single-file script into three modules with explicit imports/exports, then bundle them and inspect the resulting output file
  • Compare an app's bundle size before and after removing an unused import, and explain how tree-shaking achieved the reduction

Examples

  • export function add(a, b) { return a + b } in math.js is only usable elsewhere via import { add } from './math.js'
  • A bundler starting from index.js follows every import statement transitively to build the full set of files needed for one page

Resources

Mastery checklist