1

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.

Maxie
  • 98
  • 3
  • 13
  • '.c++' isn't a common file extension for c++ source files. I'm wondering if changing the file extension to something like '.cc' instead might help things, as I've seen node-gyp choose a different compiler (c vs c++) based on file extension and other such things. – mscdex Oct 14 '16 at 17:31
  • @mscdex I can not believe it. You're right! I was convinced that in the end the extension does not change anything. Now it works perfectly! Rewrite it as an answer if you can. – Maxie Oct 14 '16 at 17:44

1 Answers1

4

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.

mscdex
  • 104,356
  • 15
  • 192
  • 153