当我尝试测试nodejs N-API模块时,出现错误:
我的addon.c文件是:
#include <node_api.h>
napi_value HelloMethod (napi_env env, napi_callback_info info) {
napi_value world;
napi_create_string_utf8(env, "world", 5, &world);
return world;
}
void Init (napi_env env, napi_value exports, napi_value module, void* priv) {
napi_property_descriptor desc = { "hello", 0, HelloMethod, 0, 0, 0, napi_default, 0 };
napi_define_properties(env, exports, 1, &desc);
}
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)我的binding.gyp文件是:
{
"targets": [
{
"target_name": "addon",
"source": ["addon.c"]
}
]
}当我使用require('./build/Release/ addon ')调用addon模块时,错误信息是:
Error: Module did not self-register.
at Object.Module._extensions..node (internal/modules/cjs/loader.js:707:18)
at Module.load (internal/modules/cjs/loader.js:589:32)
at tryModuleLoad (internal/modules/cjs/loader.js:528:12)
at Function.Module._load (internal/modules/cjs/loader.js:520:3)
at Module.require (internal/modules/cjs/loader.js:626:17)
at require (internal/modules/cjs/helpers.js:20:18)有人能帮我吗?油箱
发布于 2019-03-15 15:09:36
似乎新版本的node-addon-api已经更改了模块注册/导出的API。您正在使用的数据类型也将不再有效。
它现在是这样做的
#include <napi.h>
Napi::String HelloMethod(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
return Napi::String::New(env, "world");
}
Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "hello"),
Napi::Function::New(env, HelloMethod));
return exports;
}
NODE_API_MODULE(addon, Init)这适用于节点11.10.1和node-addon-api 1.6.2。
发布于 2020-06-15 17:09:17
尝试将文件名"addon.c“更改为"addon.cpp",然后重新构建并运行。
看看这个:Successful compiling the Node module and "Module did not self-register."
https://stackoverflow.com/questions/50460250
复制相似问题