user4445419
user4445419

Reputation:

Change the code from child process exec to spawn

I've node app and I use the child_process.exec api which working OK, the issue is that there is maxBuffer thing that we have trouble with hence I want to change it to work with spawn

The code before was like (this code works fine I just need to pass cmd and options and it was doing the job...)

var child = child_process.exec(cmd,options, function (error) {
....
});

Now i've change it to spawn and it doesnt work

    var child = child_process.spawn(cmd, options);

    child.stdout.on('data', function (data) {
        console.log('stdout: ' + data);
    });

    child.stderr.on('data', function (data) {
        console.log('stderr: ' + data);
    });

Here Im getting error

Error: spawn ENOENT
at errnoException (child_process.js:988:11)
at Process.ChildProcess._handle.onexit (child_process.js:779:34)
stderr: execvp(): No such file or directory

Any Idea what am I missing here?

Upvotes: 2

Views: 1121

Answers (1)

Amit
Amit

Reputation: 46361

When starting a child process with spawn, you need to separate the command from the args.

If you had:

child_process.exec('somecmd somearg somearg2', options, function() {...});

You now need:

child_process.spawn('somecmd', ['somearg', 'somearg2'], options);

If there are no arguments to use, pass an empty array:

child_process.spawn('somecmd', [], options);

Upvotes: 1

Related Questions