使用electron样板文件为windows创建.exe。它需要运行一个.bat文件。一旦它';它是打包的,不需要';跑不动

使用electron样板文件为windows创建.exe。它需要运行一个.bat文件。一旦它';它是打包的,不需要';跑不动,electron,child-process,Electron,Child Process,使用electron样板文件为windows创建一个.exe,它需要运行一个.bat文件。然而,使用npm start它可以工作,但是当它与npm run发行版打包时,它不会运行.bat 这是我的函数代码 const spawn = require('child_process').spawn; const bat = spawn('cmd.exe', ['/c', 'Install.bat']); bat.stdout.on('data', (data) => { var st

使用electron样板文件为windows创建一个.exe,它需要运行一个.bat文件。然而,使用npm start它可以工作,但是当它与npm run发行版打包时,它不会运行.bat

这是我的函数代码

const spawn = require('child_process').spawn;
const bat = spawn('cmd.exe', ['/c', 'Install.bat']);

bat.stdout.on('data', (data) => {
    var str = String.fromCharCode.apply(null, data);
    addLog(data);
    console.info(str);
});

bat.stderr.on('data', (data) => {
    var str = String.fromCharCode.apply(null, data);
    addLog(data,"error");
    console.error(str); 
});

bat.on('exit', (code) => {
    console.log(`Exit ${code}`);
});

已经检查了子进程

当您通过
npm start
运行electron时,它通常会将当前工作目录设置为应用程序的文件夹(包含您的package.json)。因此,它将在该文件夹中查找
cmd.exe

构建并运行应用程序后,当前工作目录可能位于其他位置,例如
C:\\
(在Windows上)。您可以使用
process.cwd()
找到当前工作目录

要查找应用程序文件夹,而不管应用程序如何运行,Electron提供了
Electron.app.getAppPath()

所以你可以这样使用它:

const path = require('path');
const cmdPath = path.join(electron.app.getAppPath(),'cmd.exe');
const bat = spawn(cmdPath, ['/c', 'Install.bat']);

cmd.exe
是否与您的电子应用程序位于同一目录中?这可能是路径问题。这是否意味着我必须在Windows SYSTEM 32路径中安装electron样板文件文件夹?我的文件夹在CAlready中,已将cmd.exe添加到electron样板文件夹中,但仍不工作。我将其视为“是”。我不是建议修复,只是在尝试回答之前询问更多信息。回答遵循shortlyOkay,所以我编辑了我的代码,但是当我执行“npm run release”时,文件不工作,并且.bat不运行const path=require('path');const cmdPath=path.join(app.getAppPath(),'cmd.exe');const spawn=require('child_process')。spawn;const bat=spawn(cmdPath,['/c','Install.bat']);