Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: Routing and Application Architecture
Stage 5: Specialization (pick ONE)

Part 16 / Topic 3 of 9

Barrel files: the tradeoff, proven both ways

examples/03_barrel_files_tradeoffs/index.js is a barrel - one file re-exporting everything else in its folder:

// examples/03_barrel_files_tradeoffs/index.js
export { Header } from "./Header.jsx";
export { Footer } from "./Footer.jsx";

The convenience is real: import { Header, Footer } from "./folder" instead of two separate import paths, and that convenience only grows as a folder grows. barrel.test.jsx proves the honest cost directly, using a shared log.js array that Header.jsx and Footer.jsx each push their own name onto the moment their module is evaluated: importing Header.jsx directly evaluates only Header's module - the log never gets Footer's entry at all. Importing that same Header through the barrel evaluates both modules, Footer's included, even though nothing in that test ever asked for Footer. This isn't a bug in this barrel - it's how ES modules work everywhere: importing anything from a module evaluates that module in full, and a barrel re-exports the whole folder, so anything importing through it pulls in the whole folder's worth of module-evaluation side effects, not just the one export it wanted. In this toy example the only visible cost is a log entry; in a real codebase the same mechanism is exactly why a barrel can quietly drag in something that opens a connection, registers an analytics call, or (at build time) defeats a bundler's attempt to tree-shake an unused export, and it's also the shape behind a well-known class of circular-import bug: two barrels that each (even indirectly) re-export something the other one needs can deadlock the module graph in a way that a direct, non-barrel import between the same two files never would. Barrels are a convenience worth having at a feature's own public boundary (Section 5 is exactly that use) - they're a cost worth noticing before sprinkling one into every folder "for convenience" alone.

Done here? The practice set for Part 16 — including the solutions — is on the part page.

Search the guide