ERR_DLOPEN_FAILED 当尝试制作节点本机插件时

Ars*_*sha 5 c++ node.js node-gyp

我试图制作一个节点本机插件并且node-gyp configure build工作正常,当我需要在 javascript 文件中测试插件并使用它正常运行node main它然后给出错误时Error: Module did not self register, code: ERR_DLOPEN_FAILED,这里是我到目前为止的代码:

绑定.gyp

{
    "targets": [
        {
            "target_name": "hello",
            "source": ["hello.cc"]
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

你好.cc

#include <node/node.h>
#include <node/v8.h>

using namespace v8;

void Method(const FunctionCallbackInfo<Value>&args) {
  Isolate* isolate = args.GetIsolate();
  args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world").ToLocalChecked());
}

void Initialize(Local<Object> exports) {
  NODE_SET_METHOD(exports, "hello", Method);
}

NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize);
Run Code Online (Sandbox Code Playgroud)

main.js

const hello = require("./build/Release/hello");
console.log(hello.hello())
Run Code Online (Sandbox Code Playgroud)

我该如何修复这个错误?谢谢。

小智 1

您可以检查您使用的节点版本 nvm ls (列出节点版本)

尝试更改节点版本以检查并再次运行命令 nvm use version_number (更改版本)