5

I was attempting to make a node native addon and node-gyp configure build worked fine, when i required the test addon in a javascript file and ran it normally with node main it then gives the error Error: Module did not self register, code: ERR_DLOPEN_FAILED here is my code so far:

binding.gyp

{
    "targets": [
        {
            "target_name": "hello",
            "source": ["hello.cc"]
        }
    ]
}

hello.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);

main.js

const hello = require("./build/Release/hello");
console.log(hello.hello())

How do i fix this error? Thanks.

Arsha
  • 53
  • 1
  • 1
  • 4

1 Answers1

1

You can check what version of node you are using nvm ls (to list out the node versions)

try changing the node version to check and run the command again nvm use version_number (to change the version)

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • This actually worked. This issue was coming when I was using Node v17 on a project which I started in Node v14. Just changing my local node version using nvm worked for me. – Avikalp Gupta Mar 25 '22 at 08:43