将节点应用程序捆绑到 exe 后,它正在寻找一个 .node 文件,该文件的路径被硬编码到使用 node-gyp 构建的计算机上

After bundling a node app into an exe, It is looking for a .node file thats path is hardcoded to the computer it was built on with node-gyp

我有一个节点应用程序,我将其作为 windows 服务捆绑到 .exe 和 运行s 中。我包括 node_modules 和 node.exe,因此用户不需要在他们的系统上安装 npm/node。这很好,直到我需要包含一个 .dll。我使用 node-gyp 和 binding.gyp 文件来创建我在应用程序中使用的 .node 文件。似乎 node-gyp 硬编码了构建 .node 文件的机器的绝对路径。因此,当 .exe 尝试在另一台计算机上 运行 时,它会错误地寻找我的计算机的路径。这是我的 binding.gyp 文件的示例:{ "targets": [ { "target_name": "interface", "sources": [ "src/cfcInterface/interface.cpp" ], "include_dirs": [ "<!@(node -p \"require('node-addon-api').include\")", "include" ], "dependencies": [ "<!(node -p \"require('node-addon-api').gyp\")" ], "libraries": [ "<(module_root_dir)/lib/xxx.lib" ], "copies": [ { "destination": "<(module_root_dir)/build/Release", "files": [ "<(module_root_dir)/lib/xxxxxxxxx.dll", "<(module_root_dir)/lib/xxxxxxxx.dll", "<(module_root_dir)/lib/xxxxxxx.dll", ] } ], "cflags!": ["-fno-exceptions"], "cflags_cc!": ["-fno-exceptions"], "defines": ["NAPI_CPP_EXCEPTIONS"] } ] }

这是尝试 运行 .exe 时的错误消息:错误:无法打开 C:\Users\xxxx\Documents\node-thin-client\xxx-service\build\Release\interface.node:错误:找不到指定的模块。

这就是我在 index.js 中要求文件的方式:const interface = require('../build/Release/interface.node');

在我的机器上一切正常,但是当在另一台机器上安装和 运行ning 节点 windows 服务时,它仍在我的电脑上寻找路径。

有人知道是否有办法用 node-gyp 设置 relative/generic 路径吗?根据用户安装 node/npm/python/visual c++ 构建工具不是一个选项。

我最终在 webpack.config 中使用了 native-ext-loadermodule: { rules: [ { test: /\.node$/, loader: "native-ext-loader", options: { rewritePath: path.resolve('C:/Program Files (x86)/XXX/DeviceManager/build/Release') } } ] } 我硬编码了 .exe 安装到的安装目录。