打包电子应用程序后节点子进程立即退出

Node child process exits immediately after packing the electron app

我的电子应用程序的 GUI 部分中有这段代码,当来自终端的 运行 时,它工作得很好。我使用 'electron-packager' 打包了应用程序,然后我开始遇到一些问题。

最初,子进程立即终止并给出代码 127,我通过使用此处讨论的 'fix-path' 模块解决了这个问题。 https://github.com/electron/electron/issues/7688

即使在此之后,进程也会立即退出并返回代码 1,我无法解决此问题,因为没有报告任何错误。一旦子进程退出,有没有办法捕获这个 exception/error ?

const fixPath = require('fix-path');
let launch = () => {
fixPath();

const path = "SOME PATH";
var command = 'node ' + 
              path + 
              ' -d ' +      
              ' -e ' +     
              ' -r ' +      
              ' -p ' + 30 +
              ' -w ' +     
              ' -g ' +     
              '-server__ ';


const child = childProcess.exec(command, {
  detached: true,   
  stdio: 'ignore'
});

child.on('error', (err) => {
  console.log("\n\t\tERROR: spawn failed! (" + err + ")");
});

child.on('exit', (code, signal) => {
  console.log(code);
  console.log("\n\t\tGUI: spawned completed it's work!");
});

可以使用 child.stderr 数据事件处理程序来捕获错误。我在我的脚本中添加了这段代码,并且能够通过控制台上的输出调试问题。

child.stderr.on('data', function(data) {
  console.log('stdout: ' + data);
});

参考这篇帮助我解决这个问题的文章。 https://medium.freecodecamp.org/node-js-child-processes-everything-you-need-to-know-e69498fe970a