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