🛠 Set up Rust & WebAssembly in Node.js

• 2 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.

The whole system takes 10 minutes to set up on Linux. In this tutorial, we will talk you through installing Rust, Node.js, the Second State WebAssembly VM (SSVM), and the ssvmup compiler tool. The commands here are for Ubuntu Linux. Instructions for other OSes are in the links.

Rust

Installing Rust on your Linux machine is very easy. Just two commands.

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

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
$ [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
$ [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"

$ nvm install v10.19.0
$ nvm use v10.19.0

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

Second State VM

The Second State Virtual Machine (SSVM) is an open source WebAssembly runtime optimized for server-side applications. Use the following commands to install it as a Node.js module. The apt commands install the native libraries SSVM depends on.

$ sudo apt-get update
$ sudo apt-get -y upgrade
$ sudo apt install build-essential curl wget git vim libboost-all-dev

$ npm install ssvm

The ssvmup compiler toolchain

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

$ npm install -g ssvmup # 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
Fast, safe, portable and serverless Rust functions as services