Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Loading a Module Only When It's Needed

The peak chart is only for the contractor’s email, so you’ll load it only on the runs that ask for it. In Python, the digest’s chart import sits inside the chart option’s condition.

A separate check file prints Digest starting, then tests the chart option. Without the option, Python’s version prints that line. The JavaScript port carries the same import declaration into the matching block:

Language: JavaScript
const args = process.argv.slice(2);
console.log("Digest starting");
if (args.includes("--chart")) {
  import { peakChart } from "#boiler/chart.js";
  console.log(peakChart([]));
}

Run it without the chart option, so the condition is false. JavaScript doesn’t print Digest starting: the file is rejected before that message can print. The ten stack frames are cut here:

Language: Program Output
  import { peakChart } from "#boiler/chart.js";
         ^
SyntaxError: Unexpected token '{'
...

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 The peak chart is only for the contractor’s email, so load it only when a run asks. In Python, the digest’s chart import sits inside the chart option’s condition.

00:12 A separate check file prints Digest starting, then tests the chart option. Without the option, Python’s version prints that line. The JavaScript port carries the same import declaration into the matching block.

00:26 Run it without the chart option, so the condition is false.

00:31 With no chart option, does JavaScript print Digest starting? Your turn: pause the video and predict.

00:44 SyntaxError. The file is rejected before that message can print. The caret points at the brace after import, and the ellipsis replaces ten stack frames.

00:55 Even a run without the chart option can’t start.

01:00 Import declarations belong at a module’s top level. Their modules load before the file’s first line runs, as in the previous lesson, where Settings loaded printed first.

01:10 So a declaration can’t wait inside an if: the parser rejects the whole file. In Python, the import runs when its branch runs. JavaScript needs a load that happens at its own line, too.

01:25 With the same port in the digest’s async function, replace only the chart declaration with an import expression. This dynamic import loads the chart when execution reaches this line.

01:38 Unlike Python’s import, its result is a promise of the loaded module. await waits for that result before the chart call runs. Without await, you’d hold a promise, and the chart function read from it would be undefined, the familiar missed-await behavior.

01:57 The finished load gives you the module’s namespace object. Its exports are its named properties. Destructuring takes the peakChart property into a local name.

02:09 Python’s from import hands you the function directly. JavaScript’s import expression gives you the object first, then you take its function.

02:17 The next line calls it with the same three peaks.

02:22 Run the digest with the chart option. The three plant reports and the fault line come first, then Peaks and the chart. Without the option, you get the same report, and the chart module never loads.

02:37 Python three fifteen also has a lazy top-level import, which waits until the name is first used. JavaScript’s nearest counterpart, the proposed import defer, needs a flag in Node twenty-six.

02:50 Keep declarations at the top; use awaited import where optional code is needed. In the next lesson, Choosing a Module by Name, you’ll select a controller module from the plant’s make.

Become a Member to join the conversation.