In this guide, you’ll learn how to write a simple WebAssembly Text Format (WAT) module, compile it into a WASM binary using the WebAssembly Binary Toolkit (WABT), and load it in an HTML page. By the end, you’ll see “Hello, WebAssembly!” printed in your browser console.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Prerequisites
- A modern web browser
- A local HTTP server (e.g., VS Code Live Server,
python -m http.server,http-server) - Node.js or another environment to run a local server
You cannot load a WASM module via
file://. Always serve your files over HTTP or HTTPS.1. Write the WAT Module
Create a file namedhello-wasm.wat:
- Imports the JavaScript
console.logfunction - Allocates 1 page (64 KiB) of memory
- Stores the null-terminated string
"Hello, WebAssembly!"at offset 0 - Defines a
mainfunction that pushes the string offset and callslog
2. Install the WebAssembly Binary Toolkit (WABT)
WABT provides thewat2wasm compiler. Install it on your platform:
| Platform | Install Command |
|---|---|
| macOS | brew install wabt |
| Windows | choco install wabt |
| Linux | sudo apt-get install wabt (Debian/Ubuntu) |
For other distributions, visit the WABT GitHub releases page.
3. Compile WAT to WASM
Run the following command in the directory containinghello-wasm.wat:
hello-wasm.wasm in your working folder.
4. Create the HTML Loader
Make anhello-wasm.html file alongside your .wasm binary:
- Fetches the
.wasmbinary - Instantiates it with an imported
console.logfunction - Retrieves the exported memory and calls
main()
5. Run and Verify
- Start your local HTTP server in the project folder.
- Open
http://localhost:PORT/hello-wasm.htmlin your browser. - Check the developer console. You should see: