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

Watch the Modules Load

In this video, you watched the browser load Priya’s three modules, state.js, render.js and main.js, when index.html names only main.js.

Key Points

  • The entry point leads to the rest. index.html loads main.js with <script type="module">. The browser reads the import lines in main.js and fetches state.js and render.js because main.js names them.
  • The Network panel shows who asked. It lists each file the page asks the server for, in order. With its JS filter on, its Initiator column shows main.js and the import’s line for both files. The first row there, ___vscode_livepreview_injected_script, is Live Preview’s own script, which reloads the page for you.
  • Each module is fetched once. render.js imports from state.js too, but there’s no second request.
  • Imported modules run first. Before the browser runs a module, it runs every module that module imports. Even line 1 of main.js, above its imports, runs after state.js and render.js. The module the browser fetched first, main.js, runs last.
  • Each module runs once, however many files import it. So state.js makes one players array, and render.js imports that same array. When checkIn() marks Wei’s record, render() draws from that same array. If state.js ran once per importer, there’d be two arrays, and Wei’s row would stay white.
  • One broken import stops every file. The browser fetches every module and checks every import before it runs any line. A misspelled imported name means no module runs, not even state.js, which has no mistake in it. That’s why the error is a SyntaxError, found while checking, before any code runs. The error’s link points at the import line to fix.

The Code

One log line in each file shows the order. It’s line 1 of each file, above any imports, only for this test. Imports normally go at the top of a file, so remove the log lines (or move them under the imports) when you’re done.

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 Priya’s page now lives in three files. But index.html still loads only one of them, main.js, through its script tag. So how do the other two files reach the browser?

00:12 And which file runs first? To find out, give each file one log line that says which file it is. Put it on line 1, above any imports, only for this test.

00:26 Open DevTools and pick the Network tab. This panel lists every file the page asks the server for, in the order it asks. Reload the page to fill it, then pick the JS filter.

00:38 The first row is Live Preview’s own script, which reloads the page for you. Ignore it. Next comes main.js, which the script tag in index.html asks for.

00:50 Then main.js asks for two more. Its import lines name the other two files. The Initiator column says who asked: main.js, at each import’s line.

01:02 render.js imports from state.js too, but there’s no second request. The browser fetches each module only once. That’s all you need about the Network panel for now.

01:16 That reload also ran the files, and the Console holds their lines. Before you open it, look at the code panels. main.js arrived before the other two and logs on its line 1.

01:27 Your turn: pause the video and predict which log line appears first.

01:36 Open the Console. The line from state.js comes first.

01:42 Then comes the one from render.js, and the line from main.js is last. Before the browser runs a module, it runs every module that module imports.

01:54 main.js imports from state.js first. state.js imports nothing, so it runs right away. render.js is next.

02:04 The state.js it needs has already run, so render.js runs now. Only then does main.js run its own lines, even line 1, above its imports.

02:16 The module the browser fetched first runs last.

02:21 Two files import state.js, yet its line appears only once. A module runs once, however many files import it. So state.js makes one players array, and render.js imports that same array.

02:37 If state.js ran once for each importer, there’d be two arrays. Wei would be checked in on one, the page drawn from the other, and his row would stay white.

02:48 Click Check in Wei. main.js calls checkIn(), whose code in state.js marks his record. Then render() reads the same array.

02:57 Wei’s row turns green, and the Arrived line counts two.

03:03 Now break one import on purpose. Say line 9 of main.js misspells render as renders, with an extra s. That name isn’t one render.js exports.

03:14 The import from state.js above it is still fine. Save the file. Your turn: pause the video and predict how many of the three log lines appear after a reload.

03:30 Reload. None of them. The list is empty. The Console shows one red error: render.js doesn’t provide an export with that name.

03:43 Why no lines at all, not even from state.js, which has no mistake? The browser fetched every module and checked every import before it ran any line.

03:53 So one bad name stops every file. That’s why the error is a SyntaxError. The browser finds it while checking, before any code runs.

04:04 The link beside the error points at line 9 of main.js, the import line to fix. Spell the name the way render.js exports it, save, and reload.

04:15 All three lines come back.

04:19 As a rule, the browser starts at the entry point, fetches every module it imports, and checks every import. Only then does it run anything: each module once, imported files before the file that imports them, and the entry point last.

04:35 Next, in Exercise: Move the Snacks Into a Module, you’ll give the snack table’s data a file of its own.

Become a Member to join the conversation.