Search code examples
c++node.jsglfwnode.js-addonnode-addon-api

"The specified module could not be found" when using a custom nodejs addon


I am writing a nodejs addon that depends on OpenGL (glfw). It compiles successfully but when I try and use it in node I get the error The specified module could not be found.

This is the problematic part of the addon C++ code:

#include <glfw/glfw3.h>

if(glfwInit()) {
    printf("glfw init success");
}
else {
    printf("glfw init failed");
}

With this in the addon, it compiles but causes the error in node. Without this it compiles and runs without issue.

Here is my binding.gyp:

{
  "targets": [
    {
      "target_name": "engine",
      "sources": [
        "addon/addon.cc"
      ],
      "libraries": [
            "<(module_root_dir)/addon/lib/gl/glfw3dll.lib"
        ],
      "include_dirs": [
        "addon/lib",
        "<!@(node -p \"require('node-addon-api').include\")"
      ],
      'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
    }
  ]
}

And the addon file structure:

addon
  lib
    glfw
      glfw3.dll
      glfw3.h
      glfw3.lib
      glfw3dll.lib
      glfw3native.h
      opengl32.lib
  addon.cc

Edit: New binding.gyp:

{
  "targets": [
    {
      "target_name": "engine",
      "sources": [
        "addon/addon.cc"
      ],
      "libraries": [
        "-lglfw3dll",
        "-lopengl32",
        "-L<module_root_dir)/lib/glfw",
        "-Wl,-rpath,\$$ORIGIN/../../lib",
        ],
      "include_dirs": [
        "addon/lib",
        '<!@(node -p "require(\'node-addon-api\').include")'
      ],
      'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
    }
  ]
}

Solution

  • I managed to get it working with this binding.gyp file:

    {
      "targets": [
        {
          "target_name": "engine",
          "sources": [
            "addon/addon.cc"
          ],
          "libraries": [
            "legacy_stdio_definitions.lib",
            "msvcrt.lib",
            "msvcmrt.lib",
            "<(module_root_dir)/addon/lib/glfw/opengl32.lib",
            "<(module_root_dir)/addon/lib/glfw/glfw3.lib"
            ],
          "include_dirs": [
            "addon/lib",
            '<!@(node -p "require(\'node-addon-api\').include")',
          ],
          'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ]
        }
      ]
    }