Sub topic 4
Importing: Named, Default, Namespace, and Renamed Imports
This chapter assumes you know object destructuring and have read the earlier chapters on named and default exports. The examples use math.js from the Named Exports chapter. That file exports PI, area, and perimeter.
The Four Import Forms
An import declaration names the bindings you want and the module they come from. Each form below matches one way a module can expose values.
| Form | Syntax | What you get |
|---|---|---|
| Named | `import { area } from './math.js'` | The export called `area` |
| Default | `import log from './logger.js'` | The default export, under any local name you pick |
| Namespace | `import * as math from './math.js'` | An object holding every named export |
| Renamed | `import { area as circleArea } from './math.js'` | The export `area`, bound locally as `circleArea` |
You can combine a default import with named or namespace imports in one statement, as long as the default comes first.
// logger.js
export default function log(message) {
console.log(`[app] ${message}`);
}
export const level = 'info';
// main.js
import log, { level } from './logger.js';
import * as math from './math.js';
import { area as circleArea } from './math.js';
log(`level=${level}`);
console.log(math.PI, circleArea(2));Importing from ./math.js twice does not load the file twice. Both declarations refer to the same module instance.
Braces Are Not Destructuring
{ level } looks like destructuring, but it is different syntax. Renaming uses as, not a colon. You also cannot write default values or nested patterns inside the braces. Inside the braces, the name must match an export name exactly.
Braces decide which export you get. import log from asks for the default export. import { log } from asks for a named export called log. If the module has no export named log, linking fails before any code in the module graph runs:
SyntaxError: The requested module './logger.js' does not provide an export named 'log'The Namespace Object
import * as math creates a module namespace object. It has one property per named export. If the module has a default export, the namespace exposes it as the property default. The namespace object is sealed and its properties are read-only, so math.PI = 3 throws a TypeError in module code.
Rules That Catch People
- Import declarations may only appear at the top level of a module. They cannot go inside a function, an
ifblock, or atryblock. - The specifier must be a string literal.
import x from someVariableis a syntax error. - Imports are hoisted, so imported bindings are available throughout the module, even in lines above the declaration.
- An imported binding is read-only. Assigning to it throws a TypeError.
Exercise
A module util.js contains export default function format() {} and export const sep = '/';. Write one import statement that binds the default export as fmt and binds sep as separator.
Show answer
import fmt, { sep as separator } from './util.js';
Exercise
Given import * as util from './util.js' for the module above, how do you call the default export, and what happens if you run util.sep = '\\'?
Show answer
Call it with util.default(). Assigning to util.sep throws a TypeError, because namespace object properties are read-only and module code always runs in strict mode.