NodeJS和N-API

时间:2017-12-06 13:50:18

标签: c++ node.js node.js-addon node.js-napi

我正在尝试编译和使用N-API Node JS c ++插件。代码编译很好,但在运行时我得到以下错误:

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)

奇怪的是,当我用正常的&#39;替换cc代码时C ++插件代码(没有N-API),一切正常。

我正在使用NodeJS 8.9.2。

有没有人有线索?

TIA, 约翰。

1 个答案:

答案 0 :(得分:0)

您缺少一些看起来像这样的代码,需要注册您在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 );