liveBook · Manning

Import statement

As explained in The Joy of JavaScript

A JavaScript module feature for bringing exported bindings or module namespaces into the current module.

The import statement consumes functionality exposed by an ECMAScript module. It can bring in one default export, selected named exports, or a complete module namespace, supporting reusable code and separation of concerns. section 1.4 section 6.4.3

Choose an import form according to the shape of the module API:

  • Import one default export: import Block from './Block.js'; section 6.4.3
  • Import selected named exports: import { checkIndex, checkLinkage } from './block/validations.js'; section 6.4.3
  • Rename an imported binding with as, or gather named exports into a namespace with import * as ValidationsUtil. section 6.4.3
  • Load conditionally with await import(path), trading static dependency declarations for dynamic module selection. section 6.4.3

Path and binding rules

ESM relative paths begin with ./ or ../, and file extensions must be explicit when present: import Transaction from './Transaction.js';. Imported bindings remain connected to their exports as live bindings, so named imports are not destructuring copies. section 6.4.1 section 6.4.3

Listing 6.11 Path specifiers using ESM
import Transaction from './Transaction.js';  
import Transaction from '../Transaction.js';
import { curry } from '/lib/fp/combinators.js';
import { curry } from 'https://my.example.com/lib/fp/combinators.js';      #1

Compare relative, absolute, and remote ESM path specifiers in a compact listing. section 6.4.1

How The Joy of JavaScript covers it

How deep it goes
Explains default, named, aliased, namespace, package, and dynamic imports, stopping short of a complete syntax reference.
Examples
Uses Transaction, Block, validation utilities, RxJS operators, and feature-flagged wallet implementations.
What it assumes
Uses JavaScript files and module paths as context while introducing ESM import and export fundamentals.
What you can do after
Apply ESM import forms to consume default, named, namespace, package, and conditionally loaded module APIs.
How it connects
Connects ESM imports with export statements, module namespaces, live bindings, CommonJS, AMD, and package managers.

Also explained in

Compare how 13 books explain import statement →

Related concepts

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