Skip to main content
In this tutorial, you’ll learn how to compile a simple C program into WebAssembly (WASM) using Emscripten and then execute it in a Node.js environment. The workflow mirrors browser-based builds, but outputs a JavaScript “glue” file suitable for server-side execution.

Prerequisites

  • Node.js (tested with v14.18.2)
  • Emscripten SDK with emcc available in your PATH
Before you begin, make sure you’ve installed Node.js and set up the Emscripten SDK.
Run emcc --version to verify your installation.
Check your Node.js version:

1. Write the C Source

Create a file named helloworld.c with the following contents:

2. Compile to WASM for Node.js

Use Emscripten’s emcc compiler to generate both the WebAssembly binary and the JavaScript loader:
This command produces:
FileDescription
helloworld.jsJavaScript “glue” code that locates, loads, and runs the WASM asset
helloworld.wasmCompiled WebAssembly module
The top of helloworld.js looks like this:

3. Execute the WASM Module in Node.js

Run your generated loader script with Node:
That’s all! You’ve successfully compiled a C program into WASM and executed it on the server with Node.js.

Watch Video