Home JavaScript ES Modules — Barrel Files Kill Tree-Shaking
Intermediate 4 min · March 05, 2026
Modules in JavaScript — import export

ES Modules — Barrel Files Kill Tree-Shaking

An update added 35KB to your bundle? A barrel file with a default export likely blocked tree-shaking.

N
Naren Founder & Principal Engineer

20+ years shipping production JavaScript and front-end systems at scale. Notes here come from systems that actually shipped.

Follow
Production
production tested
July 19, 2026
last updated
2,466
articles · all by Naren
Before you start⏱ 25 min
  • Solid grasp of fundamentals
  • Comfortable reading code examples
  • Basic production concepts
 ● Production Incident 🔎 Debug Guide ⚙ Triage Commands
Quick Answer
  • ESM uses export to expose and import to consume — no more global pollution
  • Named exports (export const) force explicit naming at import site, improving refactoring
  • Default exports (export default) are for the file's primary responsibility, rename without as
  • Dynamic import() returns a Promise — lazy load modules on user interaction
  • In Node.js, enable ESM with "type": "module" in package.json; browsers use
    Module System Comparison
    PropertyESMCommonJSAMDUMD
    IntroducedES2015 (2015)2009 (Node.js)2011 (RequireJS)2014 (Universal)
    Static/DynamicStaticDynamicDynamicDynamic
    Syntaximport / exportrequire / module.exportsdefine([...], function)wrapping pattern
    Tree-shakableYesNoNoNo
    Top-level awaitYesNoNoNo
    Browser nativeYes (modern)No (needs bundler)Yes (RequireJS)Yes (AMD or CJS fallback)
    Node.js nativeYes (v12+)Yes (default)NoNo
    ⚙ Quick Reference
    3 commands from this guide
    FileCommand / CodePurpose
    index.htmlThe File
    module.js'use strict'; // implicit in modulesStrict Mode
    config.jsexport const API_BASE_URL = 'https://api.example.com/v2';Export Anything

    Key takeaways

    1
    Named exports (export const X) require curly braces and support Tree Shaking better than default exports.
    2
    Default exports (export default X) are best for primary classes or components; they don't require braces.
    3
    Dynamic import() is a function that returns a Promise—ideal for code splitting and reducing initial bundle size.
    4
    ES Modules are static
    imports must be at the top level and are hoisted. CommonJS require() is dynamic and can be called inside loops.
    5
    In ESM, global variables like __dirname are replaced by import.meta.url patterns.
    6
    Set `"sideEffects"
    false` in package.json to enable tree shaking for your library.
    7
    Avoid barrel files
    they block tree shaking and introduce fragile re-export chains.
    INTERVIEW PREP · PRACTICE MODE

    Interview Questions on This Topic

    Q01SENIOR
    Explain 'Tree Shaking' and why ES Modules make it possible while CommonJ...
    Q02SENIOR
    What is the 'Static Analysis' benefit of ESM over CJS?
    Q03SENIOR
    How do you simulate `__dirname` in a Node.js ES Module environment?
    Q04SENIOR
    What happens if two modules have a circular dependency in ESM vs. Common...
    Q05SENIOR
    Implement a dynamic import loader that retries the network request 3 tim...
    Q06SENIOR
    Compare 'Default' and 'Named' exports in terms of refactorability in a l...
    Q01 of 06SENIOR

    Explain 'Tree Shaking' and why ES Modules make it possible while CommonJS makes it difficult.

    ANSWER
    Tree shaking is dead code elimination that removes unused exports from the final bundle. ES Modules enable it because their import/export statements are static — they appear at the top level and are never conditional. This allows bundlers to statically analyse which exports are actually imported. CommonJS requires() can be dynamic (called inside if-blocks, loops, or functions), so the bundler cannot safely determine which exports are used without executing the code. Therefore, CommonJS modules are included in their entirety.
    FAQ · 6 QUESTIONS

    Frequently Asked Questions

    01
    When should I use a default export vs a named export?
    02
    Can I import a CommonJS module in an ES Module file?
    03
    Why do I get 'Uncaught SyntaxError: Cannot use import statement outside a module'?
    04
    Does 'import' copy the value or create a reference?
    05
    How do I use dynamic import in Node.js without bundlers?
    06
    What is a barrel file and why is it problematic?
    N
    Naren Founder & Principal Engineer

    20+ years shipping production JavaScript and front-end systems at scale. Notes here come from systems that actually shipped.

    Follow
    Verified
    production tested
    July 19, 2026
    last updated
    2,466
    articles · all by Naren
    🔥

    That's Advanced JS. Mark it forged?

    4 min read · try the examples if you haven't

Previous
Arrow Functions in JavaScript
10 / 20 · Advanced JS
Next
Symbol and BigInt in JavaScript