In the last video, Priya’s page and its script ran from your disk. You double-clicked index.html, and its Console showed the script’s message and 160. That works for one small script. Priya’s page will grow, though, so from this lesson on, its script loads the way current pages load theirs: as a module. That one change stops the script from loading when you open the page from disk.
In this lesson, you’ll see why that happens and serve your course folder from a small server on your own computer. You’ll also learn to read the address the server gives you, so you can tell at a glance how any page was opened. Start by making the change that breaks the page.
Switch the Script to a Module
Priya’s script fits in one file today, but a bigger page splits its code across several files. Modules give those files an explicit way to share. A module is a script file that names the code it uses from other script files and the code it offers them.
How files share code comes later in the path. That’s all you need to know about modules for now.
Next, you’ll see how a page asks for one. First, check that VS Code has js-course open, with scratch inside it in the Explorer panel.
With the folders in place, open scratch/index.html in your editor and change the script line near the end to this:
<script type="module" src="main.js"></script>
The new type="module" tells the browser to load main.js as a module. The src stays the same, because main.js is still in the same folder as index.html. From now on, every page in this course loads its script this way.
The change affects main.js too. Module scripts always run in strict mode, so the "use strict"; line you started main.js with no longer does anything. Delete it, so the script holds only its two console.log() calls:
scratch/main.js
console.log("Priya's game night page is loaded");
console.log(60 + 15 + 45 + 40);
Save both files. Then go back to the tab with Priya’s page and reload it, as you did in the last video. The heading still shows, but the message and 160 don’t. The Console has two red lines. Where this lesson shows ..., your Console shows the folders on your computer. The first message is one long line, wrapped here for display:
Access to script at 'file:///.../js-course/scratch/main.js' from origin
'null' has been blocked by CORS policy: Cross origin requests are only
supported for protocol schemes: chrome, chrome-untrusted, data, http, https.
Failed to load resource: net::ERR_FAILED