Node Child Process Exec Command Failed with error code 1
Asked Answered
M

2

18

I am trying to execute some line using node js child process and getting error. Following is my code:

let cmd : string = "code " + PROJECTS[value];
exec(cmd, function callback(error, stdout, stderr) {
  console.log("started console app");
});

ERROR :

cmd:"C:\WINDOWS\system32\cmd.exe /s /c "code c:\Users\shana\Dropbox\code-settings-syn... (length: 82)"
code:1
killed:false
message:"Command failed: C:\WINDOWS\system32\cmd.exe /s /c "code c:\Users\shana\Dropbox\c... (length: 99)"
signal:null
stack:undefined

Detail of error JSON.

Full CMD : "C:\WINDOWS\system32\cmd.exe /s /c "code c:\Users\shana\Dropbox\code-settings-sync""
Full message : "Command failed: C:\WINDOWS\system32\cmd.exe /s /c "code c:\Users\shana\Dropbox\code-settings-sync"\n"
Mag answered 5/1, 2016 at 22:41 Comment(2)
Try to copy full command from your programm and execute it by hand in terminal.Nero
its working when i copy and run the command in terminal but here after running 1,2 time i gives errorMag
M
1

try a simpler example ..

var exec = require('child_process').exec;
var cmd = 'code C:\Program Files';
exec(cmd, function(err, stdout, stderr) {
if (err) {
console.error(err);
return;
}
console.log(stdout); 
});

does this work??

Midgett answered 11/1, 2016 at 7:23 Comment(3)
the problem is that it works 1,2 times this example and my example after that same error popupsMag
verify the string you are passing to exec command or storing in array . is it properly escaped in terms of your osMidgett
yes you can check the full CMD in the code i have shown and its working fine in Windows when i write in command prompt.Mag
U
1

I didn't want to see the whole error (too verbose) so I did something like this:

try { 
  const { stdout, stderr } = await exec('echo TEST');
  console.log('stdout:', stdout);
  console.log('stderr:', stderr);
} catch (e) {
  // If exec fails and you want to see the whole ugly error: 
  // console.error(e);
  console.log('How about a nice human readable message instead?'); 
}

Because of the "await" this goes inside an "async" function. More information: https://mcmap.net/q/135929/-how-to-promisify-node-39-s-child_process-exec-and-child_process-execfile-functions-with-bluebird

Unpile answered 1/8, 2020 at 11:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.