Debug a bundle that stays fat after importing one export: CommonJS dependencies, a missing sideEffects flag, barrel re-exports, and namespace imports all block elimination. Shows how to verify with a bundle analyzer and which fix applies to each cause.
Frontend interview practice question
Why didn't tree shaking remove my unused import? sideEffects and barrels
Interview quick answer
Interview focus
This JavaScript interview question tests whether you can explain Tree Shaking Kept Your Unused Import: Fixes, connect it to production trade-offs, and handle common follow-up questions.
- Tree Shaking Kept Your Unused Import: Fixes explanation without falling back to memorized definitions
- Modules and Build reasoning, edge cases, and production failure modes
- How you would answer the most likely JavaScript interview follow-up
Use this JavaScript interview question to rehearse a quick answer, common mistake, follow-up, and production pitfall.
Full interview answer
The bug to explain
You import one icon from a library and the whole icon set lands in the bundle. Tree shaking did not fail at random. One of four things blocked it: the package ships CommonJS instead of ESM, its package.json does not declare sideEffects: false, a barrel file re-exports everything so the bundler must keep the module graph, or the code uses a namespace import with property access the bundler cannot prove static. Each has a specific fix below.
Bundler pitfall
Tree-shaking is not a magic "remove everything unused" switch. Bundlers need static ESM structure and trustworthy side-effect signals. When bundles stay fat, the debug question is usually where hidden side effects, barrel exports, or import shape prevented elimination.
Requirement | Why it matters | Example |
|---|---|---|
ESM exports | Static analysis of imports | export const foo = ... |
No side effects | Unused code can be safely removed | set "sideEffects": false |
Pure annotations | Signals safe removal | /* @__PURE__ */ helper() |
// Good: named ESM exports
export const add = (a, b) => a + b;
export const multiply = (a, b) => a * b;
// Consumer uses only add
import { add } from './math.js';
Pitfall | Why it breaks tree-shaking | Fix |
|---|---|---|
CommonJS modules | Require is dynamic | Publish ESM build |
Side-effect imports | Bundler must keep them | Mark sideEffects accurately |
Barrel files with side effects | Re-export triggers file execution | Keep barrels side-effect free |
Dynamic require/import | Unknown at build time | Prefer static imports |
How to verify
Use a bundle analyzer to compare sizes before/after. In devtools, inspect the output bundle to ensure unused exports are removed. Add perf budgets to prevent regressions.
Summary
Tree-shaking is effective when your code is ESM, side-effect free, and statically analyzable. If bundles stay large, check for CJS dependencies and unmarked side effects.
Use this as one explanation rep, then continue with the JavaScript interview questions cluster or a guided prep path.
Built in public
The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.