emscripten + node.js

December 2021

For some reasons I couldn’t find this info easily. For debugging purposes it would be nice to be able to connect C++ code with node.js. Official emscripten documentation explains how to run code in browser. But node.js works differently. Let’s create a simple C++ file:


#include "main.h"

float lerp(float a, float b, float t) {
    return (1 - t) * a + t * b;


float lerp(float a, float b, float t);

It will not have any emscripten-related code, so I could use my IDE normally, without it knowing about any emscripten at all 🙂 Okie, let’s go:


#include <emscripten/bind.h>
#include "main.h"

using namespace emscripten;

    function("lerp", &lerp);

Now we need to compile the code into a js code:

emcc -s WASM=0 --bind -o main.js -s EXPORT_ES6 -s ENVIRONMENT=shell embindings.cpp main.cpp


  "name": "untitled",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "type": "module"


import wasmModule from './main.js';

const instance = await wasmModule();

console.log(instance.lerp(0, 1, 0.5));

Also a note about some flags:



-s O1 is the maximum optimization level allowed. -s O2, -s O3 and -s Os result in a run-time fail of module importing.

Leave a Reply

Your email address will not be published. Required fields are marked *