🛠 Set up Rust & WebAssembly in Node.js

May 19, 2020 • 3 minutes to read

There are great use cases for WebAssembly on the server-side, especially for AI, blockchain, and big data applications. You can write functions in Rust, compile into WebAssembly, and call the managed code in Node.js applications on the server. This approach combines Rust's performance, WebAssembly's security and portability, and JavaScript's ease-of-use.

Docker setup

The easiest way to set up your dev environment is to use Docker. First, clone the starter template project.

$ git clone https://github.com/second-state/wasmedge-nodejs-starter
$ cd wasmedge-nodejs-starter

Pull a Docker image with the prerequisite software stack.

$ docker pull wasmedge/appdev_x86_64

Next run the Docker image, and mount your local workspace. This way, you can run software inside the Docker container against your local files. You can also map ports from Docker to the local machine so that you can test web applications.

$ docker run -p 3000:3000 --rm -it -v $(pwd):/app wasmedge/appdev_x86_64
(docker) $ cd /app

That's it. You can now build and deploy software from the (docker) # command line. All changes to your code will be saved in your local files regardless whether you stop or delete the Docker container.

Manual setup

Of course, not everyone wants to use Docker. In the rest of the article, we will walk through setps for a manual setup in a fresh Ubuntu Server 20.04 distribution. The whole system takes 10 minutes to set up. We will install Rust, Node.js, the WasmEdge Runtime, and the rustwasmc compiler tool.

Setup the Operating System

We recommend you use the Ubuntu 20.04 LTS or later although older Linux / MacOS / Windows would probably work too. On Ubuntu Linux, use the commands below to bring your operating system up to date with the latest developer tools.

$ sudo apt-get update
$ sudo apt install -y curl wget git

WasmEdge Runtime

The easiest way to install WasmEdge is to run the following command.

$ wget -qO- https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | bash

If you would like to install WasmEdge with its Tensorflow and image processing extensions (only available on Linux at this time), please run the following command. It will attempt to install Tensorflow and image shared libraries on your system.

$ wget -qO- https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | bash -s -- -e all

Run the following command to make the installed binary available in the current session source $HOME/.wasmedge/env

Rust

Installing Rust on your Linux machine is very easy. Just two commands. At this time, we only supports up to Rust 1.50.0.

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
$ source $HOME/.cargo/env
$ rustup override set 1.50.0

For more options on installing Rust on different systems, please refer to the official Rust documentation.

Node.js

The best way to install Node.js on Linux is through the NVM tool. NVM allows you to use the latest Node.js, and change easily when you need to test your code on a different version of Node.js.

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
$ export NVM_DIR="$HOME/.nvm"
$ [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
$ [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"

$ nvm install v12.18.3
$ nvm use v12.18.3

Next, use the following commands to install it as a Node.js module.

$ npm install wasmedge-core

If you are using the Tensorflow WASI API for AI-as-a-Service applications, install WasmEdge with extensions.

$ npm install wasmedge-extensions

The rustwasmc compiler toolchain

The rustwasmc tool provides an easy command to compile Rust functions into WebAssembly and it automatically generates Node.js and WasmEdge integration code. Since you already have Node.js installed, this just requires one command.

$ npm install -g rustwasmc # Append --unsafe-perm if permission denied

What’s next?

Now you have all the tools, get started with a hello world example!

RustWebAssemblyNode.jsgetting-startedrust-function-in-nodejs
A high-performance, extensible, and hardware optimized WebAssembly Virtual Machine for automotive, cloud, AI, and blockchain applications