"Module did not self-register" when requiring() Node Addon


#1

I wrote a basic “hello world” Node Addon that works in stand-alone Node but not under Electron.
The generated error is “Module did not self-register”.

The addon code:

#include <node.h>

void HelloWorld(const v8::FunctionCallbackInfov8::Value& args)
{
v8::Isolate* isolate = args.GetIsolate();
args.GetReturnValue().Set(v8::String::NewFromUtf8(isolate, “world”));
}

extern “C”
{
void init(v8::Handlev8::Object target)
{
NODE_SET_METHOD(target, “Hello world”, HelloWorld);
}
NODE_MODULE(SkatterImporter, init);
}

Looking at the Node Addon Instructions, it seems that one must take care to build the addon with the same Node version as Electron’s (7.9.0 in my case).

So I tried different ways to build the addon.

1. Direct build with Visual Studio

I use local Node’s headers (downloaded 7.9.0, same as Electron’s) and link against the corresponding node.lib that I built from sources.

The generated addon works in Node but it does not work in Electron.

2. Build with node-gyp

Similar to the “Manually building for Electron” part of the documentation.

> node-gyp rebuild --dist-url=https://atom.io/download/atom-shell

Works in Node, does not work in Electron.

3. Build with electron-rebuild

Similar to the “Installing modules and rebuilding for Electron](https://github.com/electron/electron/blob/master/docs/tutorial/using-native-node-modules.md#installing-modules-and-rebuilding-for-electron)” part of the documentation.

> .\node_modules\.bin\electron-rebuild.cmd -f --version 1.7.9 --module-dir .

I get another error when requiring() (even in stand-alone Node): Error: The specified module could not be found.
Maybe there’s something wrong with my command?

Anyway, I was under that the first two methods should work.
Am I missing something?

Thanks.


#2

Did you compile the native addon with x64 flag?
it is possible that you compiled in x86, then it works on your regular node.js standalone environment.

However, Electron runtime is x64 by default.


#3

Yes, I compiled in 64-bits.