Skip to content

Instantly share code, notes, and snippets.


surma/ Secret

Last active Jan 5, 2022
What would you like to do?
Example for a Emscripten project boilerplate
set -e
export OPTIMIZE="-Os"
echo "============================================="
echo "Compiling wasm bindings"
echo "============================================="
# Compile C/C++ code
emcc \
--bind \
-s STRICT=1 \
-s MALLOC=emmalloc \
-s EXPORT_ES6=1 \
-o ./my-module.js \
# Create output folder
mkdir -p dist
# Move artifacts
mv my-module.{js,wasm} dist
echo "============================================="
echo "Compiling wasm bindings done"
echo "============================================="
<!doctype html>
Open the console to see the output from the wasm module.
<script type="module">
import wasmModule from "./my-module.js";
const instance = wasmModule({
onRuntimeInitialized() {
#include <emscripten/bind.h>
using namespace emscripten;
int say_hello() {
printf("Hello from your wasm module\n");
return 0;
function("sayHello", &say_hello);
"name": "my-worldchanging-project",
"scripts": {
"build:emscripten": "docker run --rm -v $(pwd):/src trzeci/emscripten ./",
"build:app": "cp index.html dist/index.html",
"build": "npm run build:emscripten && npm run build:app",
"serve": "http-server -c0 dist"
"devDependencies": {
"http-server": "*"
Copy link

thgreasi commented May 27, 2019

Hi & thanks for this inspirational gist.
I would like to report that this atm isn't working for me and after an npm i and a docker pull trzeci/emscripten, when I run npm run build I get:

$ npm run build

> my-worldchanging-project@ build /home/teo/Desktop/wasm/cpp docker wasm gist
> npm run build:emscripten && npm run build:app

> my-worldchanging-project@ build:emscripten /home/teo/Desktop/wasm/cpp docker wasm gist
> docker run --rm -v $(pwd):/src trzeci/emscripten ./

/usr/local/bin/ exec: line 35: wasm: not found

Maybe something changed in the base image. Could we use a specific base image tag?

Copy link

nop-ea commented Jul 10, 2020

Hi, thanks for the great article!
I did not have any issues with the docker image but I had to change the index.html file in order to get it working.
It's basically just from the emscripten FAQ:

<!doctype html>
Open the console to see the output from the wasm module.
<script type="module">
  import wasmModule from "./my-module.js";

  // see

  wasmModule().then((module) => {


Copy link

webmaster128 commented Sep 17, 2020

I had to add --no-entry to the emcc call because there is no main function

wasm-ld: error: entry symbol not defined (pass --no-entry to suppress): main

Copy link

SuspendedPhan commented Apr 18, 2021

For Windows, this is my package.json / build:emscripten
"build:emscripten": "docker run --rm -v %cd%:/src trzeci/emscripten ./",

Also, I tried using the official emscripten/emsdk docker image and it was bunk. I got some BindingError when the runtime was trying to load the void type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment