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