Packaging Node App with pkg causes 'ENOENT' error, when calling another executable, despite file existing

Viewed 28

I'm trying to use the execFile command from child_process to run an executable. In my local computer/environment, everything works flawlessly, but when I try to run the app through the packaged .exe file generated by pkg, I get an ENOENT error:

Therefore I checked the command with fs.exists(), but it returns true. So I'm unsure how to fix this, since I'm wondering if its my build configuration, or a compatibility error/issue with how pkg's snapshot system works. Any help, workarounds, etc, would be appreciated :) !

index.js

const { execFile } = require("child_process");
const fs = require("fs");
const execDirectory = path.join(__dirname, "./executables");
const determineActiveWindows = async (appDataDirectory) => {
  const command = `${execDirectory}\\determineActiveWindows.exe`;
  console.log("file exists", fs.existsSync(command));
  console.log("command", command);
  execFile(
    command,
    ["--appDataDirectory", appDataDirectory],
    (err, stdout, sterr) => {
      if (err) console.error(err);
      if (stdout) console.log(stdout);
      if (sterr) console.error(sterr);
    }
  );
};

Error in command line:

file exists true
command C:\snapshot\MinimizeWindows\build\app\executables\determineActiveWindows.exe
Error: spawn C:\snapshot\MinimizeWindows\build\app\executables\determineActiveWindows.exe ENOENT
    at Process.ChildProcess._handle.onexit (node:internal/child_process:283:19)
    at onErrorNT (node:internal/child_process:478:16)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)
    at process.runNextTicks [as _tickCallback] (node:internal/process/task_queues:65:3)
    at Function.runMain (pkg/prelude/bootstrap.js:1984:13)
    at node:internal/main/run_main_module:17:47 {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'spawn C:\\snapshot\\MinimizeWindows\\build\\app\\executables\\determineActiveWindows.exe',
  path: 'C:\\snapshot\\MinimizeWindows\\build\\app\\executables\\determineActiveWindows.exe',
  spawnargs: [
    '--appDataDirectory',
    'appName'
  ],
  cmd: 'C:\\snapshot\\MinimizeWindows\\build\\app\\executables\\determineActiveWindows.exe --appDataDirectory appName'
}

build directory structure

-build
---app
------executables
---------determineActiveWindows.exe
------index.js

pkg command

pkg -c package.json build/app/index.js 

package.json pkg config

  "pkg": {
    "targets": ["node16-win-x64"],
    "scripts": "build/**/*.js",
    "assets": "build/**/*",
    "outputPath": "dist"
  }
1 Answers

Don't use path.join when you need to reference an external file that is not packaged into the exe at build time, pkg is overriding it.

From error we see: C:\\snapshot 'C:\\snapshot\\MinimizeWindows\\build\\app\\executables\\determineActiveWindows.exe --appDataDirectory appName'

Related