N-API pass argument within require statement

173 views Asked by At

I am creating an n-api module, is it possible to pass an argument when the require is called?

js wrapper

const NativeAPI = new (require('./../build/Release/mymodule.node')).Hello(3);
// how do i access this argument 3 inside TestClass::Init()

cpp wrapper

Napi::Object InitAll(Napi::Env env, Napi::Object exports) {
    return TestClass::Init(env, exports);
}
NODE_API_MODULE(NODE_GYP_MODULE_NAME, InitAll);

cpp class

Napi::FunctionReference TestClass::constructor;
Napi::Object TestClass::Init(Napi::Env env, Napi::Object exports) {
    // access 3 here
    Napi::HandleScope scope(env);
    Napi::Function func = DefineClass(env, "Hello", {
        InstanceMethod("create", &TestClass::create),
        InstanceMethod("delete", &TestClass::del)
    });
    constructor = Napi::Persistent(func);
    constructor.SuppressDestruct();
    exports.Set("Hello", func);
    return exports;
}
1

There are 1 answers

0
t348575 On

I understand this is not possible, instead create a constructor and pass any arguments to the constructor.