NodeJS 无法导入,带require的节点插件模块

zc0qhyus  于 2023-04-29  发布在  Node.js
关注(0)|答案(1)|浏览(164)

我正在使用node-addon-API和node-gyp 9开发一个nodejs插件。3.1.然而,当我尝试使用require导入时,它会给我以下错误:

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'

使用NodeJS v200.0,但我也尝试过旧版本,如v18。16.0。插件如下:

#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);

我尝试在cpp文件中的NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)中更改NODE_API_MODULE(addon, Init)。我还尝试更改要求删除。节点扩展
编辑:这是绑定。GYP文件

{
    '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},
            }
        }
    ]
}
1sbrub3j

1sbrub3j1#

我已经修复了复制SDl2的问题。dll文件到build\Release\文件夹中。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'
                            ]
                        }
                    ]
                }]
            ]
        }
    ]
}

相关问题