1<!DOCTYPE html> 2<html lang="en"> 3<head> 4 <meta charset="utf-8"> 5 <meta name="viewport" content="width=device-width"> 6 <meta name="nodejs.org:node-version" content="v14.20.1"> 7 <title>Child process | Node.js v14.20.1 Documentation</title> 8 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 9 <link rel="stylesheet" href="assets/style.css"> 10 <link rel="stylesheet" href="assets/hljs.css"> 11 <link rel="canonical" href="https://nodejs.org/api/child_process.html"> 12</head> 13<body class="alt apidoc" id="api-section-child_process"> 14 <div id="content" class="clearfix"> 15 <div id="column2" class="interior"> 16 <div id="intro" class="interior"> 17 <a href="/" title="Go back to the home page"> 18 Node.js 19 </a> 20 </div> 21 <ul> 22<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 23<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 24</ul> 25<hr class="line"> 26<ul> 27<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 28<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 29<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 30<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 31<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 32<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 33<li><a href="child_process.html" class="nav-child_process active">Child processes</a></li> 34<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 35<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 36<li><a href="console.html" class="nav-console">Console</a></li> 37<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 38<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 39<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 40<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 41<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 42<li><a href="dns.html" class="nav-dns">DNS</a></li> 43<li><a href="domain.html" class="nav-domain">Domain</a></li> 44<li><a href="errors.html" class="nav-errors">Errors</a></li> 45<li><a href="events.html" class="nav-events">Events</a></li> 46<li><a href="fs.html" class="nav-fs">File system</a></li> 47<li><a href="globals.html" class="nav-globals">Globals</a></li> 48<li><a href="http.html" class="nav-http">HTTP</a></li> 49<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 50<li><a href="https.html" class="nav-https">HTTPS</a></li> 51<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 52<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 53<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 54<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 55<li><a href="module.html" class="nav-module">Modules: <code>module</code> API</a></li> 56<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 57<li><a href="net.html" class="nav-net">Net</a></li> 58<li><a href="os.html" class="nav-os">OS</a></li> 59<li><a href="path.html" class="nav-path">Path</a></li> 60<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 61<li><a href="policy.html" class="nav-policy">Policies</a></li> 62<li><a href="process.html" class="nav-process">Process</a></li> 63<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 64<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 65<li><a href="readline.html" class="nav-readline">Readline</a></li> 66<li><a href="repl.html" class="nav-repl">REPL</a></li> 67<li><a href="report.html" class="nav-report">Report</a></li> 68<li><a href="stream.html" class="nav-stream">Stream</a></li> 69<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 70<li><a href="timers.html" class="nav-timers">Timers</a></li> 71<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 72<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 73<li><a href="tty.html" class="nav-tty">TTY</a></li> 74<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 75<li><a href="url.html" class="nav-url">URL</a></li> 76<li><a href="util.html" class="nav-util">Utilities</a></li> 77<li><a href="v8.html" class="nav-v8">V8</a></li> 78<li><a href="vm.html" class="nav-vm">VM</a></li> 79<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 80<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 81<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 82</ul> 83<hr class="line"> 84<ul> 85<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 86</ul> 87 </div> 88 89 <div id="column1" data-id="child_process" class="interior"> 90 <header> 91 <div class="header-container"> 92 <h1>Node.js v14.20.1 documentation</h1> 93 <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 94 <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 95 <path fill="none" d="M0 0h24v24H0z" /> 96 <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/> 97 <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/> 98 </svg> 99 <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 100 <path d="M0 0h24v24H0z" fill="none" /> 101 <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/> 102 </svg> 103 </button> 104 </div> 105 <div id="gtoc"> 106 <ul> 107 <li> 108 <a href="index.html">Index</a> 109 </li> 110 <li> 111 <a href="all.html">View on single page</a> 112 </li> 113 <li> 114 <a href="child_process.json">View as JSON</a> 115 </li> 116 117 <li class="version-picker"> 118 <a href="#">View another version <span>▼</span></a> 119 <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v18.x/api/child_process.html">18.x</a></li> 120<li><a href="https://nodejs.org/docs/latest-v17.x/api/child_process.html">17.x</a></li> 121<li><a href="https://nodejs.org/docs/latest-v16.x/api/child_process.html">16.x <b>LTS</b></a></li> 122<li><a href="https://nodejs.org/docs/latest-v15.x/api/child_process.html">15.x</a></li> 123<li><a href="https://nodejs.org/docs/latest-v14.x/api/child_process.html">14.x <b>LTS</b></a></li> 124<li><a href="https://nodejs.org/docs/latest-v13.x/api/child_process.html">13.x</a></li> 125<li><a href="https://nodejs.org/docs/latest-v12.x/api/child_process.html">12.x <b>LTS</b></a></li> 126<li><a href="https://nodejs.org/docs/latest-v11.x/api/child_process.html">11.x</a></li> 127<li><a href="https://nodejs.org/docs/latest-v10.x/api/child_process.html">10.x</a></li> 128<li><a href="https://nodejs.org/docs/latest-v9.x/api/child_process.html">9.x</a></li> 129<li><a href="https://nodejs.org/docs/latest-v8.x/api/child_process.html">8.x</a></li> 130<li><a href="https://nodejs.org/docs/latest-v7.x/api/child_process.html">7.x</a></li> 131<li><a href="https://nodejs.org/docs/latest-v6.x/api/child_process.html">6.x</a></li> 132<li><a href="https://nodejs.org/docs/latest-v5.x/api/child_process.html">5.x</a></li> 133<li><a href="https://nodejs.org/docs/latest-v4.x/api/child_process.html">4.x</a></li> 134<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/child_process.html">0.12.x</a></li> 135<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/child_process.html">0.10.x</a></li></ol> 136 </li> 137 138 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/child_process.md">Edit on GitHub</a></li> 139 </ul> 140 </div> 141 <hr> 142 </header> 143 144 <details id="toc" open><summary>Table of contents</summary><ul> 145<li><span class="stability_2"><a href="#child_process_child_process">Child process</a></span> 146<ul> 147<li><a href="#child_process_asynchronous_process_creation">Asynchronous process creation</a> 148<ul> 149<li><a href="#child_process_spawning_bat_and_cmd_files_on_windows">Spawning <code>.bat</code> and <code>.cmd</code> files on Windows</a></li> 150<li><a href="#child_process_child_process_exec_command_options_callback"><code>child_process.exec(command[, options][, callback])</code></a></li> 151<li><a href="#child_process_child_process_execfile_file_args_options_callback"><code>child_process.execFile(file[, args][, options][, callback])</code></a></li> 152<li><a href="#child_process_child_process_fork_modulepath_args_options"><code>child_process.fork(modulePath[, args][, options])</code></a></li> 153<li><a href="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn(command[, args][, options])</code></a> 154<ul> 155<li><a href="#child_process_options_detached"><code>options.detached</code></a></li> 156<li><a href="#child_process_options_stdio"><code>options.stdio</code></a></li> 157</ul> 158</li> 159</ul> 160</li> 161<li><a href="#child_process_synchronous_process_creation">Synchronous process creation</a> 162<ul> 163<li><a href="#child_process_child_process_execfilesync_file_args_options"><code>child_process.execFileSync(file[, args][, options])</code></a></li> 164<li><a href="#child_process_child_process_execsync_command_options"><code>child_process.execSync(command[, options])</code></a></li> 165<li><a href="#child_process_child_process_spawnsync_command_args_options"><code>child_process.spawnSync(command[, args][, options])</code></a></li> 166</ul> 167</li> 168<li><a href="#child_process_class_childprocess">Class: <code>ChildProcess</code></a> 169<ul> 170<li><a href="#child_process_event_close">Event: <code>'close'</code></a></li> 171<li><a href="#child_process_event_disconnect">Event: <code>'disconnect'</code></a></li> 172<li><a href="#child_process_event_error">Event: <code>'error'</code></a></li> 173<li><a href="#child_process_event_exit">Event: <code>'exit'</code></a></li> 174<li><a href="#child_process_event_message">Event: <code>'message'</code></a></li> 175<li><a href="#child_process_event_spawn">Event: <code>'spawn'</code></a></li> 176<li><a href="#child_process_subprocess_channel"><code>subprocess.channel</code></a> 177<ul> 178<li><a href="#child_process_subprocess_channel_ref"><code>subprocess.channel.ref()</code></a></li> 179<li><a href="#child_process_subprocess_channel_unref"><code>subprocess.channel.unref()</code></a></li> 180</ul> 181</li> 182<li><a href="#child_process_subprocess_connected"><code>subprocess.connected</code></a></li> 183<li><a href="#child_process_subprocess_disconnect"><code>subprocess.disconnect()</code></a></li> 184<li><a href="#child_process_subprocess_exitcode"><code>subprocess.exitCode</code></a></li> 185<li><a href="#child_process_subprocess_kill_signal"><code>subprocess.kill([signal])</code></a></li> 186<li><a href="#child_process_subprocess_killed"><code>subprocess.killed</code></a></li> 187<li><a href="#child_process_subprocess_pid"><code>subprocess.pid</code></a></li> 188<li><a href="#child_process_subprocess_ref"><code>subprocess.ref()</code></a></li> 189<li><a href="#child_process_subprocess_send_message_sendhandle_options_callback"><code>subprocess.send(message[, sendHandle[, options]][, callback])</code></a> 190<ul> 191<li><a href="#child_process_example_sending_a_server_object">Example: sending a server object</a></li> 192<li><a href="#child_process_example_sending_a_socket_object">Example: sending a socket object</a></li> 193</ul> 194</li> 195<li><a href="#child_process_subprocess_signalcode"><code>subprocess.signalCode</code></a></li> 196<li><a href="#child_process_subprocess_spawnargs"><code>subprocess.spawnargs</code></a></li> 197<li><a href="#child_process_subprocess_spawnfile"><code>subprocess.spawnfile</code></a></li> 198<li><a href="#child_process_subprocess_stderr"><code>subprocess.stderr</code></a></li> 199<li><a href="#child_process_subprocess_stdin"><code>subprocess.stdin</code></a></li> 200<li><a href="#child_process_subprocess_stdio"><code>subprocess.stdio</code></a></li> 201<li><a href="#child_process_subprocess_stdout"><code>subprocess.stdout</code></a></li> 202<li><a href="#child_process_subprocess_unref"><code>subprocess.unref()</code></a></li> 203</ul> 204</li> 205<li><a href="#child_process_maxbuffer_and_unicode"><code>maxBuffer</code> and Unicode</a></li> 206<li><a href="#child_process_shell_requirements">Shell requirements</a></li> 207<li><a href="#child_process_default_windows_shell">Default Windows shell</a></li> 208<li><a href="#child_process_advanced_serialization">Advanced serialization</a></li> 209</ul> 210</li> 211</ul></details> 212 213 <div id="apicontent"> 214 <h2>Child process<span><a class="mark" href="#child_process_child_process" id="child_process_child_process">#</a></span></h2> 215 216<p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p> 217<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v14.20.1/lib/child_process.js">lib/child_process.js</a></p> 218<p>The <code>child_process</code> module provides the ability to spawn subprocesses in 219a manner that is similar, but not identical, to <a href="http://man7.org/linux/man-pages/man3/popen.3.html"><code>popen(3)</code></a>. This capability 220is primarily provided by the <a href="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a> function:</p> 221<pre><code class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>); 222<span class="hljs-keyword">const</span> ls = <span class="hljs-title function_">spawn</span>(<span class="hljs-string">'ls'</span>, [<span class="hljs-string">'-lh'</span>, <span class="hljs-string">'/usr'</span>]); 223 224ls.<span class="hljs-property">stdout</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> { 225 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`stdout: <span class="hljs-subst">${data}</span>`</span>); 226}); 227 228ls.<span class="hljs-property">stderr</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> { 229 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`stderr: <span class="hljs-subst">${data}</span>`</span>); 230}); 231 232ls.<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-function">(<span class="hljs-params">code</span>) =></span> { 233 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`child process exited with code <span class="hljs-subst">${code}</span>`</span>); 234});</code></pre> 235<p>By default, pipes for <code>stdin</code>, <code>stdout</code>, and <code>stderr</code> are established between 236the parent Node.js process and the spawned subprocess. These pipes have 237limited (and platform-specific) capacity. If the subprocess writes to 238stdout in excess of that limit without the output being captured, the 239subprocess blocks waiting for the pipe buffer to accept more data. This is 240identical to the behavior of pipes in the shell. Use the <code>{ stdio: 'ignore' }</code> 241option if the output will not be consumed.</p> 242<p>The command lookup is performed using the <code>options.env.PATH</code> environment 243variable if it is in the <code>options</code> object. Otherwise, <code>process.env.PATH</code> is 244used.</p> 245<p>On Windows, environment variables are case-insensitive. Node.js 246lexicographically sorts the <code>env</code> keys and uses the first one that 247case-insensitively matches. Only first (in lexicographic order) entry will be 248passed to the subprocess. This might lead to issues on Windows when passing 249objects to the <code>env</code> option that have multiple variants of the same key, such as 250<code>PATH</code> and <code>Path</code>.</p> 251<p>The <a href="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a> method spawns the child process asynchronously, 252without blocking the Node.js event loop. The <a href="#child_process_child_process_spawnsync_command_args_options"><code>child_process.spawnSync()</code></a> 253function provides equivalent functionality in a synchronous manner that blocks 254the event loop until the spawned process either exits or is terminated.</p> 255<p>For convenience, the <code>child_process</code> module provides a handful of synchronous 256and asynchronous alternatives to <a href="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a> and 257<a href="#child_process_child_process_spawnsync_command_args_options"><code>child_process.spawnSync()</code></a>. Each of these alternatives are implemented on 258top of <a href="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a> or <a href="#child_process_child_process_spawnsync_command_args_options"><code>child_process.spawnSync()</code></a>.</p> 259<ul> 260<li><a href="#child_process_child_process_exec_command_options_callback"><code>child_process.exec()</code></a>: spawns a shell and runs a command within that 261shell, passing the <code>stdout</code> and <code>stderr</code> to a callback function when 262complete.</li> 263<li><a href="#child_process_child_process_execfile_file_args_options_callback"><code>child_process.execFile()</code></a>: similar to <a href="#child_process_child_process_exec_command_options_callback"><code>child_process.exec()</code></a> except 264that it spawns the command directly without first spawning a shell by 265default.</li> 266<li><a href="#child_process_child_process_fork_modulepath_args_options"><code>child_process.fork()</code></a>: spawns a new Node.js process and invokes a 267specified module with an IPC communication channel established that allows 268sending messages between parent and child.</li> 269<li><a href="#child_process_child_process_execsync_command_options"><code>child_process.execSync()</code></a>: a synchronous version of 270<a href="#child_process_child_process_exec_command_options_callback"><code>child_process.exec()</code></a> that will block the Node.js event loop.</li> 271<li><a href="#child_process_child_process_execfilesync_file_args_options"><code>child_process.execFileSync()</code></a>: a synchronous version of 272<a href="#child_process_child_process_execfile_file_args_options_callback"><code>child_process.execFile()</code></a> that will block the Node.js event loop.</li> 273</ul> 274<p>For certain use cases, such as automating shell scripts, the 275<a href="#child_process_synchronous_process_creation">synchronous counterparts</a> may be more convenient. In many cases, however, 276the synchronous methods can have significant impact on performance due to 277stalling the event loop while spawned processes complete.</p> 278<section><h3>Asynchronous process creation<span><a class="mark" href="#child_process_asynchronous_process_creation" id="child_process_asynchronous_process_creation">#</a></span></h3> 279<p>The <a href="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a>, <a href="#child_process_child_process_fork_modulepath_args_options"><code>child_process.fork()</code></a>, <a href="#child_process_child_process_exec_command_options_callback"><code>child_process.exec()</code></a>, 280and <a href="#child_process_child_process_execfile_file_args_options_callback"><code>child_process.execFile()</code></a> methods all follow the idiomatic asynchronous 281programming pattern typical of other Node.js APIs.</p> 282<p>Each of the methods returns a <a href="#child_process_class_childprocess"><code>ChildProcess</code></a> instance. These objects 283implement the Node.js <a href="events.html#events_class_eventemitter"><code>EventEmitter</code></a> API, allowing the parent process to 284register listener functions that are called when certain events occur during 285the life cycle of the child process.</p> 286<p>The <a href="#child_process_child_process_exec_command_options_callback"><code>child_process.exec()</code></a> and <a href="#child_process_child_process_execfile_file_args_options_callback"><code>child_process.execFile()</code></a> methods 287additionally allow for an optional <code>callback</code> function to be specified that is 288invoked when the child process terminates.</p> 289<h4>Spawning <code>.bat</code> and <code>.cmd</code> files on Windows<span><a class="mark" href="#child_process_spawning_bat_and_cmd_files_on_windows" id="child_process_spawning_bat_and_cmd_files_on_windows">#</a></span></h4> 290<p>The importance of the distinction between <a href="#child_process_child_process_exec_command_options_callback"><code>child_process.exec()</code></a> and 291<a href="#child_process_child_process_execfile_file_args_options_callback"><code>child_process.execFile()</code></a> can vary based on platform. On Unix-type 292operating systems (Unix, Linux, macOS) <a href="#child_process_child_process_execfile_file_args_options_callback"><code>child_process.execFile()</code></a> can be 293more efficient because it does not spawn a shell by default. On Windows, 294however, <code>.bat</code> and <code>.cmd</code> files are not executable on their own without a 295terminal, and therefore cannot be launched using <a href="#child_process_child_process_execfile_file_args_options_callback"><code>child_process.execFile()</code></a>. 296When running on Windows, <code>.bat</code> and <code>.cmd</code> files can be invoked using 297<a href="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a> with the <code>shell</code> option set, with 298<a href="#child_process_child_process_exec_command_options_callback"><code>child_process.exec()</code></a>, or by spawning <code>cmd.exe</code> and passing the <code>.bat</code> or 299<code>.cmd</code> file as an argument (which is what the <code>shell</code> option and 300<a href="#child_process_child_process_exec_command_options_callback"><code>child_process.exec()</code></a> do). In any case, if the script filename contains 301spaces it needs to be quoted.</p> 302<pre><code class="language-js"><span class="hljs-comment">// On Windows Only...</span> 303<span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>); 304<span class="hljs-keyword">const</span> bat = <span class="hljs-title function_">spawn</span>(<span class="hljs-string">'cmd.exe'</span>, [<span class="hljs-string">'/c'</span>, <span class="hljs-string">'my.bat'</span>]); 305 306bat.<span class="hljs-property">stdout</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> { 307 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(data.<span class="hljs-title function_">toString</span>()); 308}); 309 310bat.<span class="hljs-property">stderr</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> { 311 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(data.<span class="hljs-title function_">toString</span>()); 312}); 313 314bat.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">(<span class="hljs-params">code</span>) =></span> { 315 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Child exited with code <span class="hljs-subst">${code}</span>`</span>); 316});</code></pre> 317<pre><code class="language-js"><span class="hljs-comment">// OR...</span> 318<span class="hljs-keyword">const</span> { exec, spawn } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>); 319<span class="hljs-title function_">exec</span>(<span class="hljs-string">'my.bat'</span>, <span class="hljs-function">(<span class="hljs-params">err, stdout, stderr</span>) =></span> { 320 <span class="hljs-keyword">if</span> (err) { 321 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err); 322 <span class="hljs-keyword">return</span>; 323 } 324 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(stdout); 325}); 326 327<span class="hljs-comment">// Script with spaces in the filename:</span> 328<span class="hljs-keyword">const</span> bat = <span class="hljs-title function_">spawn</span>(<span class="hljs-string">'"my script.cmd"'</span>, [<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>], { <span class="hljs-attr">shell</span>: <span class="hljs-literal">true</span> }); 329<span class="hljs-comment">// or:</span> 330<span class="hljs-title function_">exec</span>(<span class="hljs-string">'"my script.cmd" a b'</span>, <span class="hljs-function">(<span class="hljs-params">err, stdout, stderr</span>) =></span> { 331 <span class="hljs-comment">// ...</span> 332});</code></pre> 333<h4><code>child_process.exec(command[, options][, callback])</code><span><a class="mark" href="#child_process_child_process_exec_command_options_callback" id="child_process_child_process_exec_command_options_callback">#</a></span></h4> 334<div class="api_metadata"> 335<details class="changelog"><summary>History</summary> 336<table> 337<tbody><tr><th>Version</th><th>Changes</th></tr> 338<tr><td>v14.18.0</td> 339<td><p>The <code>cwd</code> option can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 340<tr><td>v14.17.0</td> 341<td><p>AbortSignal support was added.</p></td></tr> 342<tr><td>v8.8.0</td> 343<td><p>The <code>windowsHide</code> option is supported now.</p></td></tr> 344<tr><td>v0.1.90</td> 345<td><p><span>Added in: v0.1.90</span></p></td></tr> 346</tbody></table> 347</details> 348</div> 349<ul> 350<li><code>command</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The command to run, with space-separated arguments.</li> 351<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 352<ul> 353<li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a> Current working directory of the child process. 354<strong>Default:</strong> <code>process.cwd()</code>.</li> 355<li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Environment key-value pairs. <strong>Default:</strong> <code>process.env</code>.</li> 356<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> 357<li><code>shell</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Shell to execute the command with. See 358<a href="#child_process_shell_requirements">Shell requirements</a> and <a href="#child_process_default_windows_shell">Default Windows shell</a>. <strong>Default:</strong> 359<code>'/bin/sh'</code> on Unix, <code>process.env.ComSpec</code> on Windows.</li> 360<li><code>signal</code> <a href="globals.html#globals_class_abortsignal" class="type"><AbortSignal></a> allows aborting the child process using an 361AbortSignal.</li> 362<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> <strong>Default:</strong> <code>0</code></li> 363<li><code>maxBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Largest amount of data in bytes allowed on stdout or 364stderr. If exceeded, the child process is terminated and any output is 365truncated. See caveat at <a href="#child_process_maxbuffer_and_unicode"><code>maxBuffer</code> and Unicode</a>. 366<strong>Default:</strong> <code>1024 * 1024</code>.</li> 367<li><code>killSignal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>'SIGTERM'</code></li> 368<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the user identity of the process (see <a href="http://man7.org/linux/man-pages/man2/setuid.2.html"><code>setuid(2)</code></a>).</li> 369<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the group identity of the process (see <a href="http://man7.org/linux/man-pages/man2/setgid.2.html"><code>setgid(2)</code></a>).</li> 370<li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Hide the subprocess console window that would 371normally be created on Windows systems. <strong>Default:</strong> <code>false</code>.</li> 372</ul> 373</li> 374<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> called with the output when process terminates. 375<ul> 376<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 377<li><code>stdout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> 378<li><code>stderr</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> 379</ul> 380</li> 381<li>Returns: <a href="child_process.html#child_process_class_childprocess" class="type"><ChildProcess></a></li> 382</ul> 383<p>Spawns a shell then executes the <code>command</code> within that shell, buffering any 384generated output. The <code>command</code> string passed to the exec function is processed 385directly by the shell and special characters (vary based on 386<a href="https://en.wikipedia.org/wiki/List_of_command-line_interpreters">shell</a>) 387need to be dealt with accordingly:</p> 388<pre><code class="language-js"><span class="hljs-keyword">const</span> { exec } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>); 389 390<span class="hljs-title function_">exec</span>(<span class="hljs-string">'"/path/to/test file/test.sh" arg1 arg2'</span>); 391<span class="hljs-comment">// Double quotes are used so that the space in the path is not interpreted as</span> 392<span class="hljs-comment">// a delimiter of multiple arguments.</span> 393 394<span class="hljs-title function_">exec</span>(<span class="hljs-string">'echo "The \\$HOME variable is $HOME"'</span>); 395<span class="hljs-comment">// The $HOME variable is escaped in the first instance, but not in the second.</span></code></pre> 396<p><strong>Never pass unsanitized user input to this function. Any input containing shell 397metacharacters may be used to trigger arbitrary command execution.</strong></p> 398<p>If a <code>callback</code> function is provided, it is called with the arguments 399<code>(error, stdout, stderr)</code>. On success, <code>error</code> will be <code>null</code>. On error, 400<code>error</code> will be an instance of <a href="errors.html#errors_class_error"><code>Error</code></a>. The <code>error.code</code> property will be 401the exit code of the process. By convention, any exit code other than <code>0</code> 402indicates an error. <code>error.signal</code> will be the signal that terminated the 403process.</p> 404<p>The <code>stdout</code> and <code>stderr</code> arguments passed to the callback will contain the 405stdout and stderr output of the child process. By default, Node.js will decode 406the output as UTF-8 and pass strings to the callback. The <code>encoding</code> option 407can be used to specify the character encoding used to decode the stdout and 408stderr output. If <code>encoding</code> is <code>'buffer'</code>, or an unrecognized character 409encoding, <code>Buffer</code> objects will be passed to the callback instead.</p> 410<pre><code class="language-js"><span class="hljs-keyword">const</span> { exec } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>); 411<span class="hljs-title function_">exec</span>(<span class="hljs-string">'cat *.js missing_file | wc -l'</span>, <span class="hljs-function">(<span class="hljs-params">error, stdout, stderr</span>) =></span> { 412 <span class="hljs-keyword">if</span> (error) { 413 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`exec error: <span class="hljs-subst">${error}</span>`</span>); 414 <span class="hljs-keyword">return</span>; 415 } 416 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`stdout: <span class="hljs-subst">${stdout}</span>`</span>); 417 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`stderr: <span class="hljs-subst">${stderr}</span>`</span>); 418});</code></pre> 419<p>If <code>timeout</code> is greater than <code>0</code>, the parent will send the signal 420identified by the <code>killSignal</code> property (the default is <code>'SIGTERM'</code>) if the 421child runs longer than <code>timeout</code> milliseconds.</p> 422<p>Unlike the <a href="http://man7.org/linux/man-pages/man3/exec.3.html"><code>exec(3)</code></a> POSIX system call, <code>child_process.exec()</code> does not replace 423the existing process and uses a shell to execute the command.</p> 424<p>If this method is invoked as its <a href="util.html#util_util_promisify_original"><code>util.promisify()</code></a>ed version, it returns 425a <code>Promise</code> for an <code>Object</code> with <code>stdout</code> and <code>stderr</code> properties. The returned 426<code>ChildProcess</code> instance is attached to the <code>Promise</code> as a <code>child</code> property. In 427case of an error (including any error resulting in an exit code other than 0), a 428rejected promise is returned, with the same <code>error</code> object given in the 429callback, but with two additional properties <code>stdout</code> and <code>stderr</code>.</p> 430<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>); 431<span class="hljs-keyword">const</span> exec = util.<span class="hljs-title function_">promisify</span>(<span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>).<span class="hljs-property">exec</span>); 432 433<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">lsExample</span>(<span class="hljs-params"></span>) { 434 <span class="hljs-keyword">const</span> { stdout, stderr } = <span class="hljs-keyword">await</span> <span class="hljs-title function_">exec</span>(<span class="hljs-string">'ls'</span>); 435 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'stdout:'</span>, stdout); 436 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'stderr:'</span>, stderr); 437} 438<span class="hljs-title function_">lsExample</span>();</code></pre> 439<p>If the <code>signal</code> option is enabled, calling <code>.abort()</code> on the corresponding 440<code>AbortController</code> is similar to calling <code>.kill()</code> on the child process except 441the error passed to the callback will be an <code>AbortError</code>:</p> 442<pre><code class="language-js"><span class="hljs-keyword">const</span> { exec } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>); 443<span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); 444<span class="hljs-keyword">const</span> { signal } = controller; 445<span class="hljs-keyword">const</span> child = <span class="hljs-title function_">exec</span>(<span class="hljs-string">'grep ssh'</span>, { signal }, <span class="hljs-function">(<span class="hljs-params">error</span>) =></span> { 446 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(error); <span class="hljs-comment">// an AbortError</span> 447}); 448controller.<span class="hljs-title function_">abort</span>();</code></pre> 449<h4><code>child_process.execFile(file[, args][, options][, callback])</code><span><a class="mark" href="#child_process_child_process_execfile_file_args_options_callback" id="child_process_child_process_execfile_file_args_options_callback">#</a></span></h4> 450<div class="api_metadata"> 451<details class="changelog"><summary>History</summary> 452<table> 453<tbody><tr><th>Version</th><th>Changes</th></tr> 454<tr><td>v14.18.0</td> 455<td><p>The <code>cwd</code> option can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 456<tr><td>v14.17.0</td> 457<td><p>AbortSignal support was added.</p></td></tr> 458<tr><td>v8.8.0</td> 459<td><p>The <code>windowsHide</code> option is supported now.</p></td></tr> 460<tr><td>v0.1.91</td> 461<td><p><span>Added in: v0.1.91</span></p></td></tr> 462</tbody></table> 463</details> 464</div> 465<ul> 466<li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The name or path of the executable file to run.</li> 467<li><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> List of string arguments.</li> 468<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 469<ul> 470<li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a> Current working directory of the child process.</li> 471<li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Environment key-value pairs. <strong>Default:</strong> <code>process.env</code>.</li> 472<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> 473<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> <strong>Default:</strong> <code>0</code></li> 474<li><code>maxBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Largest amount of data in bytes allowed on stdout or 475stderr. If exceeded, the child process is terminated and any output is 476truncated. See caveat at <a href="#child_process_maxbuffer_and_unicode"><code>maxBuffer</code> and Unicode</a>. 477<strong>Default:</strong> <code>1024 * 1024</code>.</li> 478<li><code>killSignal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>'SIGTERM'</code></li> 479<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the user identity of the process (see <a href="http://man7.org/linux/man-pages/man2/setuid.2.html"><code>setuid(2)</code></a>).</li> 480<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the group identity of the process (see <a href="http://man7.org/linux/man-pages/man2/setgid.2.html"><code>setgid(2)</code></a>).</li> 481<li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Hide the subprocess console window that would 482normally be created on Windows systems. <strong>Default:</strong> <code>false</code>.</li> 483<li><code>windowsVerbatimArguments</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> No quoting or escaping of arguments is 484done on Windows. Ignored on Unix. <strong>Default:</strong> <code>false</code>.</li> 485<li><code>shell</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> If <code>true</code>, runs <code>command</code> inside of a shell. Uses 486<code>'/bin/sh'</code> on Unix, and <code>process.env.ComSpec</code> on Windows. A different 487shell can be specified as a string. See <a href="#child_process_shell_requirements">Shell requirements</a> and 488<a href="#child_process_default_windows_shell">Default Windows shell</a>. <strong>Default:</strong> <code>false</code> (no shell).</li> 489<li><code>signal</code> <a href="globals.html#globals_class_abortsignal" class="type"><AbortSignal></a> allows aborting the child process using an 490AbortSignal.</li> 491</ul> 492</li> 493<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Called with the output when process terminates. 494<ul> 495<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 496<li><code>stdout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> 497<li><code>stderr</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> 498</ul> 499</li> 500<li>Returns: <a href="child_process.html#child_process_class_childprocess" class="type"><ChildProcess></a></li> 501</ul> 502<p>The <code>child_process.execFile()</code> function is similar to <a href="#child_process_child_process_exec_command_options_callback"><code>child_process.exec()</code></a> 503except that it does not spawn a shell by default. Rather, the specified 504executable <code>file</code> is spawned directly as a new process making it slightly more 505efficient than <a href="#child_process_child_process_exec_command_options_callback"><code>child_process.exec()</code></a>.</p> 506<p>The same options as <a href="#child_process_child_process_exec_command_options_callback"><code>child_process.exec()</code></a> are supported. Since a shell is 507not spawned, behaviors such as I/O redirection and file globbing are not 508supported.</p> 509<pre><code class="language-js"><span class="hljs-keyword">const</span> { execFile } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>); 510<span class="hljs-keyword">const</span> child = <span class="hljs-title function_">execFile</span>(<span class="hljs-string">'node'</span>, [<span class="hljs-string">'--version'</span>], <span class="hljs-function">(<span class="hljs-params">error, stdout, stderr</span>) =></span> { 511 <span class="hljs-keyword">if</span> (error) { 512 <span class="hljs-keyword">throw</span> error; 513 } 514 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(stdout); 515});</code></pre> 516<p>The <code>stdout</code> and <code>stderr</code> arguments passed to the callback will contain the 517stdout and stderr output of the child process. By default, Node.js will decode 518the output as UTF-8 and pass strings to the callback. The <code>encoding</code> option 519can be used to specify the character encoding used to decode the stdout and 520stderr output. If <code>encoding</code> is <code>'buffer'</code>, or an unrecognized character 521encoding, <code>Buffer</code> objects will be passed to the callback instead.</p> 522<p>If this method is invoked as its <a href="util.html#util_util_promisify_original"><code>util.promisify()</code></a>ed version, it returns 523a <code>Promise</code> for an <code>Object</code> with <code>stdout</code> and <code>stderr</code> properties. The returned 524<code>ChildProcess</code> instance is attached to the <code>Promise</code> as a <code>child</code> property. In 525case of an error (including any error resulting in an exit code other than 0), a 526rejected promise is returned, with the same <code>error</code> object given in the 527callback, but with two additional properties <code>stdout</code> and <code>stderr</code>.</p> 528<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>); 529<span class="hljs-keyword">const</span> execFile = util.<span class="hljs-title function_">promisify</span>(<span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>).<span class="hljs-property">execFile</span>); 530<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">getVersion</span>(<span class="hljs-params"></span>) { 531 <span class="hljs-keyword">const</span> { stdout } = <span class="hljs-keyword">await</span> <span class="hljs-title function_">execFile</span>(<span class="hljs-string">'node'</span>, [<span class="hljs-string">'--version'</span>]); 532 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(stdout); 533} 534<span class="hljs-title function_">getVersion</span>();</code></pre> 535<p><strong>If the <code>shell</code> option is enabled, do not pass unsanitized user input to this 536function. Any input containing shell metacharacters may be used to trigger 537arbitrary command execution.</strong></p> 538<p>If the <code>signal</code> option is enabled, calling <code>.abort()</code> on the corresponding 539<code>AbortController</code> is similar to calling <code>.kill()</code> on the child process except 540the error passed to the callback will be an <code>AbortError</code>:</p> 541<pre><code class="language-js"><span class="hljs-keyword">const</span> { execFile } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>); 542<span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); 543<span class="hljs-keyword">const</span> { signal } = controller; 544<span class="hljs-keyword">const</span> child = <span class="hljs-title function_">execFile</span>(<span class="hljs-string">'node'</span>, [<span class="hljs-string">'--version'</span>], { signal }, <span class="hljs-function">(<span class="hljs-params">error</span>) =></span> { 545 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(error); <span class="hljs-comment">// an AbortError</span> 546}); 547controller.<span class="hljs-title function_">abort</span>();</code></pre> 548<h4><code>child_process.fork(modulePath[, args][, options])</code><span><a class="mark" href="#child_process_child_process_fork_modulepath_args_options" id="child_process_child_process_fork_modulepath_args_options">#</a></span></h4> 549<div class="api_metadata"> 550<details class="changelog"><summary>History</summary> 551<table> 552<tbody><tr><th>Version</th><th>Changes</th></tr> 553<tr><td>v14.18.0</td> 554<td><p>The <code>cwd</code> option can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 555<tr><td>v14.18.0</td> 556<td><p>timeout was added.</p></td></tr> 557<tr><td>v14.18.0</td> 558<td><p>killSignal for AbortSignal was added.</p></td></tr> 559<tr><td>v14.17.0</td> 560<td><p>AbortSignal support was added.</p></td></tr> 561<tr><td>v13.2.0, v12.16.0</td> 562<td><p>The <code>serialization</code> option is supported now.</p></td></tr> 563<tr><td>v8.0.0</td> 564<td><p>The <code>stdio</code> option can now be a string.</p></td></tr> 565<tr><td>v6.4.0</td> 566<td><p>The <code>stdio</code> option is supported now.</p></td></tr> 567<tr><td>v0.5.0</td> 568<td><p><span>Added in: v0.5.0</span></p></td></tr> 569</tbody></table> 570</details> 571</div> 572<ul> 573<li><code>modulePath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The module to run in the child.</li> 574<li><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> List of string arguments.</li> 575<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 576<ul> 577<li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a> Current working directory of the child process.</li> 578<li><code>detached</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Prepare child to run independently of its parent 579process. Specific behavior depends on the platform, see 580<a href="#child_process_options_detached"><code>options.detached</code></a>).</li> 581<li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Environment key-value pairs. <strong>Default:</strong> <code>process.env</code>.</li> 582<li><code>execPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Executable used to create the child process.</li> 583<li><code>execArgv</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> List of string arguments passed to the executable. 584<strong>Default:</strong> <code>process.execArgv</code>.</li> 585<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the group identity of the process (see <a href="http://man7.org/linux/man-pages/man2/setgid.2.html"><code>setgid(2)</code></a>).</li> 586<li><code>serialization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Specify the kind of serialization used for sending 587messages between processes. Possible values are <code>'json'</code> and <code>'advanced'</code>. 588See <a href="#child_process_advanced_serialization">Advanced serialization</a> for more details. <strong>Default:</strong> <code>'json'</code>.</li> 589<li><code>signal</code> <a href="globals.html#globals_class_abortsignal" class="type"><AbortSignal></a> Allows closing the child process using an 590AbortSignal.</li> 591<li><code>killSignal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The signal value to be used when the spawned 592process will be killed by timeout or abort signal. <strong>Default:</strong> <code>'SIGTERM'</code>.</li> 593<li><code>silent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, stdin, stdout, and stderr of the child will be 594piped to the parent, otherwise they will be inherited from the parent, see 595the <code>'pipe'</code> and <code>'inherit'</code> options for <a href="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a>'s 596<a href="#child_process_options_stdio"><code>stdio</code></a> for more details. <strong>Default:</strong> <code>false</code>.</li> 597<li><code>stdio</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a>'s <a href="#child_process_options_stdio"><code>stdio</code></a>. 598When this option is provided, it overrides <code>silent</code>. If the array variant 599is used, it must contain exactly one item with value <code>'ipc'</code> or an error 600will be thrown. For instance <code>[0, 1, 2, 'ipc']</code>.</li> 601<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the user identity of the process (see <a href="http://man7.org/linux/man-pages/man2/setuid.2.html"><code>setuid(2)</code></a>).</li> 602<li><code>windowsVerbatimArguments</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> No quoting or escaping of arguments is 603done on Windows. Ignored on Unix. <strong>Default:</strong> <code>false</code>.</li> 604<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> In milliseconds the maximum amount of time the process 605is allowed to run. <strong>Default:</strong> <code>undefined</code>.</li> 606</ul> 607</li> 608<li>Returns: <a href="child_process.html#child_process_class_childprocess" class="type"><ChildProcess></a></li> 609</ul> 610<p>The <code>child_process.fork()</code> method is a special case of 611<a href="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a> used specifically to spawn new Node.js processes. 612Like <a href="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a>, a <a href="#child_process_class_childprocess"><code>ChildProcess</code></a> object is returned. The 613returned <a href="#child_process_class_childprocess"><code>ChildProcess</code></a> will have an additional communication channel 614built-in that allows messages to be passed back and forth between the parent and 615child. See <a href="#child_process_subprocess_send_message_sendhandle_options_callback"><code>subprocess.send()</code></a> for details.</p> 616<p>Keep in mind that spawned Node.js child processes are 617independent of the parent with exception of the IPC communication channel 618that is established between the two. Each process has its own memory, with 619their own V8 instances. Because of the additional resource allocations 620required, spawning a large number of child Node.js processes is not 621recommended.</p> 622<p>By default, <code>child_process.fork()</code> will spawn new Node.js instances using the 623<a href="process.html#process_process_execpath"><code>process.execPath</code></a> of the parent process. The <code>execPath</code> property in the 624<code>options</code> object allows for an alternative execution path to be used.</p> 625<p>Node.js processes launched with a custom <code>execPath</code> will communicate with the 626parent process using the file descriptor (fd) identified using the 627environment variable <code>NODE_CHANNEL_FD</code> on the child process.</p> 628<p>Unlike the <a href="http://man7.org/linux/man-pages/man2/fork.2.html"><code>fork(2)</code></a> POSIX system call, <code>child_process.fork()</code> does not clone the 629current process.</p> 630<p>The <code>shell</code> option available in <a href="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a> is not supported by 631<code>child_process.fork()</code> and will be ignored if set.</p> 632<p>If the <code>signal</code> option is enabled, calling <code>.abort()</code> on the corresponding 633<code>AbortController</code> is similar to calling <code>.kill()</code> on the child process except 634the error passed to the callback will be an <code>AbortError</code>:</p> 635<pre><code class="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-property">argv</span>[<span class="hljs-number">2</span>] === <span class="hljs-string">'child'</span>) { 636 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> { 637 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Hello from <span class="hljs-subst">${process.argv[<span class="hljs-number">2</span>]}</span>!`</span>); 638 }, <span class="hljs-number">1_000</span>); 639} <span class="hljs-keyword">else</span> { 640 <span class="hljs-keyword">const</span> { fork } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>); 641 <span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); 642 <span class="hljs-keyword">const</span> { signal } = controller; 643 <span class="hljs-keyword">const</span> child = <span class="hljs-title function_">fork</span>(__filename, [<span class="hljs-string">'child'</span>], { signal }); 644 child.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 645 <span class="hljs-comment">// This will be called with err being an AbortError if the controller aborts</span> 646 }); 647 controller.<span class="hljs-title function_">abort</span>(); <span class="hljs-comment">// Stops the child process</span> 648}</code></pre> 649<h4><code>child_process.spawn(command[, args][, options])</code><span><a class="mark" href="#child_process_child_process_spawn_command_args_options" id="child_process_child_process_spawn_command_args_options">#</a></span></h4> 650<div class="api_metadata"> 651<details class="changelog"><summary>History</summary> 652<table> 653<tbody><tr><th>Version</th><th>Changes</th></tr> 654<tr><td>v14.18.0</td> 655<td><p>The <code>cwd</code> option can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 656<tr><td>v14.18.0</td> 657<td><p>timeout was added.</p></td></tr> 658<tr><td>v14.18.0</td> 659<td><p>killSignal for AbortSignal was added.</p></td></tr> 660<tr><td>v14.17.0</td> 661<td><p>AbortSignal support was added.</p></td></tr> 662<tr><td>v13.2.0, v12.16.0</td> 663<td><p>The <code>serialization</code> option is supported now.</p></td></tr> 664<tr><td>v8.8.0</td> 665<td><p>The <code>windowsHide</code> option is supported now.</p></td></tr> 666<tr><td>v6.4.0</td> 667<td><p>The <code>argv0</code> option is supported now.</p></td></tr> 668<tr><td>v5.7.0</td> 669<td><p>The <code>shell</code> option is supported now.</p></td></tr> 670<tr><td>v0.1.90</td> 671<td><p><span>Added in: v0.1.90</span></p></td></tr> 672</tbody></table> 673</details> 674</div> 675<ul> 676<li> 677<p><code>command</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The command to run.</p> 678</li> 679<li> 680<p><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> List of string arguments.</p> 681</li> 682<li> 683<p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></p> 684<ul> 685<li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a> Current working directory of the child process.</li> 686<li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Environment key-value pairs. <strong>Default:</strong> <code>process.env</code>.</li> 687<li><code>argv0</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Explicitly set the value of <code>argv[0]</code> sent to the child 688process. This will be set to <code>command</code> if not specified.</li> 689<li><code>stdio</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Child's stdio configuration (see 690<a href="#child_process_options_stdio"><code>options.stdio</code></a>).</li> 691<li><code>detached</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Prepare child to run independently of its parent 692process. Specific behavior depends on the platform, see 693<a href="#child_process_options_detached"><code>options.detached</code></a>).</li> 694<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the user identity of the process (see <a href="http://man7.org/linux/man-pages/man2/setuid.2.html"><code>setuid(2)</code></a>).</li> 695<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the group identity of the process (see <a href="http://man7.org/linux/man-pages/man2/setgid.2.html"><code>setgid(2)</code></a>).</li> 696<li><code>serialization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Specify the kind of serialization used for sending 697messages between processes. Possible values are <code>'json'</code> and <code>'advanced'</code>. 698See <a href="#child_process_advanced_serialization">Advanced serialization</a> for more details. <strong>Default:</strong> <code>'json'</code>.</li> 699<li><code>shell</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> If <code>true</code>, runs <code>command</code> inside of a shell. Uses 700<code>'/bin/sh'</code> on Unix, and <code>process.env.ComSpec</code> on Windows. A different 701shell can be specified as a string. See <a href="#child_process_shell_requirements">Shell requirements</a> and 702<a href="#child_process_default_windows_shell">Default Windows shell</a>. <strong>Default:</strong> <code>false</code> (no shell).</li> 703<li><code>windowsVerbatimArguments</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> No quoting or escaping of arguments is 704done on Windows. Ignored on Unix. This is set to <code>true</code> automatically 705when <code>shell</code> is specified and is CMD. <strong>Default:</strong> <code>false</code>.</li> 706<li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Hide the subprocess console window that would 707normally be created on Windows systems. <strong>Default:</strong> <code>false</code>.</li> 708<li><code>signal</code> <a href="globals.html#globals_class_abortsignal" class="type"><AbortSignal></a> allows aborting the child process using an 709AbortSignal.</li> 710<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> In milliseconds the maximum amount of time the process 711is allowed to run. <strong>Default:</strong> <code>undefined</code>.</li> 712<li><code>killSignal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The signal value to be used when the spawned 713process will be killed by timeout or abort signal. <strong>Default:</strong> <code>'SIGTERM'</code>.</li> 714</ul> 715</li> 716<li> 717<p>Returns: <a href="child_process.html#child_process_class_childprocess" class="type"><ChildProcess></a></p> 718</li> 719</ul> 720<p>The <code>child_process.spawn()</code> method spawns a new process using the given 721<code>command</code>, with command-line arguments in <code>args</code>. If omitted, <code>args</code> defaults 722to an empty array.</p> 723<p><strong>If the <code>shell</code> option is enabled, do not pass unsanitized user input to this 724function. Any input containing shell metacharacters may be used to trigger 725arbitrary command execution.</strong></p> 726<p>A third argument may be used to specify additional options, with these defaults:</p> 727<pre><code class="language-js"><span class="hljs-keyword">const</span> defaults = { 728 <span class="hljs-attr">cwd</span>: <span class="hljs-literal">undefined</span>, 729 <span class="hljs-attr">env</span>: process.<span class="hljs-property">env</span> 730};</code></pre> 731<p>Use <code>cwd</code> to specify the working directory from which the process is spawned. 732If not given, the default is to inherit the current working directory. If given, 733but the path does not exist, the child process emits an <code>ENOENT</code> error 734and exits immediately. <code>ENOENT</code> is also emitted when the command 735does not exist.</p> 736<p>Use <code>env</code> to specify environment variables that will be visible to the new 737process, the default is <a href="process.html#process_process_env"><code>process.env</code></a>.</p> 738<p><code>undefined</code> values in <code>env</code> will be ignored.</p> 739<p>Example of running <code>ls -lh /usr</code>, capturing <code>stdout</code>, <code>stderr</code>, and the 740exit code:</p> 741<pre><code class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>); 742<span class="hljs-keyword">const</span> ls = <span class="hljs-title function_">spawn</span>(<span class="hljs-string">'ls'</span>, [<span class="hljs-string">'-lh'</span>, <span class="hljs-string">'/usr'</span>]); 743 744ls.<span class="hljs-property">stdout</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> { 745 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`stdout: <span class="hljs-subst">${data}</span>`</span>); 746}); 747 748ls.<span class="hljs-property">stderr</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> { 749 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`stderr: <span class="hljs-subst">${data}</span>`</span>); 750}); 751 752ls.<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-function">(<span class="hljs-params">code</span>) =></span> { 753 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`child process exited with code <span class="hljs-subst">${code}</span>`</span>); 754});</code></pre> 755<p>Example: A very elaborate way to run <code>ps ax | grep ssh</code></p> 756<pre><code class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>); 757<span class="hljs-keyword">const</span> ps = <span class="hljs-title function_">spawn</span>(<span class="hljs-string">'ps'</span>, [<span class="hljs-string">'ax'</span>]); 758<span class="hljs-keyword">const</span> grep = <span class="hljs-title function_">spawn</span>(<span class="hljs-string">'grep'</span>, [<span class="hljs-string">'ssh'</span>]); 759 760ps.<span class="hljs-property">stdout</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> { 761 grep.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">write</span>(data); 762}); 763 764ps.<span class="hljs-property">stderr</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> { 765 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`ps stderr: <span class="hljs-subst">${data}</span>`</span>); 766}); 767 768ps.<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-function">(<span class="hljs-params">code</span>) =></span> { 769 <span class="hljs-keyword">if</span> (code !== <span class="hljs-number">0</span>) { 770 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`ps process exited with code <span class="hljs-subst">${code}</span>`</span>); 771 } 772 grep.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">end</span>(); 773}); 774 775grep.<span class="hljs-property">stdout</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> { 776 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(data.<span class="hljs-title function_">toString</span>()); 777}); 778 779grep.<span class="hljs-property">stderr</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> { 780 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`grep stderr: <span class="hljs-subst">${data}</span>`</span>); 781}); 782 783grep.<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-function">(<span class="hljs-params">code</span>) =></span> { 784 <span class="hljs-keyword">if</span> (code !== <span class="hljs-number">0</span>) { 785 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`grep process exited with code <span class="hljs-subst">${code}</span>`</span>); 786 } 787});</code></pre> 788<p>Example of checking for failed <code>spawn</code>:</p> 789<pre><code class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>); 790<span class="hljs-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span>(<span class="hljs-string">'bad_command'</span>); 791 792subprocess.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 793 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'Failed to start subprocess.'</span>); 794});</code></pre> 795<p>Certain platforms (macOS, Linux) will use the value of <code>argv[0]</code> for the process 796title while others (Windows, SunOS) will use <code>command</code>.</p> 797<p>Node.js currently overwrites <code>argv[0]</code> with <code>process.execPath</code> on startup, so 798<code>process.argv[0]</code> in a Node.js child process will not match the <code>argv0</code> 799parameter passed to <code>spawn</code> from the parent, retrieve it with the 800<code>process.argv0</code> property instead.</p> 801<p>If the <code>signal</code> option is enabled, calling <code>.abort()</code> on the corresponding 802<code>AbortController</code> is similar to calling <code>.kill()</code> on the child process except 803the error passed to the callback will be an <code>AbortError</code>:</p> 804<pre><code class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>); 805<span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); 806<span class="hljs-keyword">const</span> { signal } = controller; 807<span class="hljs-keyword">const</span> grep = <span class="hljs-title function_">spawn</span>(<span class="hljs-string">'grep'</span>, [<span class="hljs-string">'ssh'</span>], { signal }); 808grep.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 809 <span class="hljs-comment">// This will be called with err being an AbortError if the controller aborts</span> 810}); 811controller.<span class="hljs-title function_">abort</span>(); <span class="hljs-comment">// Stops the child process</span></code></pre> 812<h5><code>options.detached</code><span><a class="mark" href="#child_process_options_detached" id="child_process_options_detached">#</a></span></h5> 813<div class="api_metadata"> 814<span>Added in: v0.7.10</span> 815</div> 816<p>On Windows, setting <code>options.detached</code> to <code>true</code> makes it possible for the 817child process to continue running after the parent exits. The child will have 818its own console window. Once enabled for a child process, it cannot be 819disabled.</p> 820<p>On non-Windows platforms, if <code>options.detached</code> is set to <code>true</code>, the child 821process will be made the leader of a new process group and session. Child 822processes may continue running after the parent exits regardless of whether 823they are detached or not. See <a href="http://man7.org/linux/man-pages/man2/setsid.2.html"><code>setsid(2)</code></a> for more information.</p> 824<p>By default, the parent will wait for the detached child to exit. To prevent the 825parent from waiting for a given <code>subprocess</code> to exit, use the 826<code>subprocess.unref()</code> method. Doing so will cause the parent's event loop to not 827include the child in its reference count, allowing the parent to exit 828independently of the child, unless there is an established IPC channel between 829the child and the parent.</p> 830<p>When using the <code>detached</code> option to start a long-running process, the process 831will not stay running in the background after the parent exits unless it is 832provided with a <code>stdio</code> configuration that is not connected to the parent. 833If the parent's <code>stdio</code> is inherited, the child will remain attached to the 834controlling terminal.</p> 835<p>Example of a long-running process, by detaching and also ignoring its parent 836<code>stdio</code> file descriptors, in order to ignore the parent's termination:</p> 837<pre><code class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>); 838 839<span class="hljs-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span>(process.<span class="hljs-property">argv</span>[<span class="hljs-number">0</span>], [<span class="hljs-string">'child_program.js'</span>], { 840 <span class="hljs-attr">detached</span>: <span class="hljs-literal">true</span>, 841 <span class="hljs-attr">stdio</span>: <span class="hljs-string">'ignore'</span> 842}); 843 844subprocess.<span class="hljs-title function_">unref</span>();</code></pre> 845<p>Alternatively one can redirect the child process' output into files:</p> 846<pre><code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>); 847<span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>); 848<span class="hljs-keyword">const</span> out = fs.<span class="hljs-title function_">openSync</span>(<span class="hljs-string">'./out.log'</span>, <span class="hljs-string">'a'</span>); 849<span class="hljs-keyword">const</span> err = fs.<span class="hljs-title function_">openSync</span>(<span class="hljs-string">'./out.log'</span>, <span class="hljs-string">'a'</span>); 850 851<span class="hljs-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span>(<span class="hljs-string">'prg'</span>, [], { 852 <span class="hljs-attr">detached</span>: <span class="hljs-literal">true</span>, 853 <span class="hljs-attr">stdio</span>: [ <span class="hljs-string">'ignore'</span>, out, err ] 854}); 855 856subprocess.<span class="hljs-title function_">unref</span>();</code></pre> 857<h5><code>options.stdio</code><span><a class="mark" href="#child_process_options_stdio" id="child_process_options_stdio">#</a></span></h5> 858<div class="api_metadata"> 859<details class="changelog"><summary>History</summary> 860<table> 861<tbody><tr><th>Version</th><th>Changes</th></tr> 862<tr><td>v14.18.0</td> 863<td><p>Added the <code>overlapped</code> stdio flag.</p></td></tr> 864<tr><td>v3.3.1</td> 865<td><p>The value <code>0</code> is now accepted as a file descriptor.</p></td></tr> 866<tr><td>v0.7.10</td> 867<td><p><span>Added in: v0.7.10</span></p></td></tr> 868</tbody></table> 869</details> 870</div> 871<p>The <code>options.stdio</code> option is used to configure the pipes that are established 872between the parent and child process. By default, the child's stdin, stdout, 873and stderr are redirected to corresponding <a href="#child_process_subprocess_stdin"><code>subprocess.stdin</code></a>, 874<a href="#child_process_subprocess_stdout"><code>subprocess.stdout</code></a>, and <a href="#child_process_subprocess_stderr"><code>subprocess.stderr</code></a> streams on the 875<a href="#child_process_class_childprocess"><code>ChildProcess</code></a> object. This is equivalent to setting the <code>options.stdio</code> 876equal to <code>['pipe', 'pipe', 'pipe']</code>.</p> 877<p>For convenience, <code>options.stdio</code> may be one of the following strings:</p> 878<ul> 879<li><code>'pipe'</code>: equivalent to <code>['pipe', 'pipe', 'pipe']</code> (the default)</li> 880<li><code>'overlapped'</code>: equivalent to <code>['overlapped', 'overlapped', 'overlapped']</code></li> 881<li><code>'ignore'</code>: equivalent to <code>['ignore', 'ignore', 'ignore']</code></li> 882<li><code>'inherit'</code>: equivalent to <code>['inherit', 'inherit', 'inherit']</code> or <code>[0, 1, 2]</code></li> 883</ul> 884<p>Otherwise, the value of <code>options.stdio</code> is an array where each index corresponds 885to an fd in the child. The fds 0, 1, and 2 correspond to stdin, stdout, 886and stderr, respectively. Additional fds can be specified to create additional 887pipes between the parent and child. The value is one of the following:</p> 888<ol> 889<li> 890<p><code>'pipe'</code>: Create a pipe between the child process and the parent process. 891The parent end of the pipe is exposed to the parent as a property on the 892<code>child_process</code> object as <a href="#child_process_subprocess_stdio"><code>subprocess.stdio[fd]</code></a>. Pipes 893created for fds 0, 1, and 2 are also available as <a href="#child_process_subprocess_stdin"><code>subprocess.stdin</code></a>, 894<a href="#child_process_subprocess_stdout"><code>subprocess.stdout</code></a> and <a href="#child_process_subprocess_stderr"><code>subprocess.stderr</code></a>, respectively.</p> 895</li> 896<li> 897<p><code>'overlapped'</code>: Same as <code>'pipe'</code> except that the <code>FILE_FLAG_OVERLAPPED</code> flag 898is set on the handle. This is necessary for overlapped I/O on the child 899process's stdio handles. See the 900<a href="https://docs.microsoft.com/en-us/windows/win32/fileio/synchronous-and-asynchronous-i-o">docs</a> 901for more details. This is exactly the same as <code>'pipe'</code> on non-Windows 902systems.</p> 903</li> 904<li> 905<p><code>'ipc'</code>: Create an IPC channel for passing messages/file descriptors 906between parent and child. A <a href="#child_process_class_childprocess"><code>ChildProcess</code></a> may have at most one IPC 907stdio file descriptor. Setting this option enables the 908<a href="#child_process_subprocess_send_message_sendhandle_options_callback"><code>subprocess.send()</code></a> method. If the child is a Node.js process, the 909presence of an IPC channel will enable <a href="process.html#process_process_send_message_sendhandle_options_callback"><code>process.send()</code></a> and 910<a href="process.html#process_process_disconnect"><code>process.disconnect()</code></a> methods, as well as <a href="process.html#process_event_disconnect"><code>'disconnect'</code></a> and 911<a href="process.html#process_event_message"><code>'message'</code></a> events within the child.</p> 912<p>Accessing the IPC channel fd in any way other than <a href="process.html#process_process_send_message_sendhandle_options_callback"><code>process.send()</code></a> 913or using the IPC channel with a child process that is not a Node.js instance 914is not supported.</p> 915</li> 916<li> 917<p><code>'ignore'</code>: Instructs Node.js to ignore the fd in the child. While Node.js 918will always open fds 0, 1, and 2 for the processes it spawns, setting the fd 919to <code>'ignore'</code> will cause Node.js to open <code>/dev/null</code> and attach it to the 920child's fd.</p> 921</li> 922<li> 923<p><code>'inherit'</code>: Pass through the corresponding stdio stream to/from the 924parent process. In the first three positions, this is equivalent to 925<code>process.stdin</code>, <code>process.stdout</code>, and <code>process.stderr</code>, respectively. In 926any other position, equivalent to <code>'ignore'</code>.</p> 927</li> 928<li> 929<p><a href="stream.html#stream_stream" class="type"><Stream></a> object: Share a readable or writable stream that refers to a tty, 930file, socket, or a pipe with the child process. The stream's underlying 931file descriptor is duplicated in the child process to the fd that 932corresponds to the index in the <code>stdio</code> array. The stream must have an 933underlying descriptor (file streams do not until the <code>'open'</code> event has 934occurred).</p> 935</li> 936<li> 937<p>Positive integer: The integer value is interpreted as a file descriptor 938that is currently open in the parent process. It is shared with the child 939process, similar to how <a href="stream.html#stream_stream" class="type"><Stream></a> objects can be shared. Passing sockets 940is not supported on Windows.</p> 941</li> 942<li> 943<p><code>null</code>, <code>undefined</code>: Use default value. For stdio fds 0, 1, and 2 (in other 944words, stdin, stdout, and stderr) a pipe is created. For fd 3 and up, the 945default is <code>'ignore'</code>.</p> 946</li> 947</ol> 948<pre><code class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>); 949 950<span class="hljs-comment">// Child will use parent's stdios.</span> 951<span class="hljs-title function_">spawn</span>(<span class="hljs-string">'prg'</span>, [], { <span class="hljs-attr">stdio</span>: <span class="hljs-string">'inherit'</span> }); 952 953<span class="hljs-comment">// Spawn child sharing only stderr.</span> 954<span class="hljs-title function_">spawn</span>(<span class="hljs-string">'prg'</span>, [], { <span class="hljs-attr">stdio</span>: [<span class="hljs-string">'pipe'</span>, <span class="hljs-string">'pipe'</span>, process.<span class="hljs-property">stderr</span>] }); 955 956<span class="hljs-comment">// Open an extra fd=4, to interact with programs presenting a</span> 957<span class="hljs-comment">// startd-style interface.</span> 958<span class="hljs-title function_">spawn</span>(<span class="hljs-string">'prg'</span>, [], { <span class="hljs-attr">stdio</span>: [<span class="hljs-string">'pipe'</span>, <span class="hljs-literal">null</span>, <span class="hljs-literal">null</span>, <span class="hljs-literal">null</span>, <span class="hljs-string">'pipe'</span>] });</code></pre> 959<p><em>It is worth noting that when an IPC channel is established between the 960parent and child processes, and the child is a Node.js process, the child 961is launched with the IPC channel unreferenced (using <code>unref()</code>) until the 962child registers an event handler for the <a href="process.html#process_event_disconnect"><code>'disconnect'</code></a> event 963or the <a href="process.html#process_event_message"><code>'message'</code></a> event. This allows the child to exit 964normally without the process being held open by the open IPC channel.</em></p> 965<p>On Unix-like operating systems, the <a href="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a> method 966performs memory operations synchronously before decoupling the event loop 967from the child. Applications with a large memory footprint may find frequent 968<a href="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a> calls to be a bottleneck. For more information, 969see <a href="https://bugs.chromium.org/p/v8/issues/detail?id=7381">V8 issue 7381</a>.</p> 970<p>See also: <a href="#child_process_child_process_exec_command_options_callback"><code>child_process.exec()</code></a> and <a href="#child_process_child_process_fork_modulepath_args_options"><code>child_process.fork()</code></a>.</p> 971</section><section><h3>Synchronous process creation<span><a class="mark" href="#child_process_synchronous_process_creation" id="child_process_synchronous_process_creation">#</a></span></h3> 972<p>The <a href="#child_process_child_process_spawnsync_command_args_options"><code>child_process.spawnSync()</code></a>, <a href="#child_process_child_process_execsync_command_options"><code>child_process.execSync()</code></a>, and 973<a href="#child_process_child_process_execfilesync_file_args_options"><code>child_process.execFileSync()</code></a> methods are synchronous and will block the 974Node.js event loop, pausing execution of any additional code until the spawned 975process exits.</p> 976<p>Blocking calls like these are mostly useful for simplifying general-purpose 977scripting tasks and for simplifying the loading/processing of application 978configuration at startup.</p> 979<h4><code>child_process.execFileSync(file[, args][, options])</code><span><a class="mark" href="#child_process_child_process_execfilesync_file_args_options" id="child_process_child_process_execfilesync_file_args_options">#</a></span></h4> 980<div class="api_metadata"> 981<details class="changelog"><summary>History</summary> 982<table> 983<tbody><tr><th>Version</th><th>Changes</th></tr> 984<tr><td>v14.18.0</td> 985<td><p>The <code>cwd</code> option can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 986<tr><td>v10.10.0</td> 987<td><p>The <code>input</code> option can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr> 988<tr><td>v8.8.0</td> 989<td><p>The <code>windowsHide</code> option is supported now.</p></td></tr> 990<tr><td>v8.0.0</td> 991<td><p>The <code>input</code> option can now be a <code>Uint8Array</code>.</p></td></tr> 992<tr><td>v6.2.1, v4.5.0</td> 993<td><p>The <code>encoding</code> option can now explicitly be set to <code>buffer</code>.</p></td></tr> 994<tr><td>v0.11.12</td> 995<td><p><span>Added in: v0.11.12</span></p></td></tr> 996</tbody></table> 997</details> 998</div> 999<ul> 1000<li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The name or path of the executable file to run.</li> 1001<li><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> List of string arguments.</li> 1002<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1003<ul> 1004<li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a> Current working directory of the child process.</li> 1005<li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> The value which will be passed 1006as stdin to the spawned process. Supplying this value will override 1007<code>stdio[0]</code>.</li> 1008<li><code>stdio</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> Child's stdio configuration. <code>stderr</code> by default will 1009be output to the parent process' stderr unless <code>stdio</code> is specified. 1010<strong>Default:</strong> <code>'pipe'</code>.</li> 1011<li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Environment key-value pairs. <strong>Default:</strong> <code>process.env</code>.</li> 1012<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the user identity of the process (see <a href="http://man7.org/linux/man-pages/man2/setuid.2.html"><code>setuid(2)</code></a>).</li> 1013<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the group identity of the process (see <a href="http://man7.org/linux/man-pages/man2/setgid.2.html"><code>setgid(2)</code></a>).</li> 1014<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> In milliseconds the maximum amount of time the process 1015is allowed to run. <strong>Default:</strong> <code>undefined</code>.</li> 1016<li><code>killSignal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The signal value to be used when the spawned 1017process will be killed. <strong>Default:</strong> <code>'SIGTERM'</code>.</li> 1018<li><code>maxBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Largest amount of data in bytes allowed on stdout or 1019stderr. If exceeded, the child process is terminated. See caveat at 1020<a href="#child_process_maxbuffer_and_unicode"><code>maxBuffer</code> and Unicode</a>. <strong>Default:</strong> <code>1024 * 1024</code>.</li> 1021<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The encoding used for all stdio inputs and outputs. 1022<strong>Default:</strong> <code>'buffer'</code>.</li> 1023<li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Hide the subprocess console window that would 1024normally be created on Windows systems. <strong>Default:</strong> <code>false</code>.</li> 1025<li><code>shell</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> If <code>true</code>, runs <code>command</code> inside of a shell. Uses 1026<code>'/bin/sh'</code> on Unix, and <code>process.env.ComSpec</code> on Windows. A different 1027shell can be specified as a string. See <a href="#child_process_shell_requirements">Shell requirements</a> and 1028<a href="#child_process_default_windows_shell">Default Windows shell</a>. <strong>Default:</strong> <code>false</code> (no shell).</li> 1029</ul> 1030</li> 1031<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The stdout from the command.</li> 1032</ul> 1033<p>The <code>child_process.execFileSync()</code> method is generally identical to 1034<a href="#child_process_child_process_execfile_file_args_options_callback"><code>child_process.execFile()</code></a> with the exception that the method will not 1035return until the child process has fully closed. When a timeout has been 1036encountered and <code>killSignal</code> is sent, the method won't return until the process 1037has completely exited.</p> 1038<p>If the child process intercepts and handles the <code>SIGTERM</code> signal and 1039does not exit, the parent process will still wait until the child process has 1040exited.</p> 1041<p>If the process times out or has a non-zero exit code, this method will throw an 1042<a href="errors.html#errors_class_error"><code>Error</code></a> that will include the full result of the underlying 1043<a href="#child_process_child_process_spawnsync_command_args_options"><code>child_process.spawnSync()</code></a>.</p> 1044<p><strong>If the <code>shell</code> option is enabled, do not pass unsanitized user input to this 1045function. Any input containing shell metacharacters may be used to trigger 1046arbitrary command execution.</strong></p> 1047<h4><code>child_process.execSync(command[, options])</code><span><a class="mark" href="#child_process_child_process_execsync_command_options" id="child_process_child_process_execsync_command_options">#</a></span></h4> 1048<div class="api_metadata"> 1049<details class="changelog"><summary>History</summary> 1050<table> 1051<tbody><tr><th>Version</th><th>Changes</th></tr> 1052<tr><td>v14.18.0</td> 1053<td><p>The <code>cwd</code> option can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 1054<tr><td>v10.10.0</td> 1055<td><p>The <code>input</code> option can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr> 1056<tr><td>v8.8.0</td> 1057<td><p>The <code>windowsHide</code> option is supported now.</p></td></tr> 1058<tr><td>v8.0.0</td> 1059<td><p>The <code>input</code> option can now be a <code>Uint8Array</code>.</p></td></tr> 1060<tr><td>v0.11.12</td> 1061<td><p><span>Added in: v0.11.12</span></p></td></tr> 1062</tbody></table> 1063</details> 1064</div> 1065<ul> 1066<li><code>command</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The command to run.</li> 1067<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1068<ul> 1069<li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a> Current working directory of the child process.</li> 1070<li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> The value which will be passed 1071as stdin to the spawned process. Supplying this value will override 1072<code>stdio[0]</code>.</li> 1073<li><code>stdio</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> Child's stdio configuration. <code>stderr</code> by default will 1074be output to the parent process' stderr unless <code>stdio</code> is specified. 1075<strong>Default:</strong> <code>'pipe'</code>.</li> 1076<li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Environment key-value pairs. <strong>Default:</strong> <code>process.env</code>.</li> 1077<li><code>shell</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Shell to execute the command with. See 1078<a href="#child_process_shell_requirements">Shell requirements</a> and <a href="#child_process_default_windows_shell">Default Windows shell</a>. <strong>Default:</strong> 1079<code>'/bin/sh'</code> on Unix, <code>process.env.ComSpec</code> on Windows.</li> 1080<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the user identity of the process. (See <a href="http://man7.org/linux/man-pages/man2/setuid.2.html"><code>setuid(2)</code></a>).</li> 1081<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the group identity of the process. (See <a href="http://man7.org/linux/man-pages/man2/setgid.2.html"><code>setgid(2)</code></a>).</li> 1082<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> In milliseconds the maximum amount of time the process 1083is allowed to run. <strong>Default:</strong> <code>undefined</code>.</li> 1084<li><code>killSignal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The signal value to be used when the spawned 1085process will be killed. <strong>Default:</strong> <code>'SIGTERM'</code>.</li> 1086<li><code>maxBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Largest amount of data in bytes allowed on stdout or 1087stderr. If exceeded, the child process is terminated and any output is 1088truncated. See caveat at <a href="#child_process_maxbuffer_and_unicode"><code>maxBuffer</code> and Unicode</a>. 1089<strong>Default:</strong> <code>1024 * 1024</code>.</li> 1090<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The encoding used for all stdio inputs and outputs. 1091<strong>Default:</strong> <code>'buffer'</code>.</li> 1092<li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Hide the subprocess console window that would 1093normally be created on Windows systems. <strong>Default:</strong> <code>false</code>.</li> 1094</ul> 1095</li> 1096<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The stdout from the command.</li> 1097</ul> 1098<p>The <code>child_process.execSync()</code> method is generally identical to 1099<a href="#child_process_child_process_exec_command_options_callback"><code>child_process.exec()</code></a> with the exception that the method will not return 1100until the child process has fully closed. When a timeout has been encountered 1101and <code>killSignal</code> is sent, the method won't return until the process has 1102completely exited. If the child process intercepts and handles the <code>SIGTERM</code> 1103signal and doesn't exit, the parent process will wait until the child process 1104has exited.</p> 1105<p>If the process times out or has a non-zero exit code, this method will throw. 1106The <a href="errors.html#errors_class_error"><code>Error</code></a> object will contain the entire result from 1107<a href="#child_process_child_process_spawnsync_command_args_options"><code>child_process.spawnSync()</code></a>.</p> 1108<p><strong>Never pass unsanitized user input to this function. Any input containing shell 1109metacharacters may be used to trigger arbitrary command execution.</strong></p> 1110<h4><code>child_process.spawnSync(command[, args][, options])</code><span><a class="mark" href="#child_process_child_process_spawnsync_command_args_options" id="child_process_child_process_spawnsync_command_args_options">#</a></span></h4> 1111<div class="api_metadata"> 1112<details class="changelog"><summary>History</summary> 1113<table> 1114<tbody><tr><th>Version</th><th>Changes</th></tr> 1115<tr><td>v14.18.0</td> 1116<td><p>The <code>cwd</code> option can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 1117<tr><td>v10.10.0</td> 1118<td><p>The <code>input</code> option can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr> 1119<tr><td>v8.8.0</td> 1120<td><p>The <code>windowsHide</code> option is supported now.</p></td></tr> 1121<tr><td>v8.0.0</td> 1122<td><p>The <code>input</code> option can now be a <code>Uint8Array</code>.</p></td></tr> 1123<tr><td>v5.7.0</td> 1124<td><p>The <code>shell</code> option is supported now.</p></td></tr> 1125<tr><td>v6.2.1, v4.5.0</td> 1126<td><p>The <code>encoding</code> option can now explicitly be set to <code>buffer</code>.</p></td></tr> 1127<tr><td>v0.11.12</td> 1128<td><p><span>Added in: v0.11.12</span></p></td></tr> 1129</tbody></table> 1130</details> 1131</div> 1132<ul> 1133<li><code>command</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The command to run.</li> 1134<li><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> List of string arguments.</li> 1135<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1136<ul> 1137<li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a> Current working directory of the child process.</li> 1138<li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> The value which will be passed 1139as stdin to the spawned process. Supplying this value will override 1140<code>stdio[0]</code>.</li> 1141<li><code>argv0</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Explicitly set the value of <code>argv[0]</code> sent to the child 1142process. This will be set to <code>command</code> if not specified.</li> 1143<li><code>stdio</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> Child's stdio configuration.</li> 1144<li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Environment key-value pairs. <strong>Default:</strong> <code>process.env</code>.</li> 1145<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the user identity of the process (see <a href="http://man7.org/linux/man-pages/man2/setuid.2.html"><code>setuid(2)</code></a>).</li> 1146<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the group identity of the process (see <a href="http://man7.org/linux/man-pages/man2/setgid.2.html"><code>setgid(2)</code></a>).</li> 1147<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> In milliseconds the maximum amount of time the process 1148is allowed to run. <strong>Default:</strong> <code>undefined</code>.</li> 1149<li><code>killSignal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The signal value to be used when the spawned 1150process will be killed. <strong>Default:</strong> <code>'SIGTERM'</code>.</li> 1151<li><code>maxBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Largest amount of data in bytes allowed on stdout or 1152stderr. If exceeded, the child process is terminated and any output is 1153truncated. See caveat at <a href="#child_process_maxbuffer_and_unicode"><code>maxBuffer</code> and Unicode</a>. 1154<strong>Default:</strong> <code>1024 * 1024</code>.</li> 1155<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The encoding used for all stdio inputs and outputs. 1156<strong>Default:</strong> <code>'buffer'</code>.</li> 1157<li><code>shell</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> If <code>true</code>, runs <code>command</code> inside of a shell. Uses 1158<code>'/bin/sh'</code> on Unix, and <code>process.env.ComSpec</code> on Windows. A different 1159shell can be specified as a string. See <a href="#child_process_shell_requirements">Shell requirements</a> and 1160<a href="#child_process_default_windows_shell">Default Windows shell</a>. <strong>Default:</strong> <code>false</code> (no shell).</li> 1161<li><code>windowsVerbatimArguments</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> No quoting or escaping of arguments is 1162done on Windows. Ignored on Unix. This is set to <code>true</code> automatically 1163when <code>shell</code> is specified and is CMD. <strong>Default:</strong> <code>false</code>.</li> 1164<li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Hide the subprocess console window that would 1165normally be created on Windows systems. <strong>Default:</strong> <code>false</code>.</li> 1166</ul> 1167</li> 1168<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1169<ul> 1170<li><code>pid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Pid of the child process.</li> 1171<li><code>output</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> Array of results from stdio output.</li> 1172<li><code>stdout</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The contents of <code>output[1]</code>.</li> 1173<li><code>stderr</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The contents of <code>output[2]</code>.</li> 1174<li><code>status</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> The exit code of the subprocess, or <code>null</code> if the 1175subprocess terminated due to a signal.</li> 1176<li><code>signal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> The signal used to kill the subprocess, or <code>null</code> if 1177the subprocess did not terminate due to a signal.</li> 1178<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> The error object if the child process failed or timed out.</li> 1179</ul> 1180</li> 1181</ul> 1182<p>The <code>child_process.spawnSync()</code> method is generally identical to 1183<a href="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a> with the exception that the function will not return 1184until the child process has fully closed. When a timeout has been encountered 1185and <code>killSignal</code> is sent, the method won't return until the process has 1186completely exited. If the process intercepts and handles the <code>SIGTERM</code> signal 1187and doesn't exit, the parent process will wait until the child process has 1188exited.</p> 1189<p><strong>If the <code>shell</code> option is enabled, do not pass unsanitized user input to this 1190function. Any input containing shell metacharacters may be used to trigger 1191arbitrary command execution.</strong></p> 1192</section><section><h3>Class: <code>ChildProcess</code><span><a class="mark" href="#child_process_class_childprocess" id="child_process_class_childprocess">#</a></span></h3> 1193<div class="api_metadata"> 1194<span>Added in: v2.2.0</span> 1195</div> 1196<ul> 1197<li>Extends: <a href="events.html#events_class_eventemitter" class="type"><EventEmitter></a></li> 1198</ul> 1199<p>Instances of the <code>ChildProcess</code> represent spawned child processes.</p> 1200<p>Instances of <code>ChildProcess</code> are not intended to be created directly. Rather, 1201use the <a href="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a>, <a href="#child_process_child_process_exec_command_options_callback"><code>child_process.exec()</code></a>, 1202<a href="#child_process_child_process_execfile_file_args_options_callback"><code>child_process.execFile()</code></a>, or <a href="#child_process_child_process_fork_modulepath_args_options"><code>child_process.fork()</code></a> methods to create 1203instances of <code>ChildProcess</code>.</p> 1204<h4>Event: <code>'close'</code><span><a class="mark" href="#child_process_event_close" id="child_process_event_close">#</a></span></h4> 1205<div class="api_metadata"> 1206<span>Added in: v0.7.7</span> 1207</div> 1208<ul> 1209<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The exit code if the child exited on its own.</li> 1210<li><code>signal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The signal by which the child process was terminated.</li> 1211</ul> 1212<p>The <code>'close'</code> event is emitted after a process has ended <em>and</em> the stdio 1213streams of a child process have been closed. This is distinct from the 1214<a href="#child_process_event_exit"><code>'exit'</code></a> event, since multiple processes might share the same stdio 1215streams. The <code>'close'</code> event will always emit after <a href="#child_process_event_exit"><code>'exit'</code></a> was 1216already emitted, or <a href="#child_process_event_error"><code>'error'</code></a> if the child failed to spawn.</p> 1217<pre><code class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>); 1218<span class="hljs-keyword">const</span> ls = <span class="hljs-title function_">spawn</span>(<span class="hljs-string">'ls'</span>, [<span class="hljs-string">'-lh'</span>, <span class="hljs-string">'/usr'</span>]); 1219 1220ls.<span class="hljs-property">stdout</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> { 1221 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`stdout: <span class="hljs-subst">${data}</span>`</span>); 1222}); 1223 1224ls.<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-function">(<span class="hljs-params">code</span>) =></span> { 1225 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`child process close all stdio with code <span class="hljs-subst">${code}</span>`</span>); 1226}); 1227 1228ls.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">(<span class="hljs-params">code</span>) =></span> { 1229 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`child process exited with code <span class="hljs-subst">${code}</span>`</span>); 1230});</code></pre> 1231<h4>Event: <code>'disconnect'</code><span><a class="mark" href="#child_process_event_disconnect" id="child_process_event_disconnect">#</a></span></h4> 1232<div class="api_metadata"> 1233<span>Added in: v0.7.2</span> 1234</div> 1235<p>The <code>'disconnect'</code> event is emitted after calling the 1236<a href="#child_process_subprocess_disconnect"><code>subprocess.disconnect()</code></a> method in parent process or 1237<a href="process.html#process_process_disconnect"><code>process.disconnect()</code></a> in child process. After disconnecting it is no longer 1238possible to send or receive messages, and the <a href="#child_process_subprocess_connected"><code>subprocess.connected</code></a> 1239property is <code>false</code>.</p> 1240<h4>Event: <code>'error'</code><span><a class="mark" href="#child_process_event_error" id="child_process_event_error">#</a></span></h4> 1241<ul> 1242<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> The error.</li> 1243</ul> 1244<p>The <code>'error'</code> event is emitted whenever:</p> 1245<ol> 1246<li>The process could not be spawned, or</li> 1247<li>The process could not be killed, or</li> 1248<li>Sending a message to the child process failed.</li> 1249</ol> 1250<p>The <code>'exit'</code> event may or may not fire after an error has occurred. When 1251listening to both the <code>'exit'</code> and <code>'error'</code> events, guard 1252against accidentally invoking handler functions multiple times.</p> 1253<p>See also <a href="#child_process_subprocess_kill_signal"><code>subprocess.kill()</code></a> and <a href="#child_process_subprocess_send_message_sendhandle_options_callback"><code>subprocess.send()</code></a>.</p> 1254<h4>Event: <code>'exit'</code><span><a class="mark" href="#child_process_event_exit" id="child_process_event_exit">#</a></span></h4> 1255<div class="api_metadata"> 1256<span>Added in: v0.1.90</span> 1257</div> 1258<ul> 1259<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The exit code if the child exited on its own.</li> 1260<li><code>signal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The signal by which the child process was terminated.</li> 1261</ul> 1262<p>The <code>'exit'</code> event is emitted after the child process ends. If the process 1263exited, <code>code</code> is the final exit code of the process, otherwise <code>null</code>. If the 1264process terminated due to receipt of a signal, <code>signal</code> is the string name of 1265the signal, otherwise <code>null</code>. One of the two will always be non-<code>null</code>.</p> 1266<p>When the <code>'exit'</code> event is triggered, child process stdio streams might still be 1267open.</p> 1268<p>Node.js establishes signal handlers for <code>SIGINT</code> and <code>SIGTERM</code> and Node.js 1269processes will not terminate immediately due to receipt of those signals. 1270Rather, Node.js will perform a sequence of cleanup actions and then will 1271re-raise the handled signal.</p> 1272<p>See <a href="http://man7.org/linux/man-pages/man2/waitpid.2.html"><code>waitpid(2)</code></a>.</p> 1273<h4>Event: <code>'message'</code><span><a class="mark" href="#child_process_event_message" id="child_process_event_message">#</a></span></h4> 1274<div class="api_metadata"> 1275<span>Added in: v0.5.9</span> 1276</div> 1277<ul> 1278<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> A parsed JSON object or primitive value.</li> 1279<li><code>sendHandle</code> <a href="net.html#net_server_listen_handle_backlog_callback" class="type"><Handle></a> A <a href="net.html#net_class_net_socket"><code>net.Socket</code></a> or <a href="net.html#net_class_net_server"><code>net.Server</code></a> object, or 1280undefined.</li> 1281</ul> 1282<p>The <code>'message'</code> event is triggered when a child process uses 1283<a href="process.html#process_process_send_message_sendhandle_options_callback"><code>process.send()</code></a> to send messages.</p> 1284<p>The message goes through serialization and parsing. The resulting 1285message might not be the same as what is originally sent.</p> 1286<p>If the <code>serialization</code> option was set to <code>'advanced'</code> used when spawning the 1287child process, the <code>message</code> argument can contain data that JSON is not able 1288to represent. 1289See <a href="#child_process_advanced_serialization">Advanced serialization</a> for more details.</p> 1290<h4>Event: <code>'spawn'</code><span><a class="mark" href="#child_process_event_spawn" id="child_process_event_spawn">#</a></span></h4> 1291<div class="api_metadata"> 1292<span>Added in: v14.17.0</span> 1293</div> 1294<p>The <code>'spawn'</code> event is emitted once the child process has spawned successfully. 1295If the child process does not spawn successfully, the <code>'spawn'</code> event is not 1296emitted and the <code>'error'</code> event is emitted instead.</p> 1297<p>If emitted, the <code>'spawn'</code> event comes before all other events and before any 1298data is received via <code>stdout</code> or <code>stderr</code>.</p> 1299<p>The <code>'spawn'</code> event will fire regardless of whether an error occurs <strong>within</strong> 1300the spawned process. For example, if <code>bash some-command</code> spawns successfully, 1301the <code>'spawn'</code> event will fire, though <code>bash</code> may fail to spawn <code>some-command</code>. 1302This caveat also applies when using <code>{ shell: true }</code>.</p> 1303<h4><code>subprocess.channel</code><span><a class="mark" href="#child_process_subprocess_channel" id="child_process_subprocess_channel">#</a></span></h4> 1304<div class="api_metadata"> 1305<details class="changelog"><summary>History</summary> 1306<table> 1307<tbody><tr><th>Version</th><th>Changes</th></tr> 1308<tr><td>v14.0.0</td> 1309<td><p>The object no longer accidentally exposes native C++ bindings.</p></td></tr> 1310<tr><td>v7.1.0</td> 1311<td><p><span>Added in: v7.1.0</span></p></td></tr> 1312</tbody></table> 1313</details> 1314</div> 1315<ul> 1316<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> A pipe representing the IPC channel to the child process.</li> 1317</ul> 1318<p>The <code>subprocess.channel</code> property is a reference to the child's IPC channel. If 1319no IPC channel currently exists, this property is <code>undefined</code>.</p> 1320<h5><code>subprocess.channel.ref()</code><span><a class="mark" href="#child_process_subprocess_channel_ref" id="child_process_subprocess_channel_ref">#</a></span></h5> 1321<div class="api_metadata"> 1322<span>Added in: v7.1.0</span> 1323</div> 1324<p>This method makes the IPC channel keep the event loop of the parent process 1325running if <code>.unref()</code> has been called before.</p> 1326<h5><code>subprocess.channel.unref()</code><span><a class="mark" href="#child_process_subprocess_channel_unref" id="child_process_subprocess_channel_unref">#</a></span></h5> 1327<div class="api_metadata"> 1328<span>Added in: v7.1.0</span> 1329</div> 1330<p>This method makes the IPC channel not keep the event loop of the parent process 1331running, and lets it finish even while the channel is open.</p> 1332<h4><code>subprocess.connected</code><span><a class="mark" href="#child_process_subprocess_connected" id="child_process_subprocess_connected">#</a></span></h4> 1333<div class="api_metadata"> 1334<span>Added in: v0.7.2</span> 1335</div> 1336<ul> 1337<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Set to <code>false</code> after <code>subprocess.disconnect()</code> is called.</li> 1338</ul> 1339<p>The <code>subprocess.connected</code> property indicates whether it is still possible to 1340send and receive messages from a child process. When <code>subprocess.connected</code> is 1341<code>false</code>, it is no longer possible to send or receive messages.</p> 1342<h4><code>subprocess.disconnect()</code><span><a class="mark" href="#child_process_subprocess_disconnect" id="child_process_subprocess_disconnect">#</a></span></h4> 1343<div class="api_metadata"> 1344<span>Added in: v0.7.2</span> 1345</div> 1346<p>Closes the IPC channel between parent and child, allowing the child to exit 1347gracefully once there are no other connections keeping it alive. After calling 1348this method the <code>subprocess.connected</code> and <code>process.connected</code> properties in 1349both the parent and child (respectively) will be set to <code>false</code>, and it will be 1350no longer possible to pass messages between the processes.</p> 1351<p>The <code>'disconnect'</code> event will be emitted when there are no messages in the 1352process of being received. This will most often be triggered immediately after 1353calling <code>subprocess.disconnect()</code>.</p> 1354<p>When the child process is a Node.js instance (e.g. spawned using 1355<a href="#child_process_child_process_fork_modulepath_args_options"><code>child_process.fork()</code></a>), the <code>process.disconnect()</code> method can be invoked 1356within the child process to close the IPC channel as well.</p> 1357<h4><code>subprocess.exitCode</code><span><a class="mark" href="#child_process_subprocess_exitcode" id="child_process_subprocess_exitcode">#</a></span></h4> 1358<ul> 1359<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1360</ul> 1361<p>The <code>subprocess.exitCode</code> property indicates the exit code of the child process. 1362If the child process is still running, the field will be <code>null</code>.</p> 1363<h4><code>subprocess.kill([signal])</code><span><a class="mark" href="#child_process_subprocess_kill_signal" id="child_process_subprocess_kill_signal">#</a></span></h4> 1364<div class="api_metadata"> 1365<span>Added in: v0.1.90</span> 1366</div> 1367<ul> 1368<li><code>signal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1369<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1370</ul> 1371<p>The <code>subprocess.kill()</code> method sends a signal to the child process. If no 1372argument is given, the process will be sent the <code>'SIGTERM'</code> signal. See 1373<a href="http://man7.org/linux/man-pages/man7/signal.7.html"><code>signal(7)</code></a> for a list of available signals. This function returns <code>true</code> if 1374<a href="http://man7.org/linux/man-pages/man2/kill.2.html"><code>kill(2)</code></a> succeeds, and <code>false</code> otherwise.</p> 1375<pre><code class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>); 1376<span class="hljs-keyword">const</span> grep = <span class="hljs-title function_">spawn</span>(<span class="hljs-string">'grep'</span>, [<span class="hljs-string">'ssh'</span>]); 1377 1378grep.<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-function">(<span class="hljs-params">code, signal</span>) =></span> { 1379 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>( 1380 <span class="hljs-string">`child process terminated due to receipt of signal <span class="hljs-subst">${signal}</span>`</span>); 1381}); 1382 1383<span class="hljs-comment">// Send SIGHUP to process.</span> 1384grep.<span class="hljs-title function_">kill</span>(<span class="hljs-string">'SIGHUP'</span>);</code></pre> 1385<p>The <a href="#child_process_class_childprocess"><code>ChildProcess</code></a> object may emit an <a href="#child_process_event_error"><code>'error'</code></a> event if the signal 1386cannot be delivered. Sending a signal to a child process that has already exited 1387is not an error but may have unforeseen consequences. Specifically, if the 1388process identifier (PID) has been reassigned to another process, the signal will 1389be delivered to that process instead which can have unexpected results.</p> 1390<p>While the function is called <code>kill</code>, the signal delivered to the child process 1391may not actually terminate the process.</p> 1392<p>See <a href="http://man7.org/linux/man-pages/man2/kill.2.html"><code>kill(2)</code></a> for reference.</p> 1393<p>On Windows, where POSIX signals do not exist, the <code>signal</code> argument will be 1394ignored, and the process will be killed forcefully and abruptly (similar to 1395<code>'SIGKILL'</code>). 1396See <a href="process.html#process_signal_events">Signal Events</a> for more details.</p> 1397<p>On Linux, child processes of child processes will not be terminated 1398when attempting to kill their parent. This is likely to happen when running a 1399new process in a shell or with the use of the <code>shell</code> option of <code>ChildProcess</code>:</p> 1400<pre><code class="language-js"><span class="hljs-meta">'use strict'</span>; 1401<span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>); 1402 1403<span class="hljs-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span>( 1404 <span class="hljs-string">'sh'</span>, 1405 [ 1406 <span class="hljs-string">'-c'</span>, 1407 <span class="hljs-string">`node -e "setInterval(() => { 1408 console.log(process.pid, 'is alive') 1409 }, 500);"`</span>, 1410 ], { 1411 <span class="hljs-attr">stdio</span>: [<span class="hljs-string">'inherit'</span>, <span class="hljs-string">'inherit'</span>, <span class="hljs-string">'inherit'</span>] 1412 } 1413); 1414 1415<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> { 1416 subprocess.<span class="hljs-title function_">kill</span>(); <span class="hljs-comment">// Does not terminate the Node.js process in the shell.</span> 1417}, <span class="hljs-number">2000</span>);</code></pre> 1418<h4><code>subprocess.killed</code><span><a class="mark" href="#child_process_subprocess_killed" id="child_process_subprocess_killed">#</a></span></h4> 1419<div class="api_metadata"> 1420<span>Added in: v0.5.10</span> 1421</div> 1422<ul> 1423<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Set to <code>true</code> after <code>subprocess.kill()</code> is used to successfully 1424send a signal to the child process.</li> 1425</ul> 1426<p>The <code>subprocess.killed</code> property indicates whether the child process 1427successfully received a signal from <code>subprocess.kill()</code>. The <code>killed</code> property 1428does not indicate that the child process has been terminated.</p> 1429<h4><code>subprocess.pid</code><span><a class="mark" href="#child_process_subprocess_pid" id="child_process_subprocess_pid">#</a></span></h4> 1430<div class="api_metadata"> 1431<span>Added in: v0.1.90</span> 1432</div> 1433<ul> 1434<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a></li> 1435</ul> 1436<p>Returns the process identifier (PID) of the child process. If the child process 1437fails to spawn due to errors, then the value is <code>undefined</code> and <code>error</code> is 1438emitted.</p> 1439<pre><code class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>); 1440<span class="hljs-keyword">const</span> grep = <span class="hljs-title function_">spawn</span>(<span class="hljs-string">'grep'</span>, [<span class="hljs-string">'ssh'</span>]); 1441 1442<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Spawned child pid: <span class="hljs-subst">${grep.pid}</span>`</span>); 1443grep.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">end</span>();</code></pre> 1444<h4><code>subprocess.ref()</code><span><a class="mark" href="#child_process_subprocess_ref" id="child_process_subprocess_ref">#</a></span></h4> 1445<div class="api_metadata"> 1446<span>Added in: v0.7.10</span> 1447</div> 1448<p>Calling <code>subprocess.ref()</code> after making a call to <code>subprocess.unref()</code> will 1449restore the removed reference count for the child process, forcing the parent 1450to wait for the child to exit before exiting itself.</p> 1451<pre><code class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>); 1452 1453<span class="hljs-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span>(process.<span class="hljs-property">argv</span>[<span class="hljs-number">0</span>], [<span class="hljs-string">'child_program.js'</span>], { 1454 <span class="hljs-attr">detached</span>: <span class="hljs-literal">true</span>, 1455 <span class="hljs-attr">stdio</span>: <span class="hljs-string">'ignore'</span> 1456}); 1457 1458subprocess.<span class="hljs-title function_">unref</span>(); 1459subprocess.<span class="hljs-title function_">ref</span>();</code></pre> 1460<h4><code>subprocess.send(message[, sendHandle[, options]][, callback])</code><span><a class="mark" href="#child_process_subprocess_send_message_sendhandle_options_callback" id="child_process_subprocess_send_message_sendhandle_options_callback">#</a></span></h4> 1461<div class="api_metadata"> 1462<details class="changelog"><summary>History</summary> 1463<table> 1464<tbody><tr><th>Version</th><th>Changes</th></tr> 1465<tr><td>v5.8.0</td> 1466<td><p>The <code>options</code> parameter, and the <code>keepOpen</code> option in particular, is supported now.</p></td></tr> 1467<tr><td>v5.0.0</td> 1468<td><p>This method returns a boolean for flow control now.</p></td></tr> 1469<tr><td>v4.0.0</td> 1470<td><p>The <code>callback</code> parameter is supported now.</p></td></tr> 1471<tr><td>v0.5.9</td> 1472<td><p><span>Added in: v0.5.9</span></p></td></tr> 1473</tbody></table> 1474</details> 1475</div> 1476<ul> 1477<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 1478<li><code>sendHandle</code> <a href="net.html#net_server_listen_handle_backlog_callback" class="type"><Handle></a></li> 1479<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> The <code>options</code> argument, if present, is an object used to 1480parameterize the sending of certain types of handles. <code>options</code> supports 1481the following properties: 1482<ul> 1483<li><code>keepOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> A value that can be used when passing instances of 1484<code>net.Socket</code>. When <code>true</code>, the socket is kept open in the sending process. 1485<strong>Default:</strong> <code>false</code>.</li> 1486</ul> 1487</li> 1488<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 1489<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1490</ul> 1491<p>When an IPC channel has been established between the parent and child ( 1492i.e. when using <a href="#child_process_child_process_fork_modulepath_args_options"><code>child_process.fork()</code></a>), the <code>subprocess.send()</code> method can 1493be used to send messages to the child process. When the child process is a 1494Node.js instance, these messages can be received via the <a href="process.html#process_event_message"><code>'message'</code></a> event.</p> 1495<p>The message goes through serialization and parsing. The resulting 1496message might not be the same as what is originally sent.</p> 1497<p>For example, in the parent script:</p> 1498<pre><code class="language-js"><span class="hljs-keyword">const</span> cp = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>); 1499<span class="hljs-keyword">const</span> n = cp.<span class="hljs-title function_">fork</span>(<span class="hljs-string">`<span class="hljs-subst">${__dirname}</span>/sub.js`</span>); 1500 1501n.<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function">(<span class="hljs-params">m</span>) =></span> { 1502 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'PARENT got message:'</span>, m); 1503}); 1504 1505<span class="hljs-comment">// Causes the child to print: CHILD got message: { hello: 'world' }</span> 1506n.<span class="hljs-title function_">send</span>({ <span class="hljs-attr">hello</span>: <span class="hljs-string">'world'</span> });</code></pre> 1507<p>And then the child script, <code>'sub.js'</code> might look like this:</p> 1508<pre><code class="language-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function">(<span class="hljs-params">m</span>) =></span> { 1509 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'CHILD got message:'</span>, m); 1510}); 1511 1512<span class="hljs-comment">// Causes the parent to print: PARENT got message: { foo: 'bar', baz: null }</span> 1513process.<span class="hljs-title function_">send</span>({ <span class="hljs-attr">foo</span>: <span class="hljs-string">'bar'</span>, <span class="hljs-attr">baz</span>: <span class="hljs-title class_">Na</span>N });</code></pre> 1514<p>Child Node.js processes will have a <a href="process.html#process_process_send_message_sendhandle_options_callback"><code>process.send()</code></a> method of their own 1515that allows the child to send messages back to the parent.</p> 1516<p>There is a special case when sending a <code>{cmd: 'NODE_foo'}</code> message. Messages 1517containing a <code>NODE_</code> prefix in the <code>cmd</code> property are reserved for use within 1518Node.js core and will not be emitted in the child's <a href="process.html#process_event_message"><code>'message'</code></a> 1519event. Rather, such messages are emitted using the 1520<code>'internalMessage'</code> event and are consumed internally by Node.js. 1521Applications should avoid using such messages or listening for 1522<code>'internalMessage'</code> events as it is subject to change without notice.</p> 1523<p>The optional <code>sendHandle</code> argument that may be passed to <code>subprocess.send()</code> is 1524for passing a TCP server or socket object to the child process. The child will 1525receive the object as the second argument passed to the callback function 1526registered on the <a href="process.html#process_event_message"><code>'message'</code></a> event. Any data that is received 1527and buffered in the socket will not be sent to the child.</p> 1528<p>The optional <code>callback</code> is a function that is invoked after the message is 1529sent but before the child may have received it. The function is called with a 1530single argument: <code>null</code> on success, or an <a href="errors.html#errors_class_error"><code>Error</code></a> object on failure.</p> 1531<p>If no <code>callback</code> function is provided and the message cannot be sent, an 1532<code>'error'</code> event will be emitted by the <a href="#child_process_class_childprocess"><code>ChildProcess</code></a> object. This can 1533happen, for instance, when the child process has already exited.</p> 1534<p><code>subprocess.send()</code> will return <code>false</code> if the channel has closed or when the 1535backlog of unsent messages exceeds a threshold that makes it unwise to send 1536more. Otherwise, the method returns <code>true</code>. The <code>callback</code> function can be 1537used to implement flow control.</p> 1538<h5>Example: sending a server object<span><a class="mark" href="#child_process_example_sending_a_server_object" id="child_process_example_sending_a_server_object">#</a></span></h5> 1539<p>The <code>sendHandle</code> argument can be used, for instance, to pass the handle of 1540a TCP server object to the child process as illustrated in the example below:</p> 1541<pre><code class="language-js"><span class="hljs-keyword">const</span> subprocess = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>).<span class="hljs-title function_">fork</span>(<span class="hljs-string">'subprocess.js'</span>); 1542 1543<span class="hljs-comment">// Open up the server object and send the handle.</span> 1544<span class="hljs-keyword">const</span> server = <span class="hljs-built_in">require</span>(<span class="hljs-string">'net'</span>).<span class="hljs-title function_">createServer</span>(); 1545server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'connection'</span>, <span class="hljs-function">(<span class="hljs-params">socket</span>) =></span> { 1546 socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'handled by parent'</span>); 1547}); 1548server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>, <span class="hljs-function">() =></span> { 1549 subprocess.<span class="hljs-title function_">send</span>(<span class="hljs-string">'server'</span>, server); 1550});</code></pre> 1551<p>The child would then receive the server object as:</p> 1552<pre><code class="language-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function">(<span class="hljs-params">m, server</span>) =></span> { 1553 <span class="hljs-keyword">if</span> (m === <span class="hljs-string">'server'</span>) { 1554 server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'connection'</span>, <span class="hljs-function">(<span class="hljs-params">socket</span>) =></span> { 1555 socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'handled by child'</span>); 1556 }); 1557 } 1558});</code></pre> 1559<p>Once the server is now shared between the parent and child, some connections 1560can be handled by the parent and some by the child.</p> 1561<p>While the example above uses a server created using the <code>net</code> module, <code>dgram</code> 1562module servers use exactly the same workflow with the exceptions of listening on 1563a <code>'message'</code> event instead of <code>'connection'</code> and using <code>server.bind()</code> instead 1564of <code>server.listen()</code>. This is, however, currently only supported on Unix 1565platforms.</p> 1566<h5>Example: sending a socket object<span><a class="mark" href="#child_process_example_sending_a_socket_object" id="child_process_example_sending_a_socket_object">#</a></span></h5> 1567<p>Similarly, the <code>sendHandler</code> argument can be used to pass the handle of a 1568socket to the child process. The example below spawns two children that each 1569handle connections with "normal" or "special" priority:</p> 1570<pre><code class="language-js"><span class="hljs-keyword">const</span> { fork } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>); 1571<span class="hljs-keyword">const</span> normal = <span class="hljs-title function_">fork</span>(<span class="hljs-string">'subprocess.js'</span>, [<span class="hljs-string">'normal'</span>]); 1572<span class="hljs-keyword">const</span> special = <span class="hljs-title function_">fork</span>(<span class="hljs-string">'subprocess.js'</span>, [<span class="hljs-string">'special'</span>]); 1573 1574<span class="hljs-comment">// Open up the server and send sockets to child. Use pauseOnConnect to prevent</span> 1575<span class="hljs-comment">// the sockets from being read before they are sent to the child process.</span> 1576<span class="hljs-keyword">const</span> server = <span class="hljs-built_in">require</span>(<span class="hljs-string">'net'</span>).<span class="hljs-title function_">createServer</span>({ <span class="hljs-attr">pauseOnConnect</span>: <span class="hljs-literal">true</span> }); 1577server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'connection'</span>, <span class="hljs-function">(<span class="hljs-params">socket</span>) =></span> { 1578 1579 <span class="hljs-comment">// If this is special priority...</span> 1580 <span class="hljs-keyword">if</span> (socket.<span class="hljs-property">remoteAddress</span> === <span class="hljs-string">'74.125.127.100'</span>) { 1581 special.<span class="hljs-title function_">send</span>(<span class="hljs-string">'socket'</span>, socket); 1582 <span class="hljs-keyword">return</span>; 1583 } 1584 <span class="hljs-comment">// This is normal priority.</span> 1585 normal.<span class="hljs-title function_">send</span>(<span class="hljs-string">'socket'</span>, socket); 1586}); 1587server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>);</code></pre> 1588<p>The <code>subprocess.js</code> would receive the socket handle as the second argument 1589passed to the event callback function:</p> 1590<pre><code class="language-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function">(<span class="hljs-params">m, socket</span>) =></span> { 1591 <span class="hljs-keyword">if</span> (m === <span class="hljs-string">'socket'</span>) { 1592 <span class="hljs-keyword">if</span> (socket) { 1593 <span class="hljs-comment">// Check that the client socket exists.</span> 1594 <span class="hljs-comment">// It is possible for the socket to be closed between the time it is</span> 1595 <span class="hljs-comment">// sent and the time it is received in the child process.</span> 1596 socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">`Request handled with <span class="hljs-subst">${process.argv[<span class="hljs-number">2</span>]}</span> priority`</span>); 1597 } 1598 } 1599});</code></pre> 1600<p>Do not use <code>.maxConnections</code> on a socket that has been passed to a subprocess. 1601The parent cannot track when the socket is destroyed.</p> 1602<p>Any <code>'message'</code> handlers in the subprocess should verify that <code>socket</code> exists, 1603as the connection may have been closed during the time it takes to send the 1604connection to the child.</p> 1605<h4><code>subprocess.signalCode</code><span><a class="mark" href="#child_process_subprocess_signalcode" id="child_process_subprocess_signalcode">#</a></span></h4> 1606<ul> 1607<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a></li> 1608</ul> 1609<p>The <code>subprocess.signalCode</code> property indicates the signal received by 1610the child process if any, else <code>null</code>.</p> 1611<h4><code>subprocess.spawnargs</code><span><a class="mark" href="#child_process_subprocess_spawnargs" id="child_process_subprocess_spawnargs">#</a></span></h4> 1612<ul> 1613<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a></li> 1614</ul> 1615<p>The <code>subprocess.spawnargs</code> property represents the full list of command-line 1616arguments the child process was launched with.</p> 1617<h4><code>subprocess.spawnfile</code><span><a class="mark" href="#child_process_subprocess_spawnfile" id="child_process_subprocess_spawnfile">#</a></span></h4> 1618<ul> 1619<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1620</ul> 1621<p>The <code>subprocess.spawnfile</code> property indicates the executable file name of 1622the child process that is launched.</p> 1623<p>For <a href="#child_process_child_process_fork_modulepath_args_options"><code>child_process.fork()</code></a>, its value will be equal to 1624<a href="process.html#process_process_execpath"><code>process.execPath</code></a>. 1625For <a href="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a>, its value will be the name of 1626the executable file. 1627For <a href="#child_process_child_process_exec_command_options_callback"><code>child_process.exec()</code></a>, its value will be the name of the shell 1628in which the child process is launched.</p> 1629<h4><code>subprocess.stderr</code><span><a class="mark" href="#child_process_subprocess_stderr" id="child_process_subprocess_stderr">#</a></span></h4> 1630<div class="api_metadata"> 1631<span>Added in: v0.1.90</span> 1632</div> 1633<ul> 1634<li><a href="stream.html#stream_class_stream_readable" class="type"><stream.Readable></a></li> 1635</ul> 1636<p>A <code>Readable Stream</code> that represents the child process's <code>stderr</code>.</p> 1637<p>If the child was spawned with <code>stdio[2]</code> set to anything other than <code>'pipe'</code>, 1638then this will be <code>null</code>.</p> 1639<p><code>subprocess.stderr</code> is an alias for <code>subprocess.stdio[2]</code>. Both properties will 1640refer to the same value.</p> 1641<p>The <code>subprocess.stderr</code> property can be <code>null</code> if the child process could 1642not be successfully spawned.</p> 1643<h4><code>subprocess.stdin</code><span><a class="mark" href="#child_process_subprocess_stdin" id="child_process_subprocess_stdin">#</a></span></h4> 1644<div class="api_metadata"> 1645<span>Added in: v0.1.90</span> 1646</div> 1647<ul> 1648<li><a href="stream.html#stream_class_stream_writable" class="type"><stream.Writable></a></li> 1649</ul> 1650<p>A <code>Writable Stream</code> that represents the child process's <code>stdin</code>.</p> 1651<p>If a child process waits to read all of its input, the child will not continue 1652until this stream has been closed via <code>end()</code>.</p> 1653<p>If the child was spawned with <code>stdio[0]</code> set to anything other than <code>'pipe'</code>, 1654then this will be <code>null</code>.</p> 1655<p><code>subprocess.stdin</code> is an alias for <code>subprocess.stdio[0]</code>. Both properties will 1656refer to the same value.</p> 1657<p>The <code>subprocess.stdin</code> property can be <code>undefined</code> if the child process could 1658not be successfully spawned.</p> 1659<h4><code>subprocess.stdio</code><span><a class="mark" href="#child_process_subprocess_stdio" id="child_process_subprocess_stdio">#</a></span></h4> 1660<div class="api_metadata"> 1661<span>Added in: v0.7.10</span> 1662</div> 1663<ul> 1664<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a></li> 1665</ul> 1666<p>A sparse array of pipes to the child process, corresponding with positions in 1667the <a href="#child_process_options_stdio"><code>stdio</code></a> option passed to <a href="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a> that have been set 1668to the value <code>'pipe'</code>. <code>subprocess.stdio[0]</code>, <code>subprocess.stdio[1]</code>, and 1669<code>subprocess.stdio[2]</code> are also available as <code>subprocess.stdin</code>, 1670<code>subprocess.stdout</code>, and <code>subprocess.stderr</code>, respectively.</p> 1671<p>In the following example, only the child's fd <code>1</code> (stdout) is configured as a 1672pipe, so only the parent's <code>subprocess.stdio[1]</code> is a stream, all other values 1673in the array are <code>null</code>.</p> 1674<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>); 1675<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>); 1676<span class="hljs-keyword">const</span> child_process = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>); 1677 1678<span class="hljs-keyword">const</span> subprocess = child_process.<span class="hljs-title function_">spawn</span>(<span class="hljs-string">'ls'</span>, { 1679 <span class="hljs-attr">stdio</span>: [ 1680 <span class="hljs-number">0</span>, <span class="hljs-comment">// Use parent's stdin for child.</span> 1681 <span class="hljs-string">'pipe'</span>, <span class="hljs-comment">// Pipe child's stdout to parent.</span> 1682 fs.<span class="hljs-title function_">openSync</span>(<span class="hljs-string">'err.out'</span>, <span class="hljs-string">'w'</span>), <span class="hljs-comment">// Direct child's stderr to a file.</span> 1683 ] 1684}); 1685 1686assert.<span class="hljs-title function_">strictEqual</span>(subprocess.<span class="hljs-property">stdio</span>[<span class="hljs-number">0</span>], <span class="hljs-literal">null</span>); 1687assert.<span class="hljs-title function_">strictEqual</span>(subprocess.<span class="hljs-property">stdio</span>[<span class="hljs-number">0</span>], subprocess.<span class="hljs-property">stdin</span>); 1688 1689<span class="hljs-title function_">assert</span>(subprocess.<span class="hljs-property">stdout</span>); 1690assert.<span class="hljs-title function_">strictEqual</span>(subprocess.<span class="hljs-property">stdio</span>[<span class="hljs-number">1</span>], subprocess.<span class="hljs-property">stdout</span>); 1691 1692assert.<span class="hljs-title function_">strictEqual</span>(subprocess.<span class="hljs-property">stdio</span>[<span class="hljs-number">2</span>], <span class="hljs-literal">null</span>); 1693assert.<span class="hljs-title function_">strictEqual</span>(subprocess.<span class="hljs-property">stdio</span>[<span class="hljs-number">2</span>], subprocess.<span class="hljs-property">stderr</span>);</code></pre> 1694<p>The <code>subprocess.stdio</code> property can be <code>undefined</code> if the child process could 1695not be successfully spawned.</p> 1696<h4><code>subprocess.stdout</code><span><a class="mark" href="#child_process_subprocess_stdout" id="child_process_subprocess_stdout">#</a></span></h4> 1697<div class="api_metadata"> 1698<span>Added in: v0.1.90</span> 1699</div> 1700<ul> 1701<li><a href="stream.html#stream_class_stream_readable" class="type"><stream.Readable></a></li> 1702</ul> 1703<p>A <code>Readable Stream</code> that represents the child process's <code>stdout</code>.</p> 1704<p>If the child was spawned with <code>stdio[1]</code> set to anything other than <code>'pipe'</code>, 1705then this will be <code>null</code>.</p> 1706<p><code>subprocess.stdout</code> is an alias for <code>subprocess.stdio[1]</code>. Both properties will 1707refer to the same value.</p> 1708<pre><code class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>); 1709 1710<span class="hljs-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span>(<span class="hljs-string">'ls'</span>); 1711 1712subprocess.<span class="hljs-property">stdout</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> { 1713 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Received chunk <span class="hljs-subst">${data}</span>`</span>); 1714});</code></pre> 1715<p>The <code>subprocess.stdout</code> property can be <code>null</code> if the child process could 1716not be successfully spawned.</p> 1717<h4><code>subprocess.unref()</code><span><a class="mark" href="#child_process_subprocess_unref" id="child_process_subprocess_unref">#</a></span></h4> 1718<div class="api_metadata"> 1719<span>Added in: v0.7.10</span> 1720</div> 1721<p>By default, the parent will wait for the detached child to exit. To prevent the 1722parent from waiting for a given <code>subprocess</code> to exit, use the 1723<code>subprocess.unref()</code> method. Doing so will cause the parent's event loop to not 1724include the child in its reference count, allowing the parent to exit 1725independently of the child, unless there is an established IPC channel between 1726the child and the parent.</p> 1727<pre><code class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'child_process'</span>); 1728 1729<span class="hljs-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span>(process.<span class="hljs-property">argv</span>[<span class="hljs-number">0</span>], [<span class="hljs-string">'child_program.js'</span>], { 1730 <span class="hljs-attr">detached</span>: <span class="hljs-literal">true</span>, 1731 <span class="hljs-attr">stdio</span>: <span class="hljs-string">'ignore'</span> 1732}); 1733 1734subprocess.<span class="hljs-title function_">unref</span>();</code></pre> 1735</section><section><h3><code>maxBuffer</code> and Unicode<span><a class="mark" href="#child_process_maxbuffer_and_unicode" id="child_process_maxbuffer_and_unicode">#</a></span></h3> 1736<p>The <code>maxBuffer</code> option specifies the largest number of bytes allowed on <code>stdout</code> 1737or <code>stderr</code>. If this value is exceeded, then the child process is terminated. 1738This impacts output that includes multibyte character encodings such as UTF-8 or 1739UTF-16. For instance, <code>console.log('中文测试')</code> will send 13 UTF-8 encoded bytes 1740to <code>stdout</code> although there are only 4 characters.</p> 1741</section><section><h3>Shell requirements<span><a class="mark" href="#child_process_shell_requirements" id="child_process_shell_requirements">#</a></span></h3> 1742<p>The shell should understand the <code>-c</code> switch. If the shell is <code>'cmd.exe'</code>, it 1743should understand the <code>/d /s /c</code> switches and command-line parsing should be 1744compatible.</p> 1745</section><section><h3>Default Windows shell<span><a class="mark" href="#child_process_default_windows_shell" id="child_process_default_windows_shell">#</a></span></h3> 1746<p>Although Microsoft specifies <code>%COMSPEC%</code> must contain the path to 1747<code>'cmd.exe'</code> in the root environment, child processes are not always subject to 1748the same requirement. Thus, in <code>child_process</code> functions where a shell can be 1749spawned, <code>'cmd.exe'</code> is used as a fallback if <code>process.env.ComSpec</code> is 1750unavailable.</p> 1751</section><section><h3>Advanced serialization<span><a class="mark" href="#child_process_advanced_serialization" id="child_process_advanced_serialization">#</a></span></h3> 1752<div class="api_metadata"> 1753<span>Added in: v13.2.0, v12.16.0</span> 1754</div> 1755<p>Child processes support a serialization mechanism for IPC that is based on the 1756<a href="v8.html#v8_serialization_api">serialization API of the <code>v8</code> module</a>, based on the 1757<a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm">HTML structured clone algorithm</a>. This is generally more powerful and 1758supports more built-in JavaScript object types, such as <code>BigInt</code>, <code>Map</code> 1759and <code>Set</code>, <code>ArrayBuffer</code> and <code>TypedArray</code>, <code>Buffer</code>, <code>Error</code>, <code>RegExp</code> etc.</p> 1760<p>However, this format is not a full superset of JSON, and e.g. properties set on 1761objects of such built-in types will not be passed on through the serialization 1762step. Additionally, performance may not be equivalent to that of JSON, depending 1763on the structure of the passed data. 1764Therefore, this feature requires opting in by setting the 1765<code>serialization</code> option to <code>'advanced'</code> when calling <a href="#child_process_child_process_spawn_command_args_options"><code>child_process.spawn()</code></a> 1766or <a href="#child_process_child_process_fork_modulepath_args_options"><code>child_process.fork()</code></a>.</p></section> 1767 <!-- API END --> 1768 </div> 1769 </div> 1770 </div> 1771 <script> 1772 'use strict'; 1773 { 1774 const kCustomPreference = 'customDarkTheme'; 1775 const userSettings = sessionStorage.getItem(kCustomPreference); 1776 const themeToggleButton = document.getElementById('theme-toggle-btn'); 1777 if (userSettings === null && window.matchMedia) { 1778 const mq = window.matchMedia('(prefers-color-scheme: dark)'); 1779 if ('onchange' in mq) { 1780 function mqChangeListener(e) { 1781 document.body.classList.toggle('dark-mode', e.matches); 1782 } 1783 mq.addEventListener('change', mqChangeListener); 1784 if (themeToggleButton) { 1785 themeToggleButton.addEventListener('click', function() { 1786 mq.removeEventListener('change', mqChangeListener); 1787 }, { once: true }); 1788 } 1789 } 1790 if (mq.matches) { 1791 document.body.classList.add('dark-mode'); 1792 } 1793 } else if (userSettings === 'true') { 1794 document.body.classList.add('dark-mode'); 1795 } 1796 if (themeToggleButton) { 1797 themeToggleButton.hidden = false; 1798 themeToggleButton.addEventListener('click', function() { 1799 sessionStorage.setItem( 1800 kCustomPreference, 1801 document.body.classList.toggle('dark-mode') 1802 ); 1803 }); 1804 } 1805 } 1806 </script> 1807</body> 1808</html> 1809