Dynamic import()

Loading Modules on Demand

Example

import() loads a module when needed and returns a promise. Bundlers split such modules into separate files, so rarely-used code is not downloaded upfront:

async function showChart() {
  const { format } = await import("node:util");   // in the browser: await import("./chart.js")
  console.log(format("Loaded %s on demand", "the chart module"));
}
showChart();

Output

Loaded the chart module on demand