JavaScript Barrel Files: What They Are and Their Costs
A barrel file re-exports a folder's modules from one index file. Why barrels are convenient, how they slow builds, tests, and bundles, and the alternatives.
A barrel file is a module — usually named index.ts or index.js — whose only job is to re-export things from other modules in the same folder, so consumers can import everything from one path. Barrels make imports look tidy, but they force tools to load and process every module the barrel points to, even when you only needed one. In larger codebases that hidden cost shows up as slower dev servers, slower test runs, larger bundles, and confusing circular dependencies.
What a barrel looks like
Given a folder of utilities:
src/utils/
format-date.ts
format-currency.ts
parse-query.ts
index.ts
The barrel re-exports each one:
// src/utils/index.ts
export * from "./format-date";
export * from "./format-currency";
export * from "./parse-query";
Consumers then write:
import { formatDate } from "@/utils";
instead of:
import { formatDate } from "@/utils/format-date";
The appeal is real. Imports are shorter, the folder has a single public surface, and internal files can be reorganized without touching every importer.
Why barrels are expensive
The problem is that an import from a barrel isn’t an import of one function. In ES modules, importing a module means evaluating it, and evaluating the barrel means loading every module it re-exports — and every module those modules import.
If @/utils re-exports 40 files, and some of those import a date library, a markdown parser, and a validation library, a single import { formatDate } from "@/utils" can pull in all of them. Barrels often nest, too: @/components re-exports @/components/forms, which re-exports a dozen form components, each importing their own dependencies. One innocent-looking import fans out across a large part of the dependency graph.
Slower dev servers
Unbundled dev servers like Vite serve each module to the browser individually during development. A page that imports one helper through a barrel may trigger requests and transforms for hundreds of modules. Cold starts and page loads get slower as the project grows, and it’s rarely obvious why.
Slower tests
Test runners execute modules in a fresh environment for each test file, typically without the tree-shaking a production bundler would apply. A unit test for a small function that imports through a barrel loads the whole barrel graph, and this happens again for every test file. Teams comparing runners like Vitest and Jest often discover that barrel imports account for a large share of their test startup time.
Larger production bundles
Bundlers rely on tree shaking to drop exports nobody uses. Tree shaking works well when the bundler can prove that removing a module has no effect. But if any re-exported module has top-level side effects — registering a global, patching a prototype, running code at import time — the bundler must keep it. Without a reliable "sideEffects": false declaration in package.json, a bundler has to be conservative, and barrels give it many more modules to be conservative about.
Barrels can also undermine code splitting. If a lazily loaded route imports one component from a shared barrel, the bundler may pull related modules into a shared chunk loaded much earlier than necessary.
Circular dependencies
Barrels make cycles easy to create by accident. A module inside utils/ that imports a sibling via @/utils instead of ./sibling creates a cycle through the barrel. Cycles in ES modules can produce undefined values at import time or “cannot access before initialization” errors, depending on evaluation order, and they’re painful to debug.
Barrels vs direct imports
| Barrel imports | Direct imports | |
|---|---|---|
| Import path | Short, stable (@/utils) | Longer, specific (@/utils/format-date) |
| Modules loaded per import | Everything the barrel re-exports | Only what’s needed |
| Dev server and test startup | Slower as the barrel grows | Proportional to actual usage |
| Tree shaking | Depends on side-effect analysis | Little to shake in the first place |
| Circular dependency risk | Higher | Lower |
| Refactoring internal files | Easier, one place to update | Requires updating importers (editors automate this) |
When barrels are fine
Barrels aren’t always harmful. They’re reasonable when:
- The folder is small and its modules are lightweight with no heavy dependencies.
- It’s a published library’s entry point. A package needs a public API, and its
package.jsoncan declare"sideEffects": falseand offer subpath exports for consumers who want to import narrowly. - Everything re-exported is almost always used together, so loading it all costs nothing extra.
The trouble starts with large, deeply nested internal barrels in application code, where they’re used out of habit rather than to define a real public boundary.
What to do instead
Import directly from the source file. It’s the simplest fix and editors auto-import it for you. A path alias configured in tsconfig.json keeps direct paths short.
Use subpath exports for packages. In a monorepo or published package, the exports field in package.json can expose specific entry points like my-lib/date and my-lib/currency, giving consumers a clean API without one giant barrel.
{
"exports": {
"./date": "./dist/date.js",
"./currency": "./dist/currency.js"
},
"sideEffects": false
}
Prefer named re-exports over export *. If you keep a barrel, export { formatDate } from "./format-date" is explicit about the public surface and easier for tools to analyze than wildcard re-exports.
Enforce it with lint rules. Lint rules can forbid importing from your own barrel inside the same folder (preventing cycles) or ban barrel imports from application code entirely. Some frameworks also have build options that rewrite barrel imports from known packages into direct ones.
Measure before and after. Count how many modules a test file or dev page loads. If removing a barrel import drops that number significantly, you’ve found real overhead.
The takeaway
A barrel file is a convenience layer that re-exports a folder from one index module, and the cost is that importing one thing loads everything behind it. In large applications that slows dev servers and tests, weakens tree shaking, and invites circular dependencies. Keep barrels for small folders and genuine package entry points, import directly from source files elsewhere, and use subpath exports with a sideEffects declaration when you need a public API.
Tagged
Keep reading
The Lycoris Team · · 5 min read Concurrency vs Parallelism: The Difference Explained
Concurrency is structuring a program to handle many tasks in overlapping time; parallelism is running tasks at the same instant. How they differ and relate.
Takina · · 5 min read modulepreload Explained: Preloading JavaScript Modules
rel=modulepreload tells the browser to fetch and prepare an ES module early, flattening import waterfalls. How it differs from preload and when to use it.
Takina · · 5 min read JavaScript Custom Errors and Error Cause, Explained
JavaScript custom error classes and the Error cause option let you throw specific, chainable errors. How to define them, wrap failures, and handle them well.