Home>

such a problem, there is a folder with a program that starts via a .bat file, when I start this bat manually, everything starts up fine, but I need to start using node js, and when I do this, an error pops up that .bat the file cannot find the .exe it needs to run. Launch code:

const {spawn}= require ('child_process');
const bat= spawn ('cmd.exe', ['/c', botFile]);
bat.stdout.on ('data', (data)= >
 {
    console.log (data.toString ());
});
bat.stderr.on ('data', (data)= >
 {
    console.error (data.toString ());
});
bat.on ('exit', (code)= >
 {
    console.log (`Child exited with code $ {code}`);
});

.bat file:

Start /min fleamarket.exe "fleamarketstart.cms"
    exit

The path to the .bat is registered correctly, the file itself starts, but it no longer sees the .exe that it should launch, the .bat and .exe are in the same folder, when I manually click, everything works, the problem is only through node js. What could be the problem?

Perhaps your node script and bat-file are in different folders. Then, if you run a node script from its folder, the script folder becomes the current folder, then the bat file looks for an .exe in this folder and does not find it.

vsemozhebuty2021-09-20 12:57:39
  • Answer # 1

    I still did not find a solution to do it as I wrote above, but this one more than suited me, I just execute the command from the .bat file directly in node js, specifying the full path to the .exe file. It now looks like this:

    const {exec}= require ('child_process');
    let botFile= path.resolve (__ dirname + '/bot /', 'fleamarket.exe');
    exec (`Start /min $ {botFile}" fleamarketstart.cms "exit`, (error, stdout, stderr)= >
     {
        if (error) {
            console.error (`exec error: $ {error}`);
            return;
        }
        console.log (`stdout: $ {stdout}`);
        console.error (`stderr: $ {stderr}`);
    });