Reputation: 4903
I wan't to execute a shell command using child_process from node.js https://nodejs.org/api/child_process.html, I am doing an electron program using React JS.
I want to do a Promise with bluebird, my function works but just for a small command like 'ls' but if I want to execute a simple hello world program in a folder I want to do something like : cd localbuild/login/ && java Main
. It's working on my terminal. When I tried to do that in my function I have this error : promise rejected: Error: spawn cd localbuild/login/ ENOENT closing code: -2
.
Here is my function :
_compile(command){
var Promise = require('bluebird');
var exec = require('child_process').execFile;
var pathFile = "cd localbuild/login/";
function promiseFromChildProcess(child) {
return new Promise(function (resolve, reject) {
child.addListener("error", reject);
child.addListener("exit", resolve);
});
}
var child = exec(pathFile+ " && "+command);
//var child = exec('ls'); // It works
promiseFromChildProcess(child).then(function (result) {
console.log('promise complete: ' + result);
}, function (err) {
console.log('promise rejected: ' + err);
});
child.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
child.stderr.on('data', function (data) {
console.log('stdout: ' + data);
});
child.on('close', function (code) {
console.log('closing code: ' + code);
});
}
Can you help me please ?
Upvotes: 1
Views: 363
Reputation: 696
The function of the child_process library you are importing is execFile, but you use it to run a shell command and not to run an executable file.
Just change :
var exec = require('child_process').execFile;
To:
var exec = require('child_process').exec;
And it should work!
Upvotes: 2