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

时间:2018-06-26 17:09:19

标签: node.js node-gyp

我有一个节点应用程序,我将其捆绑到一个.exe中并作为Windows服务运行。我包括了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:错误:The找不到指定的模块。

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

一切都可以在我的计算机上正常运行,但是在另一台计算机上安装并运行节点Windows服务时,它仍在我的计算机上寻找路径。

有人知道是否可以使用node-gyp设置相对/通用路径吗?取决于用户安装的是node / npm / python / visual c ++构建工具,不是一个选择。

1 个答案:

答案 0 :(得分:0)

我最终在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的安装目录进行了硬编码。