Hello Rust WASM NodeJS

February 6, 2023 #rust #wasm

I am working on a project that requires calling some Rust crates from a NodeJS runtime. I had a project with some working code (which I didn’t fully understand), so I wanted to start by implementing the classic “Hello World!” problem. Let’s start at the end with the TypeScript code:

import * as wasm from "hello-wasm";


It’s “Hello WASM!” instead of “Hello World!”, but you get the point.

pub fn greet(name: &str) {
  use web_sys::console;
  console::log_1(&JsValue::from_str(&format!("Hello, {}!", name)));

Most of the Rust “magic” comes from wasm-bindgen and web_sys, but wasm-pack is what allows me to build rust-generated WebAssembly and use alongside my TypeScript.

wasm-pack build --target nodejs

Check out my “Hello World” in Rust, compiled to WASM, and called from NodeJS project on GitHub at hakanson/hello-rust-wasm-nodejs. You don’t even need to install Rust because I used Visual Studio Code Dev Containers to develop in a container

Kevin Hakanson

Multi-Cloud Certified Architect | DevSecOps | AppSec | Web Platform | Speaker | Learner | Builder
Twitter | LinkedIn | GitHub | Stack Overflow | Credly

© 2024 Kevin Hakanson (built with Gatsby)