Search code examples
node.jsnode-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


I have a node application that I bundle into an .exe and runs as a windows service. I include the node_modules and node.exe so the user does not need to have npm/node installed on their system. This was fine until I needed to include a .dll. I use node-gyp and a binding.gyp file to create the .node file I use in the app. It seems that node-gyp hardcodes absolute paths to the machine that built the .node file. So when the .exe tries to run on a different machine, it errors out looking for a path to my computer. Here is an example of my binding.gyp file: { "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"] } ] }

Here is the error message when trying to run the .exe: Error: Cannot open C:\Users\xxxx\Documents\node-thin-client\xxx-service\build\Release\interface.node: Error: The specified module could not be found.

This is how I am requiring the file in index.js: const interface = require('../build/Release/interface.node');

Everything works fine on my machine, but when installing and running the node windows service on a different machine it is still looking for the path on my computer.

Anyone know if there is a way to set relative/generic paths with node-gyp? Depending on the user to have node/npm/python/visual c++ build tools installed is not an option.


Solution

  • I ended up using native-ext-loader in my webpack.config. module: { rules: [ { test: /\.node$/, loader: "native-ext-loader", options: { rewritePath: path.resolve('C:/Program Files (x86)/XXX/DeviceManager/build/Release') } } ] } I hard coded the installation directory the .exe is installed to.