The execa npm package is a process execution tool that simplifies working with child processes in Node.js. It provides a better user experience than the default child_process module by offering a promise-based API, improved Windows support, and additional convenience options.
What are execa's main functionalities?
Executing a shell command
This feature allows you to execute a shell command and obtain the result. The example shows how to execute the 'echo' command and print 'unicorns' to the console.
This feature is used to execute a command synchronously, blocking the event loop until the process has finished. The example synchronously executes the 'echo' command and logs the result.
This feature allows you to stream the output of a command directly to the console or another stream. The example streams the output of the 'echo' command to the process's stdout.
ShellJS is a portable Unix shell commands implementation for Node.js. It offers a higher-level API for executing commands but does not support returning promises natively.
Cross-spawn is a cross-platform solution for spawning child processes. It aims to solve compatibility issues on Windows but does not provide a promise-based API.
Execa runs commands in your script, application or library. Unlike shells, it is optimized for programmatic usage. Built on top of the child_process core module.
One of the maintainers @ehmicky is looking for a remote full-time position. Specialized in Node.js back-ends and CLIs, he led Netlify Build, Plugins and Configuration for 2.5 years. Feel free to contact him on his website or on LinkedIn!
const {stdout, pipedFrom} = await execa`npm run build`
.pipe`sort`
.pipe`head -n 2`;
// Output of `npm run build | sort | head -n 2`console.log(stdout);
// Output of `npm run build | sort`console.log(pipedFrom[0].stdout);
// Output of `npm run build`console.log(pipedFrom[0].pipedFrom[0].stdout);
// Similar to: npm run build < input.txtawaitexeca({stdin: {file: 'input.txt'}})`npm run build`;
File output
// Similar to: npm run build > output.txtawaitexeca({stdout: {file: 'output.txt'}})`npm run build`;
Split into text lines
const {stdout} = awaitexeca({lines: true})`npm run build`;
// Print first 10 linesconsole.log(stdout.slice(0, 10).join('\n'));
Streaming
Iterate over text lines
forawait (const line of execa`npm run build`) {
if (line.includes('WARN')) {
console.warn(line);
}
}
Transform/filter output
let count = 0;
// Filter out secret lines, then prepend the line numberconst transform = function * (line) {
if (!line.includes('secret')) {
yield`[${count++}] ${line}`;
}
};
awaitexeca({stdout: transform})`npm run build`;
The npm package execa receives a total of 59,411,660 weekly downloads. As such, execa popularity was classified as popular.
We found that execa demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.It has 2 open source maintainers collaborating on the project.
Package last updated on 29 Oct 2024
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.