liveBook · Manning

Import statement

As explained in Secrets of the JavaScript Ninja, Third Edition

The import keyword statically declares dependencies on exported bindings from other ECMAScript modules.

An import statement declares an ECMAScript module’s dependency on another module. Variables are private to a module unless exported; an import makes those exports available under local names. Unlike `import()`, which runs asynchronously at runtime, a static import is handled when the module is parsed. section 13.1.1 section 13.1.2

Import only the names you need

If a module exports `homeIsland` and `getOccupation`, write `import { homeIsland, getOccupation as getTitle } from "./1-export-syntax.ts";`. The braces resemble object destructuring, but this is module syntax. `as` gives the imported binding a local name—useful when two modules export the same name. section 13.1.1

Choose the form that matches the dependency:

  • `import hero, { tsundere } from "./4-default-export.ts";` combines a default import with a named import. The default export can also be named explicitly with `default as hero`. section 13.1.1
  • `import * as characters from "./4-default-export.ts";` gathers exports into a namespace; its default export is accessed as `characters.default`. section 13.1.1
  • `import "./scripts/ninja.js";` executes a module without importing any names. This is a side-effects import. section 13.1.1 section 13.1.2

Imports are live bindings, not snapshots: if the exporting module reassigns an exported value, an importer reads the new value. The importer cannot reassign that binding itself. An imported dice-roll result, for example, changes after calling the exported roll function. section 13.1.1 section 13.4

The specifier after `from` must resolve in the environment running the code. Node.js package `exports` can map a package name or subpath to files; a browser import map can map a bare name to a URL. In Node.js, `.mjs` identifies ESM, while a package’s `"type": "module"` can mark its files as ESM. When importing a `.ts` path directly in the shown TypeScript setup, type-checking requires `allowImportingTsExtensions` with `noEmit`. section 13.1.1 section 3.1.2 section 13.1.2

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Works through named, default, namespace, and side-effects imports, then contrasts static parsing with runtime `import()`.
Examples
Uses named and default imports, a changing dice-roll value, and a Node.js calculator importing built-in APIs.
What it assumes
Assumes familiarity with variables and functions while introducing module exports and imports through examples.
What you can do after
Apply it via Node.js ESM to import exported bindings, rename conflicts, and read updated exported values.
How it connects
Connects imports to exports, live bindings, package entry points, import maps, and dynamic import.

Also explained in

Compare how 14 books explain import statement →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.