Skip to main content
Saved

Pattern Fundamentals Beginner

Barrel Export

An index file can re-export all of a directory’s modules, so other code imports them from one place. That makes imports simpler, but it’s worth checking how your dev server handles barrel files, and whether your bundler’s tree-shaking, which removes exports your code never imports, still works through them.

Examples
native web
Read
4 min

The problem

Your code might have imports like import { Button } from './components/atoms/buttons/Button/Button.tsx'. Each file that uses the component now depends on your whole folder structure, so if you move one folder, you might have to update fifty import statements.

The pattern

Each module directory gets an index file at its root that re-exports the directory’s public API. Other code imports from the directory itself, as in import { Button } from './components', instead of from a deep path, so those imports don’t depend on how you organize the files inside the directory.

Problem

I’ve seen codebases where refactoring was painful because file paths were hardcoded everywhere, and developers had to memorize the directory structure to write an import. Deep relative paths like ../../../../components/Button are hard to read and break the moment you move anything.

Without clear module boundaries, other code can import implementation details you never meant to expose. A helper function you meant to keep private might end up being used in other parts of the app, and then you can’t change it without breaking that code.

Solution

That way, you can move or split files inside the directory without breaking any code outside it. Barrel files work best when they only export what other code should use, and internal helpers stay out of them. A barrel only shows which exports are public, though. Other code can still import a helper straight from its file, so if you want that blocked, you need a lint rule or a package exports field as well.

Example

The barrel below re-exports two components from deep inside a components folder, so the code that uses them only has to name the folder.

// components/atoms/buttons/Button/Button.js
export function Button(props) { /* ... */ }

// components/atoms/inputs/Input/Input.js
export function Input(props) { /* ... */ }

// components/index.js - Barrel file
export { Button } from './atoms/buttons/Button/Button';
export { Input } from './atoms/inputs/Input/Input';

// Before: each file that uses Button spells out the whole path
import { Button } from './components/atoms/buttons/Button/Button';

// After: the same file only names the directory
import { Button, Input } from './components';

If Button later moves to components/buttons/Button.js, the barrel is the only file that has to change, and the fifty files that import from ./components stay as they are.

// components/index.js
export { Button } from './buttons/Button';  // the only line that changes
export { Input } from './atoms/inputs/Input/Input';

Nested Barrel Pattern

Barrels can be nested, with a higher-level barrel re-exporting from the barrels below it, so other code can import from either level.

// components/forms/index.js
export { Input } from './Input';
export { TextArea } from './TextArea';

// components/buttons/index.js
export { PrimaryButton } from './PrimaryButton';

// components/index.js - Top-level barrel
export { Input, TextArea } from './forms';
export { PrimaryButton } from './buttons';

// Usage - Import from either level
import { TextArea } from './components/forms';
import { Input, PrimaryButton } from './components';

Namespace Imports

You can also import a barrel as a namespace, which groups its exports under a single name.

import * as Components from './components';

const button = <Components.Button label="Click me" />;
const { Button, Input } = Components;  // Can destructure

Selective Re-exporting

The barrels above name each export. The shorter export * from './forms' re-exports everything a file exports, including helpers you meant to keep internal. If you name the items instead, you decide exactly what’s in the API, and a new export in a source file doesn’t become public by accident.

// utils/dateUtils.js
export function parseDate(str) { /* ... */ }
export function formatDate(date) { /* ... */ }
export function _internalHelper(date) { /* ... */ }  // Internal

// utils/stringUtils.js
export function capitalize(str) { /* ... */ }
export function truncate(str, length) { /* ... */ }

// utils/index.js - Selective exports (prefer over export *)
export { parseDate, formatDate } from './dateUtils';
export { capitalize, truncate } from './stringUtils';
// _internalHelper is left out of the barrel

// Nothing stops another file from skipping the barrel
import { _internalHelper } from './utils/dateUtils';  // still works

To block that deep import, you can add a Linting rule such as ESLint’s no-restricted-imports, or, if the directory is its own package, list the barrel as the only entry in the exports field of its package.json.

TypeScript Type Re-exports

Barrel files can re-export TypeScript types alongside values.

// components/index.ts
export { Button, type ButtonProps } from './Button';
export { Input, type InputProps } from './Input';

// Usage
import { Button, type ButtonProps } from './components';

Benefits

  • Cleaner imports: import { Button } from './components' is easier to read than a path that’s four folders deep.
  • Easier refactoring: You can move or split files without affecting code outside the directory, as long as the barrel’s exports stay the same.
  • Explicit public APIs: The barrel shows which exports other code should use, and which ones are internal implementation details, although you need a lint rule or a package exports field to enforce that.
  • Hierarchical organization: In a large codebase, nested barrels let each feature folder expose its own small API.

Tradeoffs

  • Loading more than you use: Production bundlers such as Rollup and webpack usually tree-shake barrels well, as long as the re-exported modules have no side effects. A dev server that serves modules unbundled, or a test runner such as Jest, has to load each module a barrel re-exports, so importing one button from ./components loads every component in the folder. In a large codebase, all that extra loading makes pages slower to load in development and tests slower to start. Next.js added the optimizePackageImports option so that importing from a large library’s barrel only loads the modules you actually use.
  • Maintenance overhead: You have to update a barrel file each time you add or remove an export, and in a large codebase barrels can get unwieldy, although you can split them into smaller sub-barrels when that happens.
  • Harder source tracing: If you’re reading an import, you have to open the barrel file first to find the file the code actually comes from.
  • Circular dependency risk: Barrels that import from each other can cause bugs at runtime that are hard to track down, such as a value that is undefined because its module hasn’t finished loading yet.

Summary

A barrel export gathers a directory’s modules into one index file, so you import from that file instead of remembering exact paths. Barrels work best when they only export a module’s public API, and it’s worth keeping an eye on how much code each barrel loads in development and watching out for circular dependencies.

See also

Next in Fundamentals Semantic HTML Semantic HTML uses the right element for each part of a page, so browsers and screen readers understand its structure before you add any ARIA attributes.

A monthly email
from Den Odell.

What I'm thinking about frontend patterns, plus news when the site changes.

No spam. Unsubscribe anytime.