Node.js Hello World!

This example, walks through how to compile a hello world executable written in C++ to JavaScript and execute it with the Node.js runtime!

Before getting started, make sure Node.js and Docker are installed. On Linux, make sure you can run docker without sudo. On Windows, make sure Shared Drives are enabled in the Docker settings. On Windows, also install Git Bash, and run shell commands in Git Bash.

First, let’s create a new folder to house our project.

mkdir NodeHelloWorld
cd NodeHelloWorld

Initialize the Node package.json file:

npm init --yes

Add itk and fs-extra to your project’s dependencies:

npm install --save itk fs-extra

Let’s write some code! Populate hello.cxx with our Hello World program:

#include <iostream>

int main() {
std::cout << "Hello world!" << std::endl;
return 0;
}

Next, provide a CMake build configuration at CMakeLists.txt:

cmake_minimum_required(VERSION 3.10)
project(HelloWorld)

add_executable(hello hello.cxx)

We use the add_executable command to build executables with itk.js. The Emscripten toolchain along with itk.js build and execution configurations are contained in the itk.js dockcross Docker image used by the itk-js command line interface (CLI).

Note that the same code can also be built and tested with native operating system build tools. This is useful for development and debugging.

Next, build the program with the itk.js CLI, itk-js. This is shipped with the itk package, and the CLI can be executed from the local node_modules folder with npx. The itk-js CLI will invoke the toolchain contained in the dockcross Docker image. Pass the local source code directory root into itk-js build to perform the build.

npx itk-js build .

The project is built in ./web-build.

To execute the project, create an index.js file to invoke the module:

const path = require('path')
const runPipelineNode = require('itk/runPipelineNode')

const pipelinePath = path.resolve(__dirname, 'web-build', 'hello')
runPipelineNode(pipelinePath)

And run it!

npx node ./index.js

Congratulations! You just executed a C++ program compiled to JavaScript. 🎉