Monday, 3 December 2018

SWIG for JavaScript: "Module did not self-register." on load

With SWIG 3.0.12 and node 8.12.0 I want to create a native module from some minimal code base:

api.h:

#pragma once
#include <string>
std::string foo();

api.cpp:

#include "api.h"
std::string foo() {
  return "hello world";
}

api.i:

%module(directors="1") api
%{
#include <api.h>
%}
%include "api.h"

to build the node-module I run:

swig -c++ -javascript -node api.i
g++ -c -fPIC api_wrap.cxx api.cpp  -I /usr/include/node -I .
g++ -shared *.o -o api.node   

.. and try to import it:

node -e "api = require('./api.node');"

But now I get

module.js:682
  return process.dlopen(module, path._makeLong(filename));
                 ^

Error: Module did not self-register.
    at Object.Module._extensions..node (module.js:682:18)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at [eval]:1:7
    at ContextifyScript.Script.runInThisContext (vm.js:50:33)
    at Object.runInThisContext (vm.js:139:38)
    at Object.<anonymous> ([eval]-wrapper:6:22)

I found lot's of questions and answers regarding similar errors but they all seem to be related to npm and incorrect versions of node modules and the node runtime.

What do I do wrong?



from SWIG for JavaScript: "Module did not self-register." on load

No comments:

Post a Comment