NodeJS and N-API

864 Views Asked by At

I am trying to compile and use a N-API Node JS c++ addon. The code compiles fine but at runtime I get the following error:

module.js:664
  return process.dlopen(module, path._makeLong(filename));
             ^

Error: Module did not self-register.
at Object.Module._extensions..node (module.js:664:18)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Module.require (module.js:579:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/Users/johngorter/Desktop/WASM/index2.js:1:77)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)

The strange thing is that when I replace the cc code with 'normal' C++ addon code (without N-API), everything works fine.

I am working with NodeJS 8.9.2.

Does anyone have a clue?

TIA, John.

1

There are 1 best solutions below

0
On

You are missing some code that looks like this, which needs to register the functions that you are making available in JavsScript.

    Napi::Object Init( Napi::Env env, Napi::Object exports )
    {
      exports.Set(
        Napi::String::New( env, "someFunctionName"),
        Napi::Function::New( env, someFunctionName)
      );

      exports.Set(
        Napi::String::New( env, "someOtherFunctionName"),
        Napi::Function::New( env, someOtherFunctionName)
      );

      return exports;
    }


    NODE_API_MODULE( yourModuleName, Init );