Named and default ECMAScript exports
How it works
A module exposes selected bindings through export. A named export has an explicit public name; consumers can rename it at import time. Each module has at most one default export. export { local as publicName } changes the public name without renaming the local binding. Modules run in strict mode and do not automatically expose top-level bindings on the global object. Save the files separately; run the entry with Node.js as an ES module.
Example 1
Save each file separately. Run with Node.js: main.mjs. This is a multi-file example, not a single editor snippet.
math.mjs
const rate = 3;export { rate as unitRate };export default function total(n) { return n * rate; }main.mjs
import total, { unitRate } from "./math.mjs";console.log(unitRate, total(4));Expected output in the tested environment:
3 12The local rate is published under a different name; the default function is imported without braces.
Common mistake
export default is not a shortcut for exporting every local variable. A second default export is a syntax error.
Practice
Add a named taxRate export and import it under the alias tax; predict the new result before running.
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 and default ECMAScript exports — Knowledge Check
Reason about the concept and predict its behavior in the indicated environment.
Which declaration exposes a local binding under another public name?
How many default exports may a single ECMAScript module declare?
What happens to a nonexported module-local variable?
What is the appropriate file organization for the displayed example?
For Named and default ECMAScript exports, what is the console output of example 1 in Node.js 22.16.0?
// File: math.mjsconst rate = 3;export { rate as unitRate };export default function total(n) { return n * rate; }
// File: main.mjsimport total, { unitRate } from "./math.mjs";console.log(unitRate, total(4));