Skip to main content
Re-exporting in JavaScript is the process of forwarding module bindings from one module through another without importing those bindings into the current module’s local scope. It combines the import and export declarations into a single statement, allowing a module to act as a pass-through for variables, functions, or classes defined in a different file. Because re-exported bindings are never evaluated in the local scope of the forwarding module, they cannot be accessed or manipulated by the forwarding module itself.

Syntax Variations

JavaScript provides several syntactic forms for re-exporting, depending on whether you are handling named exports, default exports, or entire module namespaces.

1. Named Re-exports

You can selectively forward specific named exports from a target module. You can also alias these exports during the pass-through using the as keyword.

2. Namespace Re-exports

You can forward all named exports from a target module using the * wildcard.
Note: The export * from './moduleA.js' syntax explicitly ignores the default export of moduleA.js. It only forwards named exports.

3. Default Re-exports

Handling default exports requires explicit syntax, as the default keyword is treated as a specific named binding in the ES module system.

Technical Constraints and Behavior

  • Scope Isolation: If you write export { foo } from './moduleA.js';, the identifier foo is not declared in the current file. Attempting to console.log(foo) in the forwarding file will result in a ReferenceError.
  • Name Collisions: If you use export * from './moduleA.js' and export * from './moduleB.js', and both modules export a binding with the same name, JavaScript will not throw an error immediately. However, a SyntaxError will be thrown when another module attempts to import the conflicted binding from the forwarding module.
  • Live Bindings: Like standard exports, re-exports maintain live bindings to the original module. If the value of a re-exported variable changes in the originating module, that change is reflected wherever the re-export is consumed.
Tired of Poor JavaScript Skills? Fix That With Deep Grasping!Learn More