WebAssembly: Why file:// Fails
The file protocol is the address a browser shows when a page is opened straight from the disk. Loading a .wasm file that way fails, so a local server is needed instead.
Start a server with Python
Run this command in the project folder. Then open the localhost address it prints.
python3 -m http.server 8000Start a server with Node
This command serves the same folder. Use it when Node is already installed.
npx serve .Try the load and see the failure
Over file:// the fetch fails and the catch runs. The code logs only the error name.
(async () => {
try {
const response = await fetch("add.wasm");
console.log(response.ok);
} catch (error) {
console.log(error.name);
}
})();Example
(async () => {
console.log("python3 -m http.server 8000");
console.log("npx serve .");
})();Key points
- A file address starts with file:// and has no server.
- Fetch over file:// fails because the browser blocks the request.
- A local server gives the page a real web address.
- Run the server in the folder that holds the files.
- Then open the page through localhost in the browser.
In this example, the code prints the two server commands. Run one of them in the project folder, then open the page through localhost instead of opening the file directly.
Note
The exact error text varies by browser, so check the failure class, not the words.
Try it Yourself
Edit the code, then press Run.
Runs in your browser — the preview under the editor is the real result. Nothing is sent to a server. The code runner panel (right) does the same with more room.
Exercise
How do you load a .wasm file while working on your own computer?