c++node.jsnode-gypnode-addon-api

Cannot import a .node addon module with require


I am developing a nodejs addon using node-addon-api and node-gyp 9.3.1. However, when I try to import using require it gives me the following error:

Uncaught Error: The specified module could not be found.
\\?\C:\Users\simon\Documents\draw2d\build\Release\canvas_sdl2.node
    at Module._extensions..node (node:internal/modules/cjs/loader:1353:18)
    at Module.load (node:internal/modules/cjs/loader:1125:32)
    at Module._load (node:internal/modules/cjs/loader:965:12)
    at Module.require (node:internal/modules/cjs/loader:1149:19)
    at require (node:internal/modules/helpers:121:18) {
  code: 'ERR_DLOPEN_FAILED'

I am using NodeJS v20.0.0 but I've also tried with older versions like v18.16.0. The addon is the following:

#include <napi.h>
#include <SDL.h>
#include <string>

Napi::Value sdl_init_n(const Napi::CallbackInfo& info)
{
    Napi::Env env = info.Env();
    Uint32 flags = info[0].As<Napi::Number>().Uint32Value();
    return Napi::Number::New(env, SDL_Init(flags));
}

Napi::Object Init(Napi::Env env, Napi::Object exports)
{
    exports.Set(Napi::String::New(env, "init"), Napi::Function::New<sdl_init_n>(env));
    //exports.Set(Napi::String::New(env, "createWindow"), Napi::Function::New<sdl_create_window>(env));
    return exports;
}

NODE_API_MODULE(addon, Init);

I tried to change NODE_API_MODULE(addon, Init) in NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init) in the cpp file. I've also tried to change the require removing the .node extension

EDIT: This is the binding.gyp file

{
    'targets': [
        {
            'target_name': 'canvas_sdl2',
            'sources': ['src\\sdl2node.cpp'],
            'include_dirs': [
                "C:\\SDL\\SDL2-2.26.5\\include",
                "C:\\SDL_Image\\SDL2_image-2.6.3\\include",
                "<!@(node -p \"require('node-addon-api').include\")"
            ],
            'dependencies': [
                "<!(node -p \"require('node-addon-api').gyp\")"
            ],
            'libraries': [
                "C:\\SDL\\SDL2-2.26.5\\lib\\x64\\SDL2.lib",
                "C:\\SDL_Image\\SDL2_image-2.6.3\\lib\\x64\\SDL2_image.lib"
            ],
            'cflags!': ['-fno-exceptions'],
            'cflags_cc!': ['-fno-exceptions'],
            'xcode_settings': {
                'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
                'CLANG_CXX_LIBRARY': 'libc++',
                'MACOSX_DEPLOYMENT_TARGET': '10.7'
            },
            'msvs_settings': {
                'VCCLCompilerTool': {'ExceptionHandling': 1},
            }
        }
    ]
}


Solution

  • I've fixed the problem copying the SDl2.dll file into the build\Release\ folder using binding.gyp:

    {
        'targets': [
            {
                'target_name': 'canvas_sdl2',
                'sources': ['src\\sdl2node.cpp'],
                'cflags!': ['-fno-exceptions'],
                'cflags_cc!': ['-fno-exceptions'],
                'xcode_settings': {
                    'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
                    'CLANG_CXX_LIBRARY': 'libc++',
                    'MACOSX_DEPLOYMENT_TARGET': '10.7'
                },
                'msvs_settings': {
                    'VCCLCompilerTool': {'ExceptionHandling': 1},
                },
                'conditions': [
                    ["OS==\"win\"", {
                        'include_dirs': [
                            "C:\\SDL\\SDL2-2.26.5\\include",
                            "C:\\SDL_Image\\SDL2_image-2.6.3\\include",
                            "<!@(node -p \"require('node-addon-api').include\")"
                        ],
                        'dependencies': [
                            "<!(node -p \"require('node-addon-api').gyp\")"
                        ],
                        'libraries': [
                            "C:\\SDL\\SDL2-2.26.5\\lib\\x64\\SDL2.lib",
                            "C:\\SDL_Image\\SDL2_image-2.6.3\\lib\\x64\\SDL2_image.lib"
                        ],
                        'copies': [ 
                            # Here i copy the SDL2.dll file
                            {
                                'destination': '<(module_root_dir)/build/Release/',
                                'files': [
                                    'C:\\SDL\\SDL2-2.26.5\\lib\\x64\\SDL2.dll'
                                ]
                            }
                        ]
                    }]
                ]
            }
        ]
    }