nodejs / help

:sparkles: Need help with Node.js? File an Issue here. :rocket:
1.45k stars 278 forks source link

How to use a statically linked add on within statically linked embeded node? #4255

Closed billtohara closed 1 month ago

billtohara commented 9 months ago

Details

I am using node 20.8.0. I have compiled it to a static library by passing --fully-static --enable-static to the configure script and building as normal. I'm following the embedding guide (and also a simpler version simply calling node::Start(argc, argv); from my test main program.

This works fine as documented. But I'm lost when trying to write a new add-on, with the code in the same binary, and using it from within the embedded node.

Is there a guide or example of how to achieve this? Any pointers welcome!

Node.js version

{
  node: '20.8.0',
  acorn: '8.10.0',
  ada: '2.6.0',
  ares: '1.19.1',
  base64: '0.5.0',
  brotli: '1.0.9',
  cjs_module_lexer: '1.2.2',
  cldr: '43.1',
  icu: '73.2',
  llhttp: '8.1.1',
  modules: '115',
  napi: '9',
  nghttp2: '1.56.0',
  nghttp3: '0.7.0',
  ngtcp2: '0.8.1',
  openssl: '3.0.10+quic',
  simdutf: '3.2.17',
  tz: '2023c',
  undici: '5.25.2',
  unicode: '15.0',
  uv: '1.46.0',
  uvwasi: '0.0.18',
  v8: '11.3.244.8-node.16',
  zlib: '1.2.13.1-motley'
}

Example code

// My add on
class Hello {
 public:
  static auto SayHello(const v8::FunctionCallbackInfo<v8::Value>& args)
      -> void {
    v8::Isolate* isolate = args.GetIsolate();
    args.GetReturnValue().Set(
        v8::String::NewFromUtf8(isolate, "hello",
                                v8::NewStringType::kNormal)
            .ToLocalChecked());
  }

  static auto Initialize(v8::Local<v8::Object> exports) -> void {
    NODE_SET_METHOD(exports, "sayHello", SayHello);
  }
};
NODE_MODULE_CONTEXT_AWARE(mymodule, Hello::Initialize)

where my main program is

#include "node.h"
#include "mymodule.h"
auto main(int argc, char** argv) -> int {
  // How to make `mymodule` available in the node instance?
  _register_mymodule(); // this appears to not have any effect
  int const ret = node::Start(argc, argv);
  return ret;
}
% ./build/test
Welcome to Node.js v20.8.0.
Type ".help" for more information.
> mymodule
Uncaught ReferenceError: mymodule is not defined

Operating system

macOS (14.0) ubuntu 22.04

Scope

Embedding, statically linked, statically linked add on

Module and version

Not applicable.

preveen-stack commented 9 months ago

please check https://nodejs.org/api/single-executable-applications.html

github-actions[bot] commented 2 months ago

It seems there has been no activity on this issue for a while, and it is being closed in 30 days. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.

github-actions[bot] commented 1 month ago

It seems there has been no activity on this issue for a while, and it is being closed. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.