Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

import — importing JavaScript modules

The import keyword makes exports from other modules available. Learn static declarations, live bindings, and how import(...), import.meta, and ordinary function calls differ.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

import defaultExport from "module";
import { namedExport, otherExport as localName } from "module";
import * as namespace from "module";
import "module";
import(specifier);
import.meta

import is a reserved keyword: it cannot be a variable name. A static import declaration appears only at the top level of a module, not inside a function or block. Its module specifier must be a string literal. Static imports belong to the module system introduced in ECMAScript 2015.

A default import selects the default export; a named import selects a particular export, and as gives it a local name. The import * as namespace form creates a local binding to the module namespace object. import "module" runs the dependency without creating local bindings. Imported bindings are live: they reflect changes made by the exporting module. The importer cannot assign a new value to them; this does not mean an imported object is frozen.

import(specifier) is separate dynamic-import syntax, added in ECMAScript 2020, not an ordinary function. It can appear in expressions, including in a regular script, and returns a Promise. After successful loading and evaluation, the promise normally fulfills with the module namespace object. If the module has a callable export named then, however, promise resolution calls it. The promise can then fulfill with a different value or reject even though module evaluation succeeded. import.meta, also from ECMAScript 2020, exposes host-dependent module metadata and is allowed only in modules.

Specifier resolution and module loading depend on the host environment. Neither the language specification alone nor successful execution in Node.js establishes support in a particular browser. The example demonstrates a live binding: calling increase() changes the value observed through count.

Examples

// counter.mjs
export let count = 1;
export function increment() {
  count += 1;
}

// main.mjs
import { count, increment as increase } from "./counter.mjs";

console.log(count);
increase();
console.log(count);

Result

1
2

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources