1

I'm trying to create a child process, using spawn, and would like to be able to handle if the "cwd" is not found. When it's not found, it throws an "ENOENT" error, which on Windows I can only seem to catch if I spawn the process with the "shell: true" property. So what I'm doing:

const { spawn } = require('child_process')
const childProcess = spawn('MyCommand.exe', [], {
    cwd: path.join(__dirname, 'mypath')
}

childProcess.on('error', (err) => {
    // Handle error here
})

childProcess.on('exit', (code, signal) => {
    // Handle exit event
})

On Windows, the above will not properly catch the ENOENT exception unless I do:

const childProcess = spawn('MyCommand.exe', [], {
    cwd: path.join(__dirname, 'mypath'),
    shell: true
}

If I spawn using the above then, if the cwd or command isn't found, the exit event will catch the exception.

But I'd really like to be able to do it without running it in a shell. Is there a way to do this?

Kurt
  • 1,868
  • 3
  • 21
  • 42
  • sorryi dont have the answer but you gave me the aswer about ENOENT error catching. Thank you!! You should post it it to [this stackoverflow question](https://stackoverflow.com/questions/34208614/how-to-catch-an-enoent-with-nodejs-child-process-spawn) – yorg Jun 15 '19 at 19:24

0 Answers0