\href{https://npmjs.org/package/cross-spawn}{\texttt{ }} \href{https://npmjs.org/package/cross-spawn}{\texttt{ }} \href{https://travis-ci.org/moxystudio/node-cross-spawn}{\texttt{ }} \href{https://ci.appveyor.com/project/satazor/node-cross-spawn}{\texttt{ }} \href{https://codecov.io/gh/moxystudio/node-cross-spawn}{\texttt{ }} \href{https://david-dm.org/moxystudio/node-cross-spawn}{\texttt{ }} \href{https://david-dm.org/moxystudio/node-cross-spawn?type=dev}{\texttt{ }} \href{https://greenkeeper.io/}{\texttt{ }}

A cross platform solution to node\textquotesingle{}s spawn and spawn\+Sync.\hypertarget{md_sfcode_node_modules_cross_spawn_README_autotoc_md6890}{}\doxysection{Installation}\label{md_sfcode_node_modules_cross_spawn_README_autotoc_md6890}
{\ttfamily \$ npm install cross-\/spawn}\hypertarget{md_sfcode_node_modules_cross_spawn_README_autotoc_md6891}{}\doxysection{Why}\label{md_sfcode_node_modules_cross_spawn_README_autotoc_md6891}
Node has issues when using spawn on Windows\+:


\begin{DoxyItemize}
\item It ignores \href{https://github.com/joyent/node/issues/2318}{\texttt{ PATHEXT}}
\item It does not support \href{https://en.wikipedia.org/wiki/Shebang_(Unix)}{\texttt{ shebangs}}
\item Has problems running commands with \href{https://github.com/nodejs/node/issues/7367}{\texttt{ spaces}}
\item Has problems running commands with posix relative paths (e.\+g.\+: {\ttfamily ./my-\/folder/my-\/executable})
\item Has an \href{https://github.com/moxystudio/node-cross-spawn/issues/82}{\texttt{ issue}} with command shims (files in {\ttfamily node\+\_\+modules/.bin/}), where arguments with quotes and parenthesis would result in \href{https://github.com/moxystudio/node-cross-spawn/blob/e77b8f22a416db46b6196767bcd35601d7e11d54/test/index.test.js\#L149}{\texttt{ invalid syntax error}}
\item No {\ttfamily options.\+shell} support on node {\ttfamily $<$v4.\+8}
\end{DoxyItemize}

All these issues are handled correctly by {\ttfamily cross-\/spawn}. There are some known modules, such as \href{https://github.com/ForbesLindesay/win-spawn}{\texttt{ win-\/spawn}}, that try to solve this but they are either broken or provide faulty escaping of shell arguments.\hypertarget{md_sfcode_node_modules_cross_spawn_README_autotoc_md6892}{}\doxysection{Usage}\label{md_sfcode_node_modules_cross_spawn_README_autotoc_md6892}
Exactly the same way as node\textquotesingle{}s \href{https://nodejs.org/api/child_process.html\#child_process_child_process_spawn_command_args_options}{\texttt{ {\ttfamily spawn}}} or \href{https://nodejs.org/api/child_process.html\#child_process_child_process_spawnsync_command_args_options}{\texttt{ {\ttfamily spawn\+Sync}}}, so it\textquotesingle{}s a drop in replacement.


\begin{DoxyCode}{0}
\DoxyCodeLine{const spawn = require('cross-\/spawn');}
\DoxyCodeLine{}
\DoxyCodeLine{// Spawn NPM asynchronously}
\DoxyCodeLine{const child = spawn('npm', ['list', '-\/g', '-\/depth', '0'], \{ stdio: 'inherit' \});}
\DoxyCodeLine{}
\DoxyCodeLine{// Spawn NPM synchronously}
\DoxyCodeLine{const result = spawn.sync('npm', ['list', '-\/g', '-\/depth', '0'], \{ stdio: 'inherit' \}); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_cross_spawn_README_autotoc_md6893}{}\doxysection{Caveats}\label{md_sfcode_node_modules_cross_spawn_README_autotoc_md6893}
\hypertarget{md_sfcode_node_modules_cross_spawn_README_autotoc_md6894}{}\doxysubsection{Using $<$tt$>$options.\+shell$<$/tt$>$ as an alternative to $<$tt$>$cross-\/spawn$<$/tt$>$}\label{md_sfcode_node_modules_cross_spawn_README_autotoc_md6894}
Starting from node {\ttfamily v4.\+8}, {\ttfamily spawn} has a {\ttfamily shell} option that allows you run commands from within a shell. This new option solves the \href{https://github.com/joyent/node/issues/2318}{\texttt{ PATHEXT}} issue but\+:


\begin{DoxyItemize}
\item It\textquotesingle{}s not supported in node {\ttfamily $<$v4.\+8}
\item You must manually escape the command and arguments which is very error prone, specially when passing user input
\item There are a lot of other unresolved issues from the \href{\#why}{\texttt{ Why}} section that you must take into account
\end{DoxyItemize}

If you are using the {\ttfamily shell} option to spawn a command in a cross platform way, consider using {\ttfamily cross-\/spawn} instead. You have been warned.\hypertarget{md_sfcode_node_modules_cross_spawn_README_autotoc_md6895}{}\doxysubsection{$<$tt$>$options.\+shell$<$/tt$>$ support}\label{md_sfcode_node_modules_cross_spawn_README_autotoc_md6895}
While {\ttfamily cross-\/spawn} adds support for {\ttfamily options.\+shell} in node {\ttfamily $<$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 {\ttfamily options.\+shell} you are probably targeting a specific platform anyway and you don\textquotesingle{}t want things to get into your way.\hypertarget{md_sfcode_node_modules_cross_spawn_README_autotoc_md6896}{}\doxysubsection{Shebangs support}\label{md_sfcode_node_modules_cross_spawn_README_autotoc_md6896}
While {\ttfamily cross-\/spawn} handles shebangs on Windows, its support is limited. More specifically, it just supports {\ttfamily \#!/usr/bin/env $<$program$>$} where {\ttfamily $<$program$>$} must not contain any arguments. ~\newline
 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!\hypertarget{md_sfcode_node_modules_cross_spawn_README_autotoc_md6897}{}\doxysection{Tests}\label{md_sfcode_node_modules_cross_spawn_README_autotoc_md6897}
{\ttfamily \$ npm test} ~\newline
 {\ttfamily \$ npm test -\/-\/ -\/-\/watch} during development\hypertarget{md_sfcode_node_modules_cross_spawn_README_autotoc_md6898}{}\doxysection{License}\label{md_sfcode_node_modules_cross_spawn_README_autotoc_md6898}
Released under the \href{http://www.opensource.org/licenses/mit-license.php}{\texttt{ MIT License}}. 