> ## 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.

# Demo WASM in Serverside with NodeJS

> This tutorial teaches how to compile a C program into WebAssembly and execute it in a Node.js environment.

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`

<Callout icon="lightbulb" color="#1CB2FE">
  Before you begin, make sure you’ve [installed Node.js](https://nodejs.org/) and set up the [Emscripten SDK](https://emscripten.org/docs/getting_started/downloads.html).\
  Run `emcc --version` to verify your installation.
</Callout>

Check your Node.js version:

```bash theme={null}
node -v
# Expected output:
# v14.18.2
```

## 1. Write the C Source

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

```c theme={null}
#include <stdio.h>

int main() {
    printf("Hello World\n");
    return 0;
}
```

## 2. Compile to WASM for Node.js

Use Emscripten’s `emcc` compiler to generate both the WebAssembly binary and the JavaScript loader:

```bash theme={null}
emcc /path/to/helloworld.c -o /path/to/helloworld.js
```

This command produces:

| File            | Description                                                         |
| --------------- | ------------------------------------------------------------------- |
| helloworld.js   | JavaScript “glue” code that locates, loads, and runs the WASM asset |
| helloworld.wasm | Compiled WebAssembly module                                         |

The top of `helloworld.js` looks like this:

```javascript theme={null}
var Module = typeof Module != 'undefined' ? Module : {};
// …glue code to locate and instantiate helloworld.wasm…
```

## 3. Execute the WASM Module in Node.js

Run your generated loader script with Node:

```bash theme={null}
node /path/to/helloworld.js
# Output:
# Hello World
```

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

## Links and References

* [Node.js Official Website](https://nodejs.org/)
* [Emscripten Documentation](https://emscripten.org/docs/)
* [WebAssembly Developer Guide](https://webassembly.org/getting-started/developers-guide/)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/exploring-webassembly-wasm/module/a9d35579-0f55-465c-8d70-eec38ff7c750/lesson/8c34bfa6-87bb-44e3-8809-8e80cfc69dc3" />
</CardGroup>
