Skip to content

NeonRust β†’ Native Node.js Modules

A practical SOP reference for embedding Rust in Node.js. Setup, exporting functions, async tasks, boxed data, and shipping to npm - without the C/C++ headaches.

What is this? ​

This is a self-contained operating reference for Neon - the library and toolchain for writing native Node.js addons in Rust. It is organized for getting things done:

SectionUse it when you want to...
SOPsFollow a step-by-step procedure: install, scaffold a module, export functions, ship to npm.
GuidesUnderstand a concept: primitive types, objects, arrays, async, error handling, JsBox.
WorkflowsSee an end-to-end pattern with a diagram: module load order, async data flow, the publish pipeline.
Quick ReferenceLook something up fast: CLI commands, cx methods, the JS↔Rust type table, fixes for common errors.
API MapFind the right module in the neon crate (context, types, event, result, object, thread).

Mental model

A Neon function is a normal JavaScript function whose body is written in Rust. You receive a context (cx) that is your handle to the JS runtime: you use it to read arguments, build JS values, and return results. Everything else builds on that.

The 30-second version ​

rust
use neon::prelude::*;

// A Rust function that JavaScript will see as `hello()`
fn hello(mut cx: FunctionContext) -> JsResult<JsString> {
    Ok(cx.string("hello node"))
}

// Wire it into the module's exports
#[neon::main]
fn main(mut cx: ModuleContext) -> NeonResult<()> {
    cx.export_function("hello", hello)?;
    Ok(())
}
javascript
// In JavaScript, after `npm run build`
const { hello } = require('.');
console.log(hello()); // "hello node"

Start with the Install Toolchain SOP, then build your first real module in Hello, World!.