最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

无法使用 require 导入 .node 插件模块

网站源码admin43浏览0评论

无法使用 require 导入 .node 插件模块

无法使用 require 导入 .node 插件模块

我正在使用 node-addon-api 和 node-gyp 9.3.1 开发一个 nodejs 插件。 但是,当我尝试使用 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 v20.0.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(addon, Init)
中更改
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)
。 我还尝试更改删除 .node 扩展名的要求

编辑: 这是 binding.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},
            }
        }
    ]
}

回答如下:

我已经解决了使用 binding.gyp 将 SDl2.dll 文件复制到 build\Release\ 文件夹中的问题:

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

发布评论

评论列表(0)

  1. 暂无评论