What Is Tree Shaking?
Tree shaking is a build-time optimization that removes unused exports from the final bundle, based on static analysis of which code is actually imported and...
Published October 7, 2026
Tree shaking is a build-time optimization that removes unused exports from the final bundle, based on static analysis of which code is actually imported and used.
Why it exists
- Libraries often export far more functionality than any single application actually uses
- Without tree shaking, an entire library would be bundled even if only one small function from it is imported
How to apply it
- Tree shaking relies on ES module syntax (import/export), since its static structure lets a bundler determine what's actually used ahead of time
- Avoid patterns that defeat tree shaking, like re-exporting an entire module as one large object when only specific named exports are needed
FAQ
Does tree shaking work with CommonJS (require)?
Not reliably — CommonJS's dynamic, runtime-based require() calls are much harder for a bundler to statically analyze compared to ES modules' static import/export syntax.