Named, default and namespace imports
How it works
An import declaration introduces local read-only bindings to another module’s exports. Braces select named exports, a bare identifier selects the default, and * as ns creates a module namespace view. A side-effect import (import "./setup.mjs") requests evaluation without introducing a local binding. Static imports are linked before evaluation and must appear at module top level.
Example 1
Save each file separately. Run with Node.js: main.mjs. This is a multi-file example, not a single editor snippet.
values.mjs
setup.mjs
main.mjs
import "./setup.mjs";import d, { count as n } from "./values.mjs";import * as ns from "./values.mjs";console.log(d, n, ns.count, ns.default);Expected output in the tested environment:
setup9 6 6 9The setup module runs before the entry body, and both named and default exports are available on the namespace.
Common mistake
Namespace imports are not mutable copies of module exports. Assigning to an imported binding is not how you update the exporter.
Practice
Import only count under a different local name; then replace that import with a namespace import and compare the syntax.
Run this practice in the documented local environment. ByteMind module and asynchronous grader support is not yet verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Named, default and namespace imports — Knowledge Check
Reason about the concept and predict its behavior in the indicated environment.
What does import * as stats provide?
Where may a static import declaration appear?
What is introduced by import "./setup.mjs"?
Which syntax imports the named export count as n?
For Named, default and namespace imports, what is the console output of example 1 in Node.js 22.16.0?
// File: values.mjsexport const count = 6;export default 9;
// File: setup.mjsconsole.log("setup");
// File: main.mjsimport "./setup.mjs";import d, { count as n } from "./values.mjs";import * as ns from "./values.mjs";console.log(d, n, ns.count, ns.default);