What is cross-spawn?
The cross-spawn npm package is a cross-platform solution for spawning child processes in Node.js. It aims to resolve issues with spawning commands across different operating systems, particularly Windows, where the built-in child_process.spawn can behave inconsistently. It provides a consistent API for spawning processes and handling their output, errors, and exit codes.
What are cross-spawn's main functionalities?
Spawning a child process
This feature allows you to spawn a child process, similar to child_process.spawn, but with improved cross-platform support. The code sample demonstrates how to spawn an 'npm install' process and listen for its exit event.
const spawn = require('cross-spawn');
const child = spawn('npm', ['install']);
child.on('exit', function (code, signal) {
console.log('Child process exited with code ' + code);
});
Spawning a synchronous child process
This feature allows you to spawn a child process and wait for it to finish synchronously, returning the result. The code sample demonstrates how to synchronously spawn an 'npm install' process and log the exit code.
const spawnSync = require('cross-spawn').sync;
const result = spawnSync('npm', ['install'], { stdio: 'inherit' });
console.log('Child process exited with code ' + result.status);
Handling command-line arguments properly
This feature ensures that command-line arguments are handled properly across different platforms. The code sample demonstrates how to spawn an 'echo' command with arguments and handle the standard output.
const spawn = require('cross-spawn');
const child = spawn('echo', ['Hello World!'], { shell: true });
child.stdout.on('data', function (data) {
console.log('stdout: ' + data.toString());
});
Other packages similar to cross-spawn
child_process
This is a core Node.js module that provides the ability to spawn child processes. While it is similar to cross-spawn, it does not always handle cross-platform inconsistencies, especially on Windows, as well as cross-spawn does.
execa
Execa is a popular npm package that provides a similar functionality to cross-spawn. It improves child process methods and supports synchronous and promise-based APIs. It is often considered a more modern alternative with additional features like automatic error handling, but cross-spawn is still preferred in some cases for its simplicity and specific cross-platform fixes.
shelljs
ShellJS is a portable Unix shell commands implementation for Node.js. It provides a high-level API to execute shell commands in a cross-platform way. While it offers similar cross-platform benefits, it is more focused on emulating Unix shell commands rather than providing a direct child process spawn interface.
cross-spawn
A cross platform solution to node's spawn and spawnSync.
Installation
$ npm install cross-spawn
Why
Node has issues when using spawn on Windows:
- It ignores PATHEXT
- It does not support shebangs
- Has problems running commands with spaces
- Has problems running commands with posix relative paths (e.g.:
./my-folder/my-executable
) - Has an issue with command shims (files in
node_modules/.bin/
), where arguments with quotes and parenthesis would result in invalid syntax error - No
options.shell
support on node <v4.8
All these issues are handled correctly by cross-spawn
.
There are some known modules, such as win-spawn, that try to solve this but they are either broken or provide faulty escaping of shell arguments.
Usage
Exactly the same way as node's spawn
or spawnSync
, so it's a drop in replacement.
var spawn = require('cross-spawn');
var child = spawn('npm', ['list', '-g', '-depth', '0'], { stdio: 'inherit' });
var results = spawn.sync('npm', ['list', '-g', '-depth', '0'], { stdio: 'inherit' });
Caveats
Using options.shell
as an alternative to cross-spawn
Starting from node v4.8
, spawn
has a shell
option that allows you run commands from within a shell. This new option solves
the PATHEXT issue but:
- It's not supported in node
<v4.8
- You must manually escape the command and arguments which is very error prone, specially when passing user input
- There are a lot of other unresolved issues from the Why section that you must take into account
If you are using the shell
option to spawn a command in a cross platform way, consider using cross-spawn
instead. You have been warned.
options.shell
support
While cross-spawn
adds support for options.shell
in node <v4.8
, all of its enhancements are disabled.
This mimics the Node.js behavior. More specifically, the command and its arguments will not be automatically escaped nor shebang support will be offered. This is by design because if you are using options.shell
you are probably targeting a specific platform anyway and you don't want things to get into your way.
Shebangs support
While cross-spawn
handles shebangs on Windows, its support is limited. More specifically, it just supports #!/usr/bin/env <program>
where <program>
must not contain any arguments.
If you would like to have the shebang support improved, feel free to contribute via a pull-request.
Remember to always test your code on Windows!
Tests
$ npm test
$ npm test -- --watch
during development
License
Released under the MIT License.