#extension #napi #libnode

libnode_sys

C bindings for libnode

5 releases

Uses new Rust 2024

new 0.0.5 May 4, 2025
0.0.4 May 4, 2025
0.0.3 May 4, 2025
0.0.2 May 4, 2025
0.0.1 Apr 30, 2025

#39 in #napi

Download history 83/week @ 2025-04-26

83 downloads per month

MIT license

110KB
3K SLoC

Libnode Bindings for Rust

This crate exports Rust bindings for Node.js libnode and n-api.

This can be used by:

  • Applications embedding libnode
  • Applications creating n-api extensions

Installation (embedded)

# Install library
cargo add libnode_sys

# Download libnode, this must be distributed with your application
# The version below includes a patch with a C FFI for libnode
#
# https://github.com/alshdavid/libnode-prebuilt
#
mkdir -p /opt/libnode
curl \
  -L \
  --url https://github.com/alshdavid/libnode-prebuilt/releases/download/v22.15.0/libnode-linux-amd64.tar.xz \
  | tar -xJvf - -C /opt/libnode

Usage

Embedded

fn main() -> anyhow::Result<()> {
  // Load libnode. You must do this before using any other APIs
  libnode_sys::load::cdylib("/opt/libnode/libnode.so")?;

  unsafe {
    // Start Nodejs
    libnode_sys::node_embedding_start(/* ... */);

    Ok(())
  }
}

Embedded injecting native module

fn main() -> anyhow::Result<()> {
  // Load libnode. You must do this before using any other APIs
  libnode_sys::load::cdylib("/path/to/libnode.so")?;
 
  let nm = Box::into_raw(Box::new(libnode_sys::napi_module {
    // ... register native module
  }))

  unsafe {
    libnode_sys::napi_module_register(nm);

    // Start Nodejs
    libnode_sys::node_embedding_start(/* ... */);

    Ok(())
  }
}

How to include libnode in your application

I recommend distributing the dynamic library alongside your application and finding it at runtime using a relative path to your executable

/my-application
  my-application
  libnode.so

Dependencies

~0–4.5MB