Lesson 12 of 30
A build tool combines dozens of source files into a handful of optimized bundles — and tree shaking is specifically the step that deletes exports nobody actually imports, before that bundle ever ships.
13 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 2
Why does tree shaking rely on code being written as ES Modules rather than CommonJS?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
Bundling takes your app's real module graph — dozens or hundreds of separate files importing each other — and combines it into a small number of files optimized for the browser to download and parse, instead of making a separate network request per source file.
Tree shaking (dead-code elimination, specifically for ES Modules) is a bundling step that removes exports nothing in your app actually imports:
// utils.js
export function used() { return 1; }
export function neverImported() { return 2; } // dropped entirely from the bundle
// app.js
import { used } from "./utils.js";
If nothing imports neverImported, a tree-shaking-capable bundler removes it from the final output entirely — the bundle only contains code that's actually reachable from your app's real entry point.
Ten separate files, ten separate network requests — slow, especially over older HTTP.
"sideEffects": false in package.json.import { debounce } from "big-lib" can still pull in the whole library."Why does your app's bundle include code you're pretty sure you're not using?" is a real, practical debugging question, and understanding tree shaking's actual requirements (ESM, no unexpected side effects, a tree-shakeable library build) is what lets you diagnose it instead of just re-running the build and hoping.