Preventing Child Process Termination on Node.js Error When Executing an EXE File

35 Views Asked by At

I'm a developer working with Node.js and I've encountered an issue using the child_process module's execFile function.

When I use it to open and run an EXE file, I've noticed that if my Node.js project encounters an error, the process started by the EXE file also terminates unexpectedly.

Is there an alternative function or method I can use to ensure that running ./index.exe from the terminal behaves the same way when invoked from my Node.js project, without it being affected by the Node.js process's state?

I attempted to execute the file via an HTTP request. Could this have been an issue?

executableRouter.post('/start-exe', async (req, res) => {
  const running = await isProcessRunning(exeFile); 
  if (running) {
    return res
      .status(200)
      .json({ message: 'Process is already running. No action taken.' });
  }

  execFile(exeFile, (error, stdout, stderr) => {
    if (error) {
      console.error(`Error executing .exe: ${error}`);
    }
  });

  return res.status(200).json({ message: 'Attempting to start .exe' });
});

0

There are 0 best solutions below