Sub topic 2
Named Exports
This chapter assumes you know core JavaScript syntax, the difference between let, const, and var, and how to run a file in a browser or Node.js. It also assumes you have read the previous chapter on module scope.
A named export makes a binding from module scope visible to other modules under a specific name. A module can have any number of named exports. Each name must be unique within that module.
Two ways to declare named exports
You can put export in front of a declaration. You can also list existing bindings in an export list. Both forms work together in the same file.
// math.js
export const PI = 3.14159;
export function area(radius) {
return PI * radius * radius;
}
function circumference(radius) {
return 2 * PI * radius;
}
export { circumference as perimeter };This module has three public names: PI, area, and perimeter. Inside math.js, the function is still called circumference. The as keyword changes only the name that consumers see.
| Form | Example | Use it when |
|---|---|---|
| Inline declaration | `export const PI = 3.14159;` | The export is obvious at the definition |
| Export list | `export { a, b };` | You want the public API in one place |
| Renamed export | `export { circumference as perimeter };` | The internal name differs from the public one |
An export list can appear anywhere at the top level of the module, even before the bindings it names are declared. Many teams put a single export list at the bottom of the file so the module's whole API reads in one block.
What a consumer sees
Importers ask for exports by their exported names, inside braces. The next chapter on imports covers this in detail.
// main.js
import { PI, area, perimeter } from './math.js';
console.log(PI); // 3.14159
console.log(area(2)); // 12.56636
console.log(perimeter(1)); // 6.28318If main.js asked for circumference, loading would fail before any code ran, because math.js exports no binding by that name.
Common errors
Named exports have a few rules that produce SyntaxErrors at parse time:
- Exporting a bare value.
export 42;andexport { 1 + 1 };are invalid. An export needs a name, either from a declaration or from an existing binding. - Exporting an undeclared name.
export { total };fails if the module never declarestotal. - Duplicate export names.
export { area }; export { perimeter as area };fails becauseareais exported twice. - Exporting from a nested scope.
exportis allowed only at the top level, so it cannot go inside anifblock or a function.
Because these checks happen when the module is parsed, a mistake in an export list stops the program before any code runs. The bug does not stay hidden until some later function call.
Exercise
A module contains const rate = 0.2; function tax(x) { return x * rate; } followed by export { tax as computeTax, rate };. Which names can another module import, and is the name tax among them?
Show answer
The importable names are computeTax and rate. The name tax exists only inside the module, so importing { tax } fails with a SyntaxError during linking.
Exercise
Why is if (debug) { export const level = 'verbose'; } rejected?
Show answer
Export declarations must appear at the top level of a module. That keeps the set of exports static, so it is known from parsing alone without running any code.