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