Getting started
Install
npm i -D barrelifypnpm add -D barrelifyyarn add -D barrelifyTIP
Barrelify requires Node.js 24 or later. It installs two equivalent commands, barrelify and barrel.
Opt in an index file
Barrelify never creates files. Create the barrel yourself, starting with // AUTO-BARREL as its very first characters:
// src/components/index.ts
// AUTO-BARRELAny index.ts, index.tsx, index.cts or index.mts can be a barrel. Index files without the marker are left alone, so hand-written barrels can sit next to generated ones.
Generate barrels
npx barrelifyBarrelify searches the current directory and everything below it for marked index files, and rewrites each one to re-export its sibling files, sorted by name:
src/components/
├── Button.tsx
├── button.styles.ts
├── types.d.ts
├── index.ts
└── icons/
└── ...// AUTO-BARREL
export * from './Button.js';
export * from './button.styles.js';- Only
.ts,.tsx,.ctsand.mtsfiles are exported. Declaration files (.d.ts) are skipped. - Subdirectories are not exported, unless you opt in.
- Files ignored by
.gitignore, and everything innode_modules, are skipped. - Which extensions are exported depends on the package's module system. See Module systems.
The marker is kept, so the next run rewrites the file again. Anything else you write in a barrel is replaced, except for type-only exports.
Commit the barrels, like any other source file.
Type-only exports
To re-export only the types of a file, change its line to export type *:
// AUTO-BARREL
export * from './Button.js';
export type * from './button.styles.js';Barrelify keeps the line type-only when it rewrites the file.
Subdirectories
To also re-export subdirectories, start the barrel with // AUTO-BARREL subdirectories. Each subdirectory that has its own index file is exported through it, sorted with the other files:
// AUTO-BARREL subdirectories
export * from './Button.js';
export * from './button.styles.js';
export * from './icons/index.js';- Only direct subdirectories are exported, and only through their own index file. To export deeper ones, give
icons/a// AUTO-BARREL subdirectoriesbarrel too. - The subdirectory's index file doesn't have to be a barrel, and its extension follows the same module system rules as sibling files.
- Gitignored subdirectories and
node_modulesare skipped, and a line changed toexport type *stays type-only.
Check barrels in CI
npx barrelify --ci--ci computes every barrel without writing, prints the barrels that would change, and exits with an error if any would. It is on by default when barrelify detects CI (with ci-info), so a plain barrelify in a CI script checks rather than writes.
Run barrelify before compiling, so the compiler sees up-to-date barrels. In an Nx workspace, a cached target per project does that, and checks instead of writing in CI:
// nx.json
{
"targetDefaults": {
"barrelify": {
"executor": "nx:run-commands",
"options": {
"command": "barrelify",
"cwd": "{projectRoot}"
},
"cache": true,
"inputs": ["{projectRoot}/src/**/*", "{projectRoot}/package.json"]
}
}
}Next steps
- Module systems: which files an ESM or CommonJS barrel exports, and with which extension.
- API and CLI reference: every option, and running barrelify from code.