0

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 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.