Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Module loading and code splitting

Modules form a dependency graph. Static import declares a dependency, while import() lets code request a module at runtime. Splitting code into separately loaded pieces depends on the environment and tooling, not on JavaScript syntax alone.

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

// module.mjs
export const name = value;

// main.mjs
import { name } from "./module.mjs";

async function main() {
  const namespace = await import("./module.mjs");
}
main().catch(console.error);

Module loading involves obtaining a module and its dependencies, linking imports to exports, and evaluating code. Static import declarations appear at module top level; they identify dependencies before the module body executes. Imported names are live bindings to exports, not copies of their values.

Dynamic import(specifier) is an expression also available in ordinary scripts and inside conditions or functions. It returns a Promise that normally fulfills with a module namespace object containing the exports after successful loading, linking, and evaluation. If the module exports a callable value named then, however, promise resolution calls it with resolve and reject functions. The promise's outcome then depends on that call, so it need not fulfill with the namespace object. Handle rejection with try/catch around await or with .catch(). Its argument can be computed at runtime.

Code splitting means preparing separately loadable pieces of an application. import() provides an on-demand loading point, but ECMAScript does not require separate output files or network requests. Specifier resolution and module fetching depend on the host; output-file splitting depends on tooling. Importing the same resolved module again does not rerun its code. Without a callable then export, both successful dynamic imports fulfill with the same namespace object.

Static modules were introduced in ES2015, and import() in ES2020. This is not a browser compatibility table: the language specification does not guarantee support in a particular browser, and Node 24 does not establish Web API support. In the example, main.mjs is the entry point, and both import() calls target the same local module, math.mjs. Only that module is loaded dynamically. The example does not demonstrate bundler-generated code splitting.

Examples

// math.mjs
export const double = value => value * 2;

// main.mjs
async function main() {
  const first = await import("./math.mjs");
  const second = await import("./math.mjs");
  console.log(first.double(21));
  console.log(first === second);
}
main().catch(console.error);

Result

42
true

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