An enhanced child_process for node.js called execa

I have written a post on using the built in node.js child_process module which is one way to go about launching additional processes in a node.js environment. However I have not ever look into, and tested out something that is an improvement over that. Today I have come across something called execa that does the same thing as child_process, but adds some more features, such as making each method a promise.

Basic example of execa

So the method that will be used the most typically is what is given in the main method that is exported when requiring execa into a project. This method works just like spawn in the native child_process node.js module, so it is faster, and safer than another method in this project that works like exec.

So I made a simple test script that just does something with arguments.

1
2
3
4
5
6
7
let argv = process.argv.slice(2);
if (argv.length >= 2) {
console.log(Number(argv[0]) + Number(argv[1]));
}

And my basic.js file that I will be parent process that I will start with node looks like this.

1
2
3
4
5
6
7
8
9
10
11
12
let execa = require('execa');
execa('node', ['script_test', '40','2']).then(function (data) {
console.log('the answer is');
console.log(data.stdout);
}).catch (function (e) {
console.log(e);
});