Re-exporting from Other Modules
What you'll learn: How to streamline imports by creating aggregation modules that re-export code from multiple sources.
As your codebase grows, you might have dozens of modules spread across folders. Instead of forcing consumers to import from many different files, you can create a single "barrel" file that gathers and re-exports everything from one convenient location.
The Core Idea
Re-exporting means importing something from one module and immediately exporting it from the current module, acting as a middleman. This lets you create a centralized entry point for related functionality.
Basic Re-export Syntax
You can combine import and export in a single statement:
// utils/index.js - barrel file
export { formatDate } from './date.js';
export { validateEmail } from './validation.js';
export { capitalize } from './strings.js';
Now consumers only need one import:
// app.js
import { formatDate, validateEmail, capitalize } from './utils/index.js';
Re-exporting Everything
Use the * syntax to re-export all named exports:
// shapes/index.js
export * from './circle.js';
export * from './rectangle.js';
export * from './triangle.js';
Renaming While Re-exporting
You can rename exports as they pass through:
// api/index.js
export { fetchUser as getUser } from './users.js';
export { fetchPost as getPost } from './posts.js';
Key Takeaway: Re-exporting lets you create aggregation or barrel files that consolidate multiple module exports into a single, convenient import point, simplifying your module structure and consumer code.