JavaScript Modules Namespace
Module Namespace Support
In JavaScript, module namespace support refers to the mechanism that allows you to import all exports from a module as a single, unified object (the module namespace object).
Note
The Module Namespace feature is a part of the ES Modules (ESM) introduced in ECMAScript 2015 (ES6) to help organize and access module contents in a consistent way.
The Module Namespace Object
When you use the import * as name from "module" syntax, JavaScript creates a module namespace object - an immutable object that contains all the exported bindings from that module.
Example
// mathUtils.js
export const PI = 3.14159;
export function add(a, b) {
return a + b;
}
export function multiply(a, b) {
return a * b;
}
You can import everything from that file into a namespace:
// main.js
import * as math from "./mathUtils.js";
console.log(math.PI); // 3.14159
console.log(math.add(2, 3)); // 5
Here, math is a module namespace object that bundles together all the exports (PI, add, multiply).
| Property | Description |
|---|---|
| Read Only | You can access exports but not reassign them. |
| Live Binding | If an exported value changes in the module, the namespace reflects that change automatically. |
| No Prototype | It is a plain object with no prototype (Object.getPrototypeOf(math) === null). |
| Enumerable | Its properties are enumerable and correspond to named exports. |
| Not Callable | It is not a function or a class. |
Before ES Modules
// In mathUtils.js
export let counter = 0;
export function increment() {
counter++;
}
// In main.js
import * as math from "./mathUtils.js";
math.increment();
console.log(math.counter); // 1 (live binding - reflects change)
Now, ES Modules and namespace imports make this standardized and native:
// main.js
import * as math from "./mathUtils.js";
console.log(math.PI); // 3.14159
console.log(math.add(2, 3)); // 5
Related Syntax
import { add } from "./mathUtils.js";
import math from "./mathUtils.js";
import * as math from "./mathUtils.js";
Summary
| Concept | Description |
|---|---|
| Module Namespace | A special object containing all named exports from a module. |
| Created By | import * as name from "module" |
| Purpose | Organize, access, and reference module exports safely. |
| Immutable | You cannot modify or reassign its properties. |
| Live Bindings | Reflects all changes in the original module. |