A cross platform solution to node's spawn and spawnSync.
npm install cross-spawnNode 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
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.
Exactly the same way as node's spawn or spawnSync, so it's a drop in replacement.
import { spawn, spawnSync } from 'cross-spawn';
// Spawn NPM asynchronously
const child = spawn('npm', ['list', '-g', '-depth', '0'], { stdio: 'inherit' });
// Spawn NPM synchronously
const result = spawnSync('npm', ['list', '-g', '-depth', '0'], { stdio: 'inherit' });cross-spawn is an ES module, which CommonJS code can load with require() too:
const { spawn, spawnSync } = require('cross-spawn');TypeScript types are included, so @types/cross-spawn isn't needed.
Node's spawn has a shell option that allows you run commands from within a shell. This option solves
the PATHEXT issue but:
- 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.
When options.shell is used, all of cross-spawn's 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.
Since the arguments are passed to Node's spawn as is, Node.js 24 and later print a DEP0190 deprecation warning when arguments are used together with options.shell, because they are only joined with spaces, not escaped.
On Windows, shell built-ins such as dir or echo aren't programs, so, like with Node's spawn, they need shell: true.
On Unix systems arguments are passed to a new process as an array of strings, but on Windows they are passed as a single command line string and it is up to the child process to parse it back into an array. Therefore cross-spawn and the child process must agree on how the command line is encoded.
cross-spawn escapes arguments according to the conventions of the Microsoft C runtime, which most programs use, including Node.js, Python, Java and .NET. However, some programs have their own way of parsing the command line, in which case they may see a different array of arguments than the one you passed.
Commands that are not executables, such as batch files (.bat and .cmd), are run through cmd.exe, which uses non-standard parsing and is especially vulnerable to malicious input, as it may be used to run arbitrary commands. cross-spawn escapes the command and its arguments according to cmd.exe rules, similarly to other runtimes such as Rust. Still, due to the complexity of cmd.exe, there are limitations that no escaping can overcome:
- Some characters can't be escaped at all. Line breaks (
\rand\n) can't be escaped and passing them results in an error being thrown. - The command must be the name or path of a single program, with its arguments passed in
args. The command is quoted, sospawn('npm install')fails withENOENT, like it does on other platforms. - The command, and the file it resolves to, e.g. through
PATH, can't contain double quotes,^or%, or end with a dot or whitespace. Windows file names can't contain double quotes or end with dots or spaces, and batch files may parse their own path again, e.g. withcall "%~dp0other.bat", where^and%would be interpreted. Passing such a command results in an error being thrown. - Shell built-ins that run other commands, such as
callandstart, pass their arguments to those commands unescaped. Call the program directly instead, e.g.spawn('tool.bat', args)instead ofspawn('call', ['tool.bat', ...args]). - Batch files are assumed to forward arguments as received, e.g. with
%*,%1or"%~1". Batch files that use%~1unquoted,callor enable delayed expansion re-parse their arguments and may be vulnerable to malicious input. - Programs called by batch files must follow the Microsoft C runtime conventions. Programs that don't, such as those written in Go or that use
CommandLineToArgvW, may split arguments containing double quotes differently.
Additionally, programs that read their arguments with the legacy ANSI code page instead of Unicode may convert certain Unicode characters into others, such as " (U+FF02) into ", which changes how arguments are split. This is known as WorstFit and can't be mitigated by cross-spawn.
When passing untrusted input on Windows, validate it so that only a safe subset is allowed, and prefer calling executables directly rather than through batch files.
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!
npm test
# TypeScript types
npm run typecheck
# During development, in watch mode
node --test --watch "test/*.test.js" "src/**/*.test.js"Releases use npm's staged publishing, so a version only goes live after a maintainer approves it with 2FA:
$ npm run releaseruns the tests and lint, bumps the version, updatesCHANGELOG.md, commits and tags locally, then stages the package withnpm stage publish. Nothing is public yet.$ npm stage list cross-spawnshows the stage ID. Optionally, inspect the tarball withnpm stage download <stage-id>.$ npm stage approve <stage-id>asks for 2FA and publishes the version.$ git push --follow-tags origin HEADpushes the release commit and tag.
To abort instead, run npm stage reject <stage-id>, then delete the local tag and reset the release commit. Nothing was pushed, so nothing else needs undoing.
This needs a version of npm that has the npm stage command.
Released under the MIT License.