Successful compiling the Node module and “Module did not self-register.”

时间秒杀一切 提交于 2021-01-28 19:32:18

问题


I decided to write a native module for Node using VS2015 compiler and node-gyp under Windows 8.1 32bit. I worked with the instructions from this page. I searched the internet (including StackOverflow) in search of a solution to my problem.

I use following versions:

  • Node: 4.6.0
  • Node-gyp: 3.4.0

The source code of the module:

// main.c++
#include <node.h>
#include <v8.h>

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

void init(v8::Local<v8::Object> target) {
  NODE_SET_METHOD(target, "hello", Method);
}

NODE_MODULE(sapphire, init);
// binding.gyp
{
    "targets": [
        {
            "target_name": "sapphire",
            "sources": [ "main.c++" ]
        }
    ]
}

Every time compilation (node-gyp rebuild called inside addon's source code folder) succeeds. Before compilation Node-Gyp prints that compiles for node@4.6.0 | win32 | ia32. Everything looked good until that point.

For the test, I wrote the simplest possible script.

// test.js
try {
    var sapphire = require('/build/Release/sapphire');
    console.log(sapphire.hello());
} catch(err) {
    console.log(err);
}

The result was printed error Error: Module did not self-register.. I tried to replace Node and V8 with NAN (following this tutorial). But the result was the same.

During the search for a solution to my problem I came across two possible reasons:

  • Incorrect versions (libraries compared to the interpreter). Unfortunately eliminated this possibility, Node-Gyp clearly uses the library versions of the same as the interpreter, the one I use.
  • Re-download modules from node_modules. I honestly do not understand why. All the necessary modules have been updated during the installation of Node-Gyp, the rest does not matter.

What can cause this error? The source code of the module was downloaded from the tutorial from the documentation for the Node (v4.6.0). I also tried to make small changes and use all sorts of unofficial guides including of NAN. Each time the problem is the same.


回答1:


node-gyp (the tool used to build node addons) tends to make some assumptions, at least when it comes to compiling your source files. For example, it will automatically choose the "right" compiler based on the source file's extension. So if you have a '.c' file it uses the C compiler and if you have a '.cc' file (or perhaps '.cpp' file) it uses the C++ compiler.

'.c++' isn't a common file extension for C++ source files, so node-gyp may be interpreting the extension in an unexpected way (probably as a C source file). Changing the extension to something more common may help things.



来源:https://stackoverflow.com/questions/40047990/successful-compiling-the-node-module-and-module-did-not-self-register

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!