Skip to content

Conversation

alshdavid
Copy link

@alshdavid alshdavid commented Aug 15, 2025

This adds the ability to use napi-rs with libnode - which is the Nodejs embedding API.

Usage

// Summarized usage
fn main() {
  napi::setup("/path/to/libnode.so");

  napi::napi_module_register(napi::napi_module {
     nm_modname: "example_native_module",
     // ...
  });

  let args = vec!["-e", "console.log(process._linkedBinding('example_native_module'))"];
  // ... create c_args[]

  // Run embedded Nodejs instance
  unsafe { 
    // libnode_sys is not required - it just has bindings for the start command
    libnode_sys::node_embedding_main(c_args.len() as c_int, c_args.as_ptr())  // Prints '42'
  };
}

// Register an embedded napi module
unsafe extern "C" register(env: napi::Env, exports: napi::JsObject) -> napi::JsObject {
  exports.set("meaningOfLife", 42);
  exports
}

Copy link

graphite-app bot commented Aug 15, 2025

How to use the Graphite Merge Queue

Add the label ready-to-merge to this PR to add it to the merge queue.

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

@alshdavid alshdavid changed the base branch from main to napi2 August 15, 2025 09:43
This was referenced Aug 15, 2025
@alshdavid alshdavid changed the title Added feature libnode (v2 backport) [v2] Adding support for dynamically binding to libnode Aug 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant