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.htmlloadsmain.jswith<script type="module">. The browser reads the import lines inmain.jsand fetchesstate.jsandrender.jsbecausemain.jsnames them. - The Network panel shows who asked. It lists each file the page asks the server for, in order. With its
JSfilter on, its Initiator column showsmain.jsand 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.jsimports fromstate.jstoo, 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 afterstate.jsandrender.js. The module the browser fetched first,main.js, runs last. - Each module runs once, however many files import it. So
state.jsmakes oneplayersarray, andrender.jsimports that same array. WhencheckIn()marks Wei’s record,render()draws from that same array. Ifstate.jsran 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 aSyntaxError, 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.