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 digest can load its chart only for runs that request --chart. In Python, an ordinary import can sit inside the condition. A JavaScript import declaration inside that block causes a syntax error before the file starts, even when the condition would be false.

Keep declarations at the module’s top level. Use the import() expression where optional code is needed. It returns a promise for the module’s namespace object; await supplies that object, and destructuring selects its named chart function. Without await, reading the function from the promise gives undefined.

Keep all earlier imports, the setting’s setter call, the plant loop, the collected peaks, the fault report, and the entry-file guard. Inside the existing async main(args), replace only the optional chart block:

Language: JavaScript
if (args.includes("--chart")) {
  const { peakChart } = await import("#boiler/chart.js");
  console.log(peakChart(peaks));
}

Run node digest.js logs/week-12 --chart for the ordinary three-plant report and the complete weekly chart. Without --chart, the report stays the same and the chart module isn’t loaded. The existing --verbose option still reports the science log’s skipped line on standard error and can be combined with --chart.

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 You’ll load the weekly chart only for the contractor’s email. Dev’s digest already reports all three plants. Its verbose option still reports the science sensor dropout.

00:10 Only an email run needs the extra picture. In Python, the chart import sits inside the option’s condition. The existing peaks hold the main boiler, sports hall, and science block results.

00:22 Keep that whole report; change only when its chart code loads.

00:32 For a focused check, use a separate check file. In Python, it prints a starting message, then checks the chart option. JavaScript’s argument array comes from the command line; its inclusion test asks the same question.

00:46 Carry Python’s import into the matching block in the JavaScript port.

00:56 A named import still requests the chart function. Run the check without the chart option, so the condition would be false. Keep the working digest untouched.

01:07 With no chart option, does JavaScript print Digest starting? Your turn: pause the video and predict whether that first message appears.

01:22 SyntaxError. The declaration is rejected before that message can print. The brace marks where this syntax fails. The ellipsis replaces ten stack frames.

01:33 Even a run without the chart option cannot start. JavaScript import declarations belong at a module’s top level. Their dependencies load before the module body starts, as the setting example showed.

01:48 A false condition cannot protect invalid syntax inside that body. In Python, the conditional import executes when that branch runs. The JavaScript port needs a load that executes there too.

02:01 Keep normal imports at the top; make this optional load happen at its own line. Back in the working digest’s async function, replace only the chart declaration with an import expression.

02:20 The call loads the chart when execution reaches this line. Its result is a promise, representing completion of the load. await waits for that result before the following chart call.

02:34 Without await, you’d have a promise, and reading the chart function from it would give undefined, the familiar missed-await slip.

02:41 Keep this existing function async.

02:46 The completed load gives you a module namespace object. That object exposes the module’s exports as named properties. The familiar destructuring takes its peakChart property into a local name.

03:04 In Python, the conditional from import supplies the function directly. In JavaScript, await supplies the object first, then you select its function.

03:15 The next line calls that function with the same three peaks. Keep both steps visible.

03:28 Run the corrected digest with the chart option. The familiar plant reports and fault line remain. Then Peaks appears, followed by the chart for all three plants.

03:38 Run it without that option; you get the report, with no chart module loaded. The verbose option still reports the science dropout on standard error. It can accompany the chart option.

03:52 The chart change hasn’t replaced the plant data, the setting, or the existing imports. Keep this edit inside its original branch.

04:02 Python 3.15 also offers a lazy top-level import, deferred until the imported name is used. JavaScript’s proposed import defer is not available here without a flag.

04:13 That’s a different mechanism; the conditional call is the one to use for this digest. Keep declarations at the top; use awaited import where optional code is needed.

04:24 Your chart stays inside its condition. 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.