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?