JavaScript Modules Export
Module Namespace Export
Module namespace export refers to exporting an entire imported module namespace object from another module.
Namespace Export
Normally, you import all of a module's exports into a module namespace object like this:
Example
import * as math from "./math_module.js";
let myPi = math.PI;
let result = math.add(2, 3);
The math module namespace object bundles all the exports (PI, add, multiply):
// math_module.js
export const PI = 3.14159;
export function add(a, b) {
return a + b;
}
export function multiply(a, b) {
return a * b;
}
With module namespace exports, you can re-export that entire namespace from your own module - without having to import it first:
Now, you can create a new module that re-exports this whole namespace:utilities.js
export * from "./math_module.js";
Note
The syntax export * as name from "module" was added in ECMAScript 2020.
Syntax Overview
| Syntax | Result |
|---|---|
| export * from "module" | Re-export all named exports individually (not as a namespace) |
| export * as name from "module" | Re-export all exports grouped under a single namespace |
| export { name } from "module" | Re-export specific exports |