Tree Shaking
What Is Tree Shaking in JavaScript?
Tree shaking is a build-time optimization that removes unused code from an application's final JavaScript bundle. Also called dead-code elimination, it keeps exports the build tool can prove the app uses and discards the rest. MDN's tree shaking reference explains the process for modern JavaScript modules.
How Tree Shaking Works
Tree shaking runs during a production build. Bundlers such as webpack, Rollup, Vite, and esbuild can perform it.
- The bundler starts from application entry points.
- It analyzes static ES module imports and exports to map dependencies.
- It marks reachable functions, classes, and modules as needed.
- It preserves modules with important loading side effects.
- It removes unused code and often minifies the remaining bundle.
Tree Shaking vs. Minification vs. Code Splitting
These optimizations work together but solve different problems.
| Technique | Purpose | Outcome |
|---|---|---|
| Tree shaking | Remove code that is not used | Fewer modules or exports in the bundle |
| Minification | Make retained code more compact | Smaller file size, with the same behavior |
| Code splitting | Load code only when a route or feature needs it | Multiple files and less initial loading |
Benefits of Tree Shaking
Its value depends on an application's dependencies and usage.
- Smaller bundles can reduce JavaScript downloads.
- Less code may reduce browser parsing and execution on first load.
- Specific imports encourage a focused dependency footprint.
- With code splitting, it can keep rarely used features out of the initial bundle.
Practical Limits of Tree Shaking
A bundler removes code only when it can safely do so.
- CommonJS patterns and runtime-dependent property access are harder to analyze than ES modules.
- Dynamic behavior can hide which exports an app will use.
- Barrel files can load side-effectful modules or obscure intended imports.
- Top-level work, such as registering a plugin or loading a stylesheet, may need to remain.
How to Make Code Tree-Shakable
Application and library authors can make safe removal easier.
- Prefer ES modules and import specific exports.
- Avoid unnecessary top-level work in reusable modules.
- Declare package side effects accurately.
- Offer separate entry points for large, independent features when useful.
- Inspect production bundle reports after adding major dependencies.
Checking Whether Tree Shaking Worked
Import one date-formatting utility instead of an entire library, then create a production build and inspect it with a bundle analyzer. Confirm unused exports are absent and test the app carefully. A broad side-effect setting can remove required code. This review is useful throughout app design and development.
Tree Shaking in Apps Built With Emergent
Full-stack web and mobile apps benefit from production build optimization after features are generated. When using Emergent as an AI app builder, review client dependencies and production bundles so users receive only the code their experience needs. Build leaner production apps with Emergent.
on Emergent today

