11cb0ef41Sopenharmony_ci<!DOCTYPE html>
21cb0ef41Sopenharmony_ci<html lang="en">
31cb0ef41Sopenharmony_ci<head>
41cb0ef41Sopenharmony_ci  <meta charset="utf-8">
51cb0ef41Sopenharmony_ci  <meta name="viewport" content="width=device-width">
61cb0ef41Sopenharmony_ci  <meta name="nodejs.org:node-version" content="v18.20.1">
71cb0ef41Sopenharmony_ci  <title>Child process | Node.js v18.20.1 Documentation</title>
81cb0ef41Sopenharmony_ci  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
91cb0ef41Sopenharmony_ci  <link rel="stylesheet" href="assets/style.css">
101cb0ef41Sopenharmony_ci  <link rel="stylesheet" href="assets/hljs.css">
111cb0ef41Sopenharmony_ci  <link rel="canonical" href="https://nodejs.org/api/child_process.html">
121cb0ef41Sopenharmony_ci  <script async defer src="assets/api.js" type="text/javascript"></script>
131cb0ef41Sopenharmony_ci  
141cb0ef41Sopenharmony_ci</head>
151cb0ef41Sopenharmony_ci<body class="alt apidoc" id="api-section-child_process">
161cb0ef41Sopenharmony_ci  <div id="content" class="clearfix">
171cb0ef41Sopenharmony_ci    <div id="column2" class="interior">
181cb0ef41Sopenharmony_ci      <div id="intro" class="interior">
191cb0ef41Sopenharmony_ci        <a href="/" title="Go back to the home page">
201cb0ef41Sopenharmony_ci          Node.js
211cb0ef41Sopenharmony_ci        </a>
221cb0ef41Sopenharmony_ci      </div>
231cb0ef41Sopenharmony_ci      <ul>
241cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
251cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
261cb0ef41Sopenharmony_ci</ul>
271cb0ef41Sopenharmony_ci<hr class="line">
281cb0ef41Sopenharmony_ci<ul>
291cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
301cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
311cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
321cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
331cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
341cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
351cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
361cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process active">Child processes</a></li>
371cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
381cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
391cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li>
401cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
411cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
421cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
431cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
441cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
451cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li>
461cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li>
471cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li>
481cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li>
491cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li>
501cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li>
511cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li>
521cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
531cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li>
541cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
551cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
561cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
571cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
581cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
591cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
601cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li>
611cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li>
621cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li>
631cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
641cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
651cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li>
661cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
671cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
681cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li>
691cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li>
701cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li>
711cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
721cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li>
731cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
741cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li>
751cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li>
761cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
771cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
781cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li>
791cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
801cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li>
811cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li>
821cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li>
831cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li>
841cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
851cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
861cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
871cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
881cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
891cb0ef41Sopenharmony_ci</ul>
901cb0ef41Sopenharmony_ci<hr class="line">
911cb0ef41Sopenharmony_ci<ul>
921cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
931cb0ef41Sopenharmony_ci</ul>
941cb0ef41Sopenharmony_ci    </div>
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci    <div id="column1" data-id="child_process" class="interior">
971cb0ef41Sopenharmony_ci      <header class="header">
981cb0ef41Sopenharmony_ci        <div class="header-container">
991cb0ef41Sopenharmony_ci          <h1>Node.js v18.20.1 documentation</h1>
1001cb0ef41Sopenharmony_ci          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
1011cb0ef41Sopenharmony_ci            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
1021cb0ef41Sopenharmony_ci              <path fill="none" d="M0 0h24v24H0z" />
1031cb0ef41Sopenharmony_ci              <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"/>
1041cb0ef41Sopenharmony_ci              <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"/>
1051cb0ef41Sopenharmony_ci            </svg>
1061cb0ef41Sopenharmony_ci            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
1071cb0ef41Sopenharmony_ci              <path d="M0 0h24v24H0z" fill="none" />
1081cb0ef41Sopenharmony_ci              <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"/>
1091cb0ef41Sopenharmony_ci            </svg>
1101cb0ef41Sopenharmony_ci          </button>
1111cb0ef41Sopenharmony_ci        </div>
1121cb0ef41Sopenharmony_ci        <div id="gtoc">
1131cb0ef41Sopenharmony_ci          <ul>
1141cb0ef41Sopenharmony_ci            <li class="pinned-header">Node.js v18.20.1</li>
1151cb0ef41Sopenharmony_ci            
1161cb0ef41Sopenharmony_ci    <li class="picker-header">
1171cb0ef41Sopenharmony_ci      <a href="#">
1181cb0ef41Sopenharmony_ci        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
1191cb0ef41Sopenharmony_ci        Table of contents
1201cb0ef41Sopenharmony_ci      </a>
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci      <div class="picker"><div class="toc"><ul>
1231cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#child-process">Child process</a></span>
1241cb0ef41Sopenharmony_ci<ul>
1251cb0ef41Sopenharmony_ci<li><a href="#asynchronous-process-creation">Asynchronous process creation</a>
1261cb0ef41Sopenharmony_ci<ul>
1271cb0ef41Sopenharmony_ci<li><a href="#spawning-bat-and-cmd-files-on-windows">Spawning <code>.bat</code> and <code>.cmd</code> files on Windows</a></li>
1281cb0ef41Sopenharmony_ci<li><a href="#child_processexeccommand-options-callback"><code>child_process.exec(command[, options][, callback])</code></a></li>
1291cb0ef41Sopenharmony_ci<li><a href="#child_processexecfilefile-args-options-callback"><code>child_process.execFile(file[, args][, options][, callback])</code></a></li>
1301cb0ef41Sopenharmony_ci<li><a href="#child_processforkmodulepath-args-options"><code>child_process.fork(modulePath[, args][, options])</code></a></li>
1311cb0ef41Sopenharmony_ci<li><a href="#child_processspawncommand-args-options"><code>child_process.spawn(command[, args][, options])</code></a>
1321cb0ef41Sopenharmony_ci<ul>
1331cb0ef41Sopenharmony_ci<li><a href="#optionsdetached"><code>options.detached</code></a></li>
1341cb0ef41Sopenharmony_ci<li><a href="#optionsstdio"><code>options.stdio</code></a></li>
1351cb0ef41Sopenharmony_ci</ul>
1361cb0ef41Sopenharmony_ci</li>
1371cb0ef41Sopenharmony_ci</ul>
1381cb0ef41Sopenharmony_ci</li>
1391cb0ef41Sopenharmony_ci<li><a href="#synchronous-process-creation">Synchronous process creation</a>
1401cb0ef41Sopenharmony_ci<ul>
1411cb0ef41Sopenharmony_ci<li><a href="#child_processexecfilesyncfile-args-options"><code>child_process.execFileSync(file[, args][, options])</code></a></li>
1421cb0ef41Sopenharmony_ci<li><a href="#child_processexecsynccommand-options"><code>child_process.execSync(command[, options])</code></a></li>
1431cb0ef41Sopenharmony_ci<li><a href="#child_processspawnsynccommand-args-options"><code>child_process.spawnSync(command[, args][, options])</code></a></li>
1441cb0ef41Sopenharmony_ci</ul>
1451cb0ef41Sopenharmony_ci</li>
1461cb0ef41Sopenharmony_ci<li><a href="#class-childprocess">Class: <code>ChildProcess</code></a>
1471cb0ef41Sopenharmony_ci<ul>
1481cb0ef41Sopenharmony_ci<li><a href="#event-close">Event: <code>'close'</code></a></li>
1491cb0ef41Sopenharmony_ci<li><a href="#event-disconnect">Event: <code>'disconnect'</code></a></li>
1501cb0ef41Sopenharmony_ci<li><a href="#event-error">Event: <code>'error'</code></a></li>
1511cb0ef41Sopenharmony_ci<li><a href="#event-exit">Event: <code>'exit'</code></a></li>
1521cb0ef41Sopenharmony_ci<li><a href="#event-message">Event: <code>'message'</code></a></li>
1531cb0ef41Sopenharmony_ci<li><a href="#event-spawn">Event: <code>'spawn'</code></a></li>
1541cb0ef41Sopenharmony_ci<li><a href="#subprocesschannel"><code>subprocess.channel</code></a>
1551cb0ef41Sopenharmony_ci<ul>
1561cb0ef41Sopenharmony_ci<li><a href="#subprocesschannelref"><code>subprocess.channel.ref()</code></a></li>
1571cb0ef41Sopenharmony_ci<li><a href="#subprocesschannelunref"><code>subprocess.channel.unref()</code></a></li>
1581cb0ef41Sopenharmony_ci</ul>
1591cb0ef41Sopenharmony_ci</li>
1601cb0ef41Sopenharmony_ci<li><a href="#subprocessconnected"><code>subprocess.connected</code></a></li>
1611cb0ef41Sopenharmony_ci<li><a href="#subprocessdisconnect"><code>subprocess.disconnect()</code></a></li>
1621cb0ef41Sopenharmony_ci<li><a href="#subprocessexitcode"><code>subprocess.exitCode</code></a></li>
1631cb0ef41Sopenharmony_ci<li><a href="#subprocesskillsignal"><code>subprocess.kill([signal])</code></a></li>
1641cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#subprocesssymboldispose"><code>subprocess[Symbol.dispose]()</code></a></span></li>
1651cb0ef41Sopenharmony_ci<li><a href="#subprocesskilled"><code>subprocess.killed</code></a></li>
1661cb0ef41Sopenharmony_ci<li><a href="#subprocesspid"><code>subprocess.pid</code></a></li>
1671cb0ef41Sopenharmony_ci<li><a href="#subprocessref"><code>subprocess.ref()</code></a></li>
1681cb0ef41Sopenharmony_ci<li><a href="#subprocesssendmessage-sendhandle-options-callback"><code>subprocess.send(message[, sendHandle[, options]][, callback])</code></a>
1691cb0ef41Sopenharmony_ci<ul>
1701cb0ef41Sopenharmony_ci<li><a href="#example-sending-a-server-object">Example: sending a server object</a></li>
1711cb0ef41Sopenharmony_ci<li><a href="#example-sending-a-socket-object">Example: sending a socket object</a></li>
1721cb0ef41Sopenharmony_ci</ul>
1731cb0ef41Sopenharmony_ci</li>
1741cb0ef41Sopenharmony_ci<li><a href="#subprocesssignalcode"><code>subprocess.signalCode</code></a></li>
1751cb0ef41Sopenharmony_ci<li><a href="#subprocessspawnargs"><code>subprocess.spawnargs</code></a></li>
1761cb0ef41Sopenharmony_ci<li><a href="#subprocessspawnfile"><code>subprocess.spawnfile</code></a></li>
1771cb0ef41Sopenharmony_ci<li><a href="#subprocessstderr"><code>subprocess.stderr</code></a></li>
1781cb0ef41Sopenharmony_ci<li><a href="#subprocessstdin"><code>subprocess.stdin</code></a></li>
1791cb0ef41Sopenharmony_ci<li><a href="#subprocessstdio"><code>subprocess.stdio</code></a></li>
1801cb0ef41Sopenharmony_ci<li><a href="#subprocessstdout"><code>subprocess.stdout</code></a></li>
1811cb0ef41Sopenharmony_ci<li><a href="#subprocessunref"><code>subprocess.unref()</code></a></li>
1821cb0ef41Sopenharmony_ci</ul>
1831cb0ef41Sopenharmony_ci</li>
1841cb0ef41Sopenharmony_ci<li><a href="#maxbuffer-and-unicode"><code>maxBuffer</code> and Unicode</a></li>
1851cb0ef41Sopenharmony_ci<li><a href="#shell-requirements">Shell requirements</a></li>
1861cb0ef41Sopenharmony_ci<li><a href="#default-windows-shell">Default Windows shell</a></li>
1871cb0ef41Sopenharmony_ci<li><a href="#advanced-serialization">Advanced serialization</a></li>
1881cb0ef41Sopenharmony_ci</ul>
1891cb0ef41Sopenharmony_ci</li>
1901cb0ef41Sopenharmony_ci</ul></div></div>
1911cb0ef41Sopenharmony_ci    </li>
1921cb0ef41Sopenharmony_ci  
1931cb0ef41Sopenharmony_ci            
1941cb0ef41Sopenharmony_ci    <li class="picker-header">
1951cb0ef41Sopenharmony_ci      <a href="#">
1961cb0ef41Sopenharmony_ci        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
1971cb0ef41Sopenharmony_ci        Index
1981cb0ef41Sopenharmony_ci      </a>
1991cb0ef41Sopenharmony_ci
2001cb0ef41Sopenharmony_ci      <div class="picker"><ul>
2011cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
2021cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
2031cb0ef41Sopenharmony_ci
2041cb0ef41Sopenharmony_ci      <li>
2051cb0ef41Sopenharmony_ci        <a href="index.html">Index</a>
2061cb0ef41Sopenharmony_ci      </li>
2071cb0ef41Sopenharmony_ci    </ul>
2081cb0ef41Sopenharmony_ci  
2091cb0ef41Sopenharmony_ci<hr class="line">
2101cb0ef41Sopenharmony_ci<ul>
2111cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
2121cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
2131cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
2141cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
2151cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
2161cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
2171cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
2181cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process active">Child processes</a></li>
2191cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
2201cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
2211cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li>
2221cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
2231cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
2241cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
2251cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
2261cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
2271cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li>
2281cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li>
2291cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li>
2301cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li>
2311cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li>
2321cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li>
2331cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li>
2341cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
2351cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li>
2361cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
2371cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
2381cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
2391cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
2401cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
2411cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
2421cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li>
2431cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li>
2441cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li>
2451cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
2461cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
2471cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li>
2481cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
2491cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
2501cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li>
2511cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li>
2521cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li>
2531cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
2541cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li>
2551cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
2561cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li>
2571cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li>
2581cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
2591cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
2601cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li>
2611cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
2621cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li>
2631cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li>
2641cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li>
2651cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li>
2661cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
2671cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
2681cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
2691cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
2701cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
2711cb0ef41Sopenharmony_ci</ul>
2721cb0ef41Sopenharmony_ci<hr class="line">
2731cb0ef41Sopenharmony_ci<ul>
2741cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
2751cb0ef41Sopenharmony_ci</ul></div>
2761cb0ef41Sopenharmony_ci    </li>
2771cb0ef41Sopenharmony_ci  
2781cb0ef41Sopenharmony_ci            
2791cb0ef41Sopenharmony_ci    <li class="picker-header">
2801cb0ef41Sopenharmony_ci      <a href="#">
2811cb0ef41Sopenharmony_ci        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
2821cb0ef41Sopenharmony_ci        Other versions
2831cb0ef41Sopenharmony_ci      </a>
2841cb0ef41Sopenharmony_ci      <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>
2851cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v20.x/api/child_process.html">20.x <b>LTS</b></a></li>
2861cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v19.x/api/child_process.html">19.x</a></li>
2871cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v18.x/api/child_process.html">18.x <b>LTS</b></a></li>
2881cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v17.x/api/child_process.html">17.x</a></li>
2891cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v16.x/api/child_process.html">16.x</a></li>
2901cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v15.x/api/child_process.html">15.x</a></li>
2911cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v14.x/api/child_process.html">14.x</a></li>
2921cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v13.x/api/child_process.html">13.x</a></li>
2931cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v12.x/api/child_process.html">12.x</a></li>
2941cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v11.x/api/child_process.html">11.x</a></li>
2951cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v10.x/api/child_process.html">10.x</a></li>
2961cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v9.x/api/child_process.html">9.x</a></li>
2971cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v8.x/api/child_process.html">8.x</a></li>
2981cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v7.x/api/child_process.html">7.x</a></li>
2991cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v6.x/api/child_process.html">6.x</a></li>
3001cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v5.x/api/child_process.html">5.x</a></li>
3011cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v4.x/api/child_process.html">4.x</a></li>
3021cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/child_process.html">0.12.x</a></li>
3031cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/child_process.html">0.10.x</a></li></ol></div>
3041cb0ef41Sopenharmony_ci    </li>
3051cb0ef41Sopenharmony_ci  
3061cb0ef41Sopenharmony_ci            <li class="picker-header">
3071cb0ef41Sopenharmony_ci              <a href="#">
3081cb0ef41Sopenharmony_ci                <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
3091cb0ef41Sopenharmony_ci                Options
3101cb0ef41Sopenharmony_ci              </a>
3111cb0ef41Sopenharmony_ci        
3121cb0ef41Sopenharmony_ci              <div class="picker">
3131cb0ef41Sopenharmony_ci                <ul>
3141cb0ef41Sopenharmony_ci                  <li>
3151cb0ef41Sopenharmony_ci                    <a href="all.html">View on single page</a>
3161cb0ef41Sopenharmony_ci                  </li>
3171cb0ef41Sopenharmony_ci                  <li>
3181cb0ef41Sopenharmony_ci                    <a href="child_process.json">View as JSON</a>
3191cb0ef41Sopenharmony_ci                  </li>
3201cb0ef41Sopenharmony_ci                  <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/child_process.md">Edit on GitHub</a></li>    
3211cb0ef41Sopenharmony_ci                </ul>
3221cb0ef41Sopenharmony_ci              </div>
3231cb0ef41Sopenharmony_ci            </li>
3241cb0ef41Sopenharmony_ci          </ul>
3251cb0ef41Sopenharmony_ci        </div>
3261cb0ef41Sopenharmony_ci        <hr>
3271cb0ef41Sopenharmony_ci      </header>
3281cb0ef41Sopenharmony_ci
3291cb0ef41Sopenharmony_ci      <details id="toc" open><summary>Table of contents</summary><ul>
3301cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#child-process">Child process</a></span>
3311cb0ef41Sopenharmony_ci<ul>
3321cb0ef41Sopenharmony_ci<li><a href="#asynchronous-process-creation">Asynchronous process creation</a>
3331cb0ef41Sopenharmony_ci<ul>
3341cb0ef41Sopenharmony_ci<li><a href="#spawning-bat-and-cmd-files-on-windows">Spawning <code>.bat</code> and <code>.cmd</code> files on Windows</a></li>
3351cb0ef41Sopenharmony_ci<li><a href="#child_processexeccommand-options-callback"><code>child_process.exec(command[, options][, callback])</code></a></li>
3361cb0ef41Sopenharmony_ci<li><a href="#child_processexecfilefile-args-options-callback"><code>child_process.execFile(file[, args][, options][, callback])</code></a></li>
3371cb0ef41Sopenharmony_ci<li><a href="#child_processforkmodulepath-args-options"><code>child_process.fork(modulePath[, args][, options])</code></a></li>
3381cb0ef41Sopenharmony_ci<li><a href="#child_processspawncommand-args-options"><code>child_process.spawn(command[, args][, options])</code></a>
3391cb0ef41Sopenharmony_ci<ul>
3401cb0ef41Sopenharmony_ci<li><a href="#optionsdetached"><code>options.detached</code></a></li>
3411cb0ef41Sopenharmony_ci<li><a href="#optionsstdio"><code>options.stdio</code></a></li>
3421cb0ef41Sopenharmony_ci</ul>
3431cb0ef41Sopenharmony_ci</li>
3441cb0ef41Sopenharmony_ci</ul>
3451cb0ef41Sopenharmony_ci</li>
3461cb0ef41Sopenharmony_ci<li><a href="#synchronous-process-creation">Synchronous process creation</a>
3471cb0ef41Sopenharmony_ci<ul>
3481cb0ef41Sopenharmony_ci<li><a href="#child_processexecfilesyncfile-args-options"><code>child_process.execFileSync(file[, args][, options])</code></a></li>
3491cb0ef41Sopenharmony_ci<li><a href="#child_processexecsynccommand-options"><code>child_process.execSync(command[, options])</code></a></li>
3501cb0ef41Sopenharmony_ci<li><a href="#child_processspawnsynccommand-args-options"><code>child_process.spawnSync(command[, args][, options])</code></a></li>
3511cb0ef41Sopenharmony_ci</ul>
3521cb0ef41Sopenharmony_ci</li>
3531cb0ef41Sopenharmony_ci<li><a href="#class-childprocess">Class: <code>ChildProcess</code></a>
3541cb0ef41Sopenharmony_ci<ul>
3551cb0ef41Sopenharmony_ci<li><a href="#event-close">Event: <code>'close'</code></a></li>
3561cb0ef41Sopenharmony_ci<li><a href="#event-disconnect">Event: <code>'disconnect'</code></a></li>
3571cb0ef41Sopenharmony_ci<li><a href="#event-error">Event: <code>'error'</code></a></li>
3581cb0ef41Sopenharmony_ci<li><a href="#event-exit">Event: <code>'exit'</code></a></li>
3591cb0ef41Sopenharmony_ci<li><a href="#event-message">Event: <code>'message'</code></a></li>
3601cb0ef41Sopenharmony_ci<li><a href="#event-spawn">Event: <code>'spawn'</code></a></li>
3611cb0ef41Sopenharmony_ci<li><a href="#subprocesschannel"><code>subprocess.channel</code></a>
3621cb0ef41Sopenharmony_ci<ul>
3631cb0ef41Sopenharmony_ci<li><a href="#subprocesschannelref"><code>subprocess.channel.ref()</code></a></li>
3641cb0ef41Sopenharmony_ci<li><a href="#subprocesschannelunref"><code>subprocess.channel.unref()</code></a></li>
3651cb0ef41Sopenharmony_ci</ul>
3661cb0ef41Sopenharmony_ci</li>
3671cb0ef41Sopenharmony_ci<li><a href="#subprocessconnected"><code>subprocess.connected</code></a></li>
3681cb0ef41Sopenharmony_ci<li><a href="#subprocessdisconnect"><code>subprocess.disconnect()</code></a></li>
3691cb0ef41Sopenharmony_ci<li><a href="#subprocessexitcode"><code>subprocess.exitCode</code></a></li>
3701cb0ef41Sopenharmony_ci<li><a href="#subprocesskillsignal"><code>subprocess.kill([signal])</code></a></li>
3711cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#subprocesssymboldispose"><code>subprocess[Symbol.dispose]()</code></a></span></li>
3721cb0ef41Sopenharmony_ci<li><a href="#subprocesskilled"><code>subprocess.killed</code></a></li>
3731cb0ef41Sopenharmony_ci<li><a href="#subprocesspid"><code>subprocess.pid</code></a></li>
3741cb0ef41Sopenharmony_ci<li><a href="#subprocessref"><code>subprocess.ref()</code></a></li>
3751cb0ef41Sopenharmony_ci<li><a href="#subprocesssendmessage-sendhandle-options-callback"><code>subprocess.send(message[, sendHandle[, options]][, callback])</code></a>
3761cb0ef41Sopenharmony_ci<ul>
3771cb0ef41Sopenharmony_ci<li><a href="#example-sending-a-server-object">Example: sending a server object</a></li>
3781cb0ef41Sopenharmony_ci<li><a href="#example-sending-a-socket-object">Example: sending a socket object</a></li>
3791cb0ef41Sopenharmony_ci</ul>
3801cb0ef41Sopenharmony_ci</li>
3811cb0ef41Sopenharmony_ci<li><a href="#subprocesssignalcode"><code>subprocess.signalCode</code></a></li>
3821cb0ef41Sopenharmony_ci<li><a href="#subprocessspawnargs"><code>subprocess.spawnargs</code></a></li>
3831cb0ef41Sopenharmony_ci<li><a href="#subprocessspawnfile"><code>subprocess.spawnfile</code></a></li>
3841cb0ef41Sopenharmony_ci<li><a href="#subprocessstderr"><code>subprocess.stderr</code></a></li>
3851cb0ef41Sopenharmony_ci<li><a href="#subprocessstdin"><code>subprocess.stdin</code></a></li>
3861cb0ef41Sopenharmony_ci<li><a href="#subprocessstdio"><code>subprocess.stdio</code></a></li>
3871cb0ef41Sopenharmony_ci<li><a href="#subprocessstdout"><code>subprocess.stdout</code></a></li>
3881cb0ef41Sopenharmony_ci<li><a href="#subprocessunref"><code>subprocess.unref()</code></a></li>
3891cb0ef41Sopenharmony_ci</ul>
3901cb0ef41Sopenharmony_ci</li>
3911cb0ef41Sopenharmony_ci<li><a href="#maxbuffer-and-unicode"><code>maxBuffer</code> and Unicode</a></li>
3921cb0ef41Sopenharmony_ci<li><a href="#shell-requirements">Shell requirements</a></li>
3931cb0ef41Sopenharmony_ci<li><a href="#default-windows-shell">Default Windows shell</a></li>
3941cb0ef41Sopenharmony_ci<li><a href="#advanced-serialization">Advanced serialization</a></li>
3951cb0ef41Sopenharmony_ci</ul>
3961cb0ef41Sopenharmony_ci</li>
3971cb0ef41Sopenharmony_ci</ul></details>
3981cb0ef41Sopenharmony_ci
3991cb0ef41Sopenharmony_ci      <div id="apicontent">
4001cb0ef41Sopenharmony_ci        <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>
4011cb0ef41Sopenharmony_ci
4021cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p>
4031cb0ef41Sopenharmony_ci<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>
4041cb0ef41Sopenharmony_ci<p>The <code>node:child_process</code> module provides the ability to spawn subprocesses in
4051cb0ef41Sopenharmony_cia 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
4061cb0ef41Sopenharmony_ciis primarily provided by the <a href="#child_processspawncommand-args-options"><code>child_process.spawn()</code></a> function:</p>
4071cb0ef41Sopenharmony_ci<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>);
4081cb0ef41Sopenharmony_ci<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>]);
4091cb0ef41Sopenharmony_ci
4101cb0ef41Sopenharmony_cils.<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> {
4111cb0ef41Sopenharmony_ci  <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>);
4121cb0ef41Sopenharmony_ci});
4131cb0ef41Sopenharmony_ci
4141cb0ef41Sopenharmony_cils.<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> {
4151cb0ef41Sopenharmony_ci  <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>);
4161cb0ef41Sopenharmony_ci});
4171cb0ef41Sopenharmony_ci
4181cb0ef41Sopenharmony_cils.<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-function">(<span class="hljs-params">code</span>) =></span> {
4191cb0ef41Sopenharmony_ci  <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>);
4201cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
4211cb0ef41Sopenharmony_ci<p>By default, pipes for <code>stdin</code>, <code>stdout</code>, and <code>stderr</code> are established between
4221cb0ef41Sopenharmony_cithe parent Node.js process and the spawned subprocess. These pipes have
4231cb0ef41Sopenharmony_cilimited (and platform-specific) capacity. If the subprocess writes to
4241cb0ef41Sopenharmony_cistdout in excess of that limit without the output being captured, the
4251cb0ef41Sopenharmony_cisubprocess blocks waiting for the pipe buffer to accept more data. This is
4261cb0ef41Sopenharmony_ciidentical to the behavior of pipes in the shell. Use the <code>{ stdio: 'ignore' }</code>
4271cb0ef41Sopenharmony_cioption if the output will not be consumed.</p>
4281cb0ef41Sopenharmony_ci<p>The command lookup is performed using the <code>options.env.PATH</code> environment
4291cb0ef41Sopenharmony_civariable if <code>env</code> is in the <code>options</code> object. Otherwise, <code>process.env.PATH</code> is
4301cb0ef41Sopenharmony_ciused. If <code>options.env</code> is set without <code>PATH</code>, lookup on Unix is performed
4311cb0ef41Sopenharmony_cion a default search path search of <code>/usr/bin:/bin</code> (see your operating system's
4321cb0ef41Sopenharmony_cimanual for execvpe/execvp), on Windows the current processes environment
4331cb0ef41Sopenharmony_civariable <code>PATH</code> is used.</p>
4341cb0ef41Sopenharmony_ci<p>On Windows, environment variables are case-insensitive. Node.js
4351cb0ef41Sopenharmony_cilexicographically sorts the <code>env</code> keys and uses the first one that
4361cb0ef41Sopenharmony_cicase-insensitively matches. Only first (in lexicographic order) entry will be
4371cb0ef41Sopenharmony_cipassed to the subprocess. This might lead to issues on Windows when passing
4381cb0ef41Sopenharmony_ciobjects to the <code>env</code> option that have multiple variants of the same key, such as
4391cb0ef41Sopenharmony_ci<code>PATH</code> and <code>Path</code>.</p>
4401cb0ef41Sopenharmony_ci<p>The <a href="#child_processspawncommand-args-options"><code>child_process.spawn()</code></a> method spawns the child process asynchronously,
4411cb0ef41Sopenharmony_ciwithout blocking the Node.js event loop. The <a href="#child_processspawnsynccommand-args-options"><code>child_process.spawnSync()</code></a>
4421cb0ef41Sopenharmony_cifunction provides equivalent functionality in a synchronous manner that blocks
4431cb0ef41Sopenharmony_cithe event loop until the spawned process either exits or is terminated.</p>
4441cb0ef41Sopenharmony_ci<p>For convenience, the <code>node:child_process</code> module provides a handful of
4451cb0ef41Sopenharmony_cisynchronous and asynchronous alternatives to <a href="#child_processspawncommand-args-options"><code>child_process.spawn()</code></a> and
4461cb0ef41Sopenharmony_ci<a href="#child_processspawnsynccommand-args-options"><code>child_process.spawnSync()</code></a>. Each of these alternatives are implemented on
4471cb0ef41Sopenharmony_citop 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>
4481cb0ef41Sopenharmony_ci<ul>
4491cb0ef41Sopenharmony_ci<li><a href="#child_processexeccommand-options-callback"><code>child_process.exec()</code></a>: spawns a shell and runs a command within that
4501cb0ef41Sopenharmony_cishell, passing the <code>stdout</code> and <code>stderr</code> to a callback function when
4511cb0ef41Sopenharmony_cicomplete.</li>
4521cb0ef41Sopenharmony_ci<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
4531cb0ef41Sopenharmony_cithat it spawns the command directly without first spawning a shell by
4541cb0ef41Sopenharmony_cidefault.</li>
4551cb0ef41Sopenharmony_ci<li><a href="#child_processforkmodulepath-args-options"><code>child_process.fork()</code></a>: spawns a new Node.js process and invokes a
4561cb0ef41Sopenharmony_cispecified module with an IPC communication channel established that allows
4571cb0ef41Sopenharmony_cisending messages between parent and child.</li>
4581cb0ef41Sopenharmony_ci<li><a href="#child_processexecsynccommand-options"><code>child_process.execSync()</code></a>: a synchronous version of
4591cb0ef41Sopenharmony_ci<a href="#child_processexeccommand-options-callback"><code>child_process.exec()</code></a> that will block the Node.js event loop.</li>
4601cb0ef41Sopenharmony_ci<li><a href="#child_processexecfilesyncfile-args-options"><code>child_process.execFileSync()</code></a>: a synchronous version of
4611cb0ef41Sopenharmony_ci<a href="#child_processexecfilefile-args-options-callback"><code>child_process.execFile()</code></a> that will block the Node.js event loop.</li>
4621cb0ef41Sopenharmony_ci</ul>
4631cb0ef41Sopenharmony_ci<p>For certain use cases, such as automating shell scripts, the
4641cb0ef41Sopenharmony_ci<a href="#synchronous-process-creation">synchronous counterparts</a> may be more convenient. In many cases, however,
4651cb0ef41Sopenharmony_cithe synchronous methods can have significant impact on performance due to
4661cb0ef41Sopenharmony_cistalling the event loop while spawned processes complete.</p>
4671cb0ef41Sopenharmony_ci<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>
4681cb0ef41Sopenharmony_ci<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>,
4691cb0ef41Sopenharmony_ciand <a href="#child_processexecfilefile-args-options-callback"><code>child_process.execFile()</code></a> methods all follow the idiomatic asynchronous
4701cb0ef41Sopenharmony_ciprogramming pattern typical of other Node.js APIs.</p>
4711cb0ef41Sopenharmony_ci<p>Each of the methods returns a <a href="#class-childprocess"><code>ChildProcess</code></a> instance. These objects
4721cb0ef41Sopenharmony_ciimplement the Node.js <a href="events.html#class-eventemitter"><code>EventEmitter</code></a> API, allowing the parent process to
4731cb0ef41Sopenharmony_ciregister listener functions that are called when certain events occur during
4741cb0ef41Sopenharmony_cithe life cycle of the child process.</p>
4751cb0ef41Sopenharmony_ci<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
4761cb0ef41Sopenharmony_ciadditionally allow for an optional <code>callback</code> function to be specified that is
4771cb0ef41Sopenharmony_ciinvoked when the child process terminates.</p>
4781cb0ef41Sopenharmony_ci<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>
4791cb0ef41Sopenharmony_ci<p>The importance of the distinction between <a href="#child_processexeccommand-options-callback"><code>child_process.exec()</code></a> and
4801cb0ef41Sopenharmony_ci<a href="#child_processexecfilefile-args-options-callback"><code>child_process.execFile()</code></a> can vary based on platform. On Unix-type
4811cb0ef41Sopenharmony_cioperating systems (Unix, Linux, macOS) <a href="#child_processexecfilefile-args-options-callback"><code>child_process.execFile()</code></a> can be
4821cb0ef41Sopenharmony_cimore efficient because it does not spawn a shell by default. On Windows,
4831cb0ef41Sopenharmony_cihowever, <code>.bat</code> and <code>.cmd</code> files are not executable on their own without a
4841cb0ef41Sopenharmony_citerminal, and therefore cannot be launched using <a href="#child_processexecfilefile-args-options-callback"><code>child_process.execFile()</code></a>.
4851cb0ef41Sopenharmony_ciWhen running on Windows, <code>.bat</code> and <code>.cmd</code> files can be invoked using
4861cb0ef41Sopenharmony_ci<a href="#child_processspawncommand-args-options"><code>child_process.spawn()</code></a> with the <code>shell</code> option set, with
4871cb0ef41Sopenharmony_ci<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
4881cb0ef41Sopenharmony_ci<code>.cmd</code> file as an argument (which is what the <code>shell</code> option and
4891cb0ef41Sopenharmony_ci<a href="#child_processexeccommand-options-callback"><code>child_process.exec()</code></a> do). In any case, if the script filename contains
4901cb0ef41Sopenharmony_cispaces it needs to be quoted.</p>
4911cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// On Windows Only...</span>
4921cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:child_process'</span>);
4931cb0ef41Sopenharmony_ci<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>]);
4941cb0ef41Sopenharmony_ci
4951cb0ef41Sopenharmony_cibat.<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> {
4961cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(data.<span class="hljs-title function_">toString</span>());
4971cb0ef41Sopenharmony_ci});
4981cb0ef41Sopenharmony_ci
4991cb0ef41Sopenharmony_cibat.<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> {
5001cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(data.<span class="hljs-title function_">toString</span>());
5011cb0ef41Sopenharmony_ci});
5021cb0ef41Sopenharmony_ci
5031cb0ef41Sopenharmony_cibat.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">(<span class="hljs-params">code</span>) =></span> {
5041cb0ef41Sopenharmony_ci  <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>);
5051cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
5061cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// OR...</span>
5071cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { exec, spawn } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:child_process'</span>);
5081cb0ef41Sopenharmony_ci<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> {
5091cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (err) {
5101cb0ef41Sopenharmony_ci    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err);
5111cb0ef41Sopenharmony_ci    <span class="hljs-keyword">return</span>;
5121cb0ef41Sopenharmony_ci  }
5131cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(stdout);
5141cb0ef41Sopenharmony_ci});
5151cb0ef41Sopenharmony_ci
5161cb0ef41Sopenharmony_ci<span class="hljs-comment">// Script with spaces in the filename:</span>
5171cb0ef41Sopenharmony_ci<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> });
5181cb0ef41Sopenharmony_ci<span class="hljs-comment">// or:</span>
5191cb0ef41Sopenharmony_ci<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> {
5201cb0ef41Sopenharmony_ci  <span class="hljs-comment">// ...</span>
5211cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
5221cb0ef41Sopenharmony_ci<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>
5231cb0ef41Sopenharmony_ci<div class="api_metadata">
5241cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
5251cb0ef41Sopenharmony_ci<table>
5261cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
5271cb0ef41Sopenharmony_ci<tr><td>v15.4.0</td>
5281cb0ef41Sopenharmony_ci<td><p>AbortSignal support was added.</p></td></tr>
5291cb0ef41Sopenharmony_ci<tr><td>v16.4.0, v14.18.0</td>
5301cb0ef41Sopenharmony_ci<td><p>The <code>cwd</code> option can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
5311cb0ef41Sopenharmony_ci<tr><td>v8.8.0</td>
5321cb0ef41Sopenharmony_ci<td><p>The <code>windowsHide</code> option is supported now.</p></td></tr>
5331cb0ef41Sopenharmony_ci<tr><td>v0.1.90</td>
5341cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.90</span></p></td></tr>
5351cb0ef41Sopenharmony_ci</tbody></table>
5361cb0ef41Sopenharmony_ci</details>
5371cb0ef41Sopenharmony_ci</div>
5381cb0ef41Sopenharmony_ci<ul>
5391cb0ef41Sopenharmony_ci<li><code>command</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The command to run, with space-separated arguments.</li>
5401cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
5411cb0ef41Sopenharmony_ci<ul>
5421cb0ef41Sopenharmony_ci<li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> Current working directory of the child process.
5431cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>process.cwd()</code>.</li>
5441cb0ef41Sopenharmony_ci<li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> Environment key-value pairs. <strong>Default:</strong> <code>process.env</code>.</li>
5451cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>'utf8'</code></li>
5461cb0ef41Sopenharmony_ci<li><code>shell</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Shell to execute the command with. See
5471cb0ef41Sopenharmony_ci<a href="#shell-requirements">Shell requirements</a> and <a href="#default-windows-shell">Default Windows shell</a>. <strong>Default:</strong>
5481cb0ef41Sopenharmony_ci<code>'/bin/sh'</code> on Unix, <code>process.env.ComSpec</code> on Windows.</li>
5491cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type">&#x3C;AbortSignal></a> allows aborting the child process using an
5501cb0ef41Sopenharmony_ciAbortSignal.</li>
5511cb0ef41Sopenharmony_ci<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <strong>Default:</strong> <code>0</code></li>
5521cb0ef41Sopenharmony_ci<li><code>maxBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Largest amount of data in bytes allowed on stdout or
5531cb0ef41Sopenharmony_cistderr. If exceeded, the child process is terminated and any output is
5541cb0ef41Sopenharmony_citruncated. See caveat at <a href="#maxbuffer-and-unicode"><code>maxBuffer</code> and Unicode</a>.
5551cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>1024 * 1024</code>.</li>
5561cb0ef41Sopenharmony_ci<li><code>killSignal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>'SIGTERM'</code></li>
5571cb0ef41Sopenharmony_ci<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;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>
5581cb0ef41Sopenharmony_ci<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;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>
5591cb0ef41Sopenharmony_ci<li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Hide the subprocess console window that would
5601cb0ef41Sopenharmony_cinormally be created on Windows systems. <strong>Default:</strong> <code>false</code>.</li>
5611cb0ef41Sopenharmony_ci</ul>
5621cb0ef41Sopenharmony_ci</li>
5631cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> called with the output when process terminates.
5641cb0ef41Sopenharmony_ci<ul>
5651cb0ef41Sopenharmony_ci<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
5661cb0ef41Sopenharmony_ci<li><code>stdout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
5671cb0ef41Sopenharmony_ci<li><code>stderr</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
5681cb0ef41Sopenharmony_ci</ul>
5691cb0ef41Sopenharmony_ci</li>
5701cb0ef41Sopenharmony_ci<li>Returns: <a href="child_process.html#class-childprocess" class="type">&#x3C;ChildProcess></a></li>
5711cb0ef41Sopenharmony_ci</ul>
5721cb0ef41Sopenharmony_ci<p>Spawns a shell then executes the <code>command</code> within that shell, buffering any
5731cb0ef41Sopenharmony_cigenerated output. The <code>command</code> string passed to the exec function is processed
5741cb0ef41Sopenharmony_cidirectly by the shell and special characters (vary based on
5751cb0ef41Sopenharmony_ci<a href="https://en.wikipedia.org/wiki/List_of_command-line_interpreters">shell</a>)
5761cb0ef41Sopenharmony_cineed to be dealt with accordingly:</p>
5771cb0ef41Sopenharmony_ci<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>);
5781cb0ef41Sopenharmony_ci
5791cb0ef41Sopenharmony_ci<span class="hljs-title function_">exec</span>(<span class="hljs-string">'"/path/to/test file/test.sh" arg1 arg2'</span>);
5801cb0ef41Sopenharmony_ci<span class="hljs-comment">// Double quotes are used so that the space in the path is not interpreted as</span>
5811cb0ef41Sopenharmony_ci<span class="hljs-comment">// a delimiter of multiple arguments.</span>
5821cb0ef41Sopenharmony_ci
5831cb0ef41Sopenharmony_ci<span class="hljs-title function_">exec</span>(<span class="hljs-string">'echo "The \\$HOME variable is $HOME"'</span>);
5841cb0ef41Sopenharmony_ci<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>
5851cb0ef41Sopenharmony_ci<p><strong>Never pass unsanitized user input to this function. Any input containing shell
5861cb0ef41Sopenharmony_cimetacharacters may be used to trigger arbitrary command execution.</strong></p>
5871cb0ef41Sopenharmony_ci<p>If a <code>callback</code> function is provided, it is called with the arguments
5881cb0ef41Sopenharmony_ci<code>(error, stdout, stderr)</code>. On success, <code>error</code> will be <code>null</code>. On error,
5891cb0ef41Sopenharmony_ci<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
5901cb0ef41Sopenharmony_cithe exit code of the process. By convention, any exit code other than <code>0</code>
5911cb0ef41Sopenharmony_ciindicates an error. <code>error.signal</code> will be the signal that terminated the
5921cb0ef41Sopenharmony_ciprocess.</p>
5931cb0ef41Sopenharmony_ci<p>The <code>stdout</code> and <code>stderr</code> arguments passed to the callback will contain the
5941cb0ef41Sopenharmony_cistdout and stderr output of the child process. By default, Node.js will decode
5951cb0ef41Sopenharmony_cithe output as UTF-8 and pass strings to the callback. The <code>encoding</code> option
5961cb0ef41Sopenharmony_cican be used to specify the character encoding used to decode the stdout and
5971cb0ef41Sopenharmony_cistderr output. If <code>encoding</code> is <code>'buffer'</code>, or an unrecognized character
5981cb0ef41Sopenharmony_ciencoding, <code>Buffer</code> objects will be passed to the callback instead.</p>
5991cb0ef41Sopenharmony_ci<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>);
6001cb0ef41Sopenharmony_ci<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> {
6011cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (error) {
6021cb0ef41Sopenharmony_ci    <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>);
6031cb0ef41Sopenharmony_ci    <span class="hljs-keyword">return</span>;
6041cb0ef41Sopenharmony_ci  }
6051cb0ef41Sopenharmony_ci  <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>);
6061cb0ef41Sopenharmony_ci  <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>);
6071cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
6081cb0ef41Sopenharmony_ci<p>If <code>timeout</code> is greater than <code>0</code>, the parent will send the signal
6091cb0ef41Sopenharmony_ciidentified by the <code>killSignal</code> property (the default is <code>'SIGTERM'</code>) if the
6101cb0ef41Sopenharmony_cichild runs longer than <code>timeout</code> milliseconds.</p>
6111cb0ef41Sopenharmony_ci<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
6121cb0ef41Sopenharmony_cithe existing process and uses a shell to execute the command.</p>
6131cb0ef41Sopenharmony_ci<p>If this method is invoked as its <a href="util.html#utilpromisifyoriginal"><code>util.promisify()</code></a>ed version, it returns
6141cb0ef41Sopenharmony_cia <code>Promise</code> for an <code>Object</code> with <code>stdout</code> and <code>stderr</code> properties. The returned
6151cb0ef41Sopenharmony_ci<code>ChildProcess</code> instance is attached to the <code>Promise</code> as a <code>child</code> property. In
6161cb0ef41Sopenharmony_cicase of an error (including any error resulting in an exit code other than 0), a
6171cb0ef41Sopenharmony_cirejected promise is returned, with the same <code>error</code> object given in the
6181cb0ef41Sopenharmony_cicallback, but with two additional properties <code>stdout</code> and <code>stderr</code>.</p>
6191cb0ef41Sopenharmony_ci<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>);
6201cb0ef41Sopenharmony_ci<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>);
6211cb0ef41Sopenharmony_ci
6221cb0ef41Sopenharmony_ci<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">lsExample</span>(<span class="hljs-params"></span>) {
6231cb0ef41Sopenharmony_ci  <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>);
6241cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'stdout:'</span>, stdout);
6251cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'stderr:'</span>, stderr);
6261cb0ef41Sopenharmony_ci}
6271cb0ef41Sopenharmony_ci<span class="hljs-title function_">lsExample</span>();</code> <button class="copy-button">copy</button></pre>
6281cb0ef41Sopenharmony_ci<p>If the <code>signal</code> option is enabled, calling <code>.abort()</code> on the corresponding
6291cb0ef41Sopenharmony_ci<code>AbortController</code> is similar to calling <code>.kill()</code> on the child process except
6301cb0ef41Sopenharmony_cithe error passed to the callback will be an <code>AbortError</code>:</p>
6311cb0ef41Sopenharmony_ci<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>);
6321cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>();
6331cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { signal } = controller;
6341cb0ef41Sopenharmony_ci<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> {
6351cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(error); <span class="hljs-comment">// an AbortError</span>
6361cb0ef41Sopenharmony_ci});
6371cb0ef41Sopenharmony_cicontroller.<span class="hljs-title function_">abort</span>();</code> <button class="copy-button">copy</button></pre>
6381cb0ef41Sopenharmony_ci<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>
6391cb0ef41Sopenharmony_ci<div class="api_metadata">
6401cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
6411cb0ef41Sopenharmony_ci<table>
6421cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
6431cb0ef41Sopenharmony_ci<tr><td>v16.4.0, v14.18.0</td>
6441cb0ef41Sopenharmony_ci<td><p>The <code>cwd</code> option can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
6451cb0ef41Sopenharmony_ci<tr><td>v15.4.0, v14.17.0</td>
6461cb0ef41Sopenharmony_ci<td><p>AbortSignal support was added.</p></td></tr>
6471cb0ef41Sopenharmony_ci<tr><td>v8.8.0</td>
6481cb0ef41Sopenharmony_ci<td><p>The <code>windowsHide</code> option is supported now.</p></td></tr>
6491cb0ef41Sopenharmony_ci<tr><td>v0.1.91</td>
6501cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.91</span></p></td></tr>
6511cb0ef41Sopenharmony_ci</tbody></table>
6521cb0ef41Sopenharmony_ci</details>
6531cb0ef41Sopenharmony_ci</div>
6541cb0ef41Sopenharmony_ci<ul>
6551cb0ef41Sopenharmony_ci<li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The name or path of the executable file to run.</li>
6561cb0ef41Sopenharmony_ci<li><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> List of string arguments.</li>
6571cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
6581cb0ef41Sopenharmony_ci<ul>
6591cb0ef41Sopenharmony_ci<li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> Current working directory of the child process.</li>
6601cb0ef41Sopenharmony_ci<li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> Environment key-value pairs. <strong>Default:</strong> <code>process.env</code>.</li>
6611cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>'utf8'</code></li>
6621cb0ef41Sopenharmony_ci<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <strong>Default:</strong> <code>0</code></li>
6631cb0ef41Sopenharmony_ci<li><code>maxBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Largest amount of data in bytes allowed on stdout or
6641cb0ef41Sopenharmony_cistderr. If exceeded, the child process is terminated and any output is
6651cb0ef41Sopenharmony_citruncated. See caveat at <a href="#maxbuffer-and-unicode"><code>maxBuffer</code> and Unicode</a>.
6661cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>1024 * 1024</code>.</li>
6671cb0ef41Sopenharmony_ci<li><code>killSignal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>'SIGTERM'</code></li>
6681cb0ef41Sopenharmony_ci<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;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>
6691cb0ef41Sopenharmony_ci<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;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>
6701cb0ef41Sopenharmony_ci<li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Hide the subprocess console window that would
6711cb0ef41Sopenharmony_cinormally be created on Windows systems. <strong>Default:</strong> <code>false</code>.</li>
6721cb0ef41Sopenharmony_ci<li><code>windowsVerbatimArguments</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> No quoting or escaping of arguments is
6731cb0ef41Sopenharmony_cidone on Windows. Ignored on Unix. <strong>Default:</strong> <code>false</code>.</li>
6741cb0ef41Sopenharmony_ci<li><code>shell</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> If <code>true</code>, runs <code>command</code> inside of a shell. Uses
6751cb0ef41Sopenharmony_ci<code>'/bin/sh'</code> on Unix, and <code>process.env.ComSpec</code> on Windows. A different
6761cb0ef41Sopenharmony_cishell can be specified as a string. See <a href="#shell-requirements">Shell requirements</a> and
6771cb0ef41Sopenharmony_ci<a href="#default-windows-shell">Default Windows shell</a>. <strong>Default:</strong> <code>false</code> (no shell).</li>
6781cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type">&#x3C;AbortSignal></a> allows aborting the child process using an
6791cb0ef41Sopenharmony_ciAbortSignal.</li>
6801cb0ef41Sopenharmony_ci</ul>
6811cb0ef41Sopenharmony_ci</li>
6821cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Called with the output when process terminates.
6831cb0ef41Sopenharmony_ci<ul>
6841cb0ef41Sopenharmony_ci<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
6851cb0ef41Sopenharmony_ci<li><code>stdout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
6861cb0ef41Sopenharmony_ci<li><code>stderr</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
6871cb0ef41Sopenharmony_ci</ul>
6881cb0ef41Sopenharmony_ci</li>
6891cb0ef41Sopenharmony_ci<li>Returns: <a href="child_process.html#class-childprocess" class="type">&#x3C;ChildProcess></a></li>
6901cb0ef41Sopenharmony_ci</ul>
6911cb0ef41Sopenharmony_ci<p>The <code>child_process.execFile()</code> function is similar to <a href="#child_processexeccommand-options-callback"><code>child_process.exec()</code></a>
6921cb0ef41Sopenharmony_ciexcept that it does not spawn a shell by default. Rather, the specified
6931cb0ef41Sopenharmony_ciexecutable <code>file</code> is spawned directly as a new process making it slightly more
6941cb0ef41Sopenharmony_ciefficient than <a href="#child_processexeccommand-options-callback"><code>child_process.exec()</code></a>.</p>
6951cb0ef41Sopenharmony_ci<p>The same options as <a href="#child_processexeccommand-options-callback"><code>child_process.exec()</code></a> are supported. Since a shell is
6961cb0ef41Sopenharmony_cinot spawned, behaviors such as I/O redirection and file globbing are not
6971cb0ef41Sopenharmony_cisupported.</p>
6981cb0ef41Sopenharmony_ci<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>);
6991cb0ef41Sopenharmony_ci<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> {
7001cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (error) {
7011cb0ef41Sopenharmony_ci    <span class="hljs-keyword">throw</span> error;
7021cb0ef41Sopenharmony_ci  }
7031cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(stdout);
7041cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
7051cb0ef41Sopenharmony_ci<p>The <code>stdout</code> and <code>stderr</code> arguments passed to the callback will contain the
7061cb0ef41Sopenharmony_cistdout and stderr output of the child process. By default, Node.js will decode
7071cb0ef41Sopenharmony_cithe output as UTF-8 and pass strings to the callback. The <code>encoding</code> option
7081cb0ef41Sopenharmony_cican be used to specify the character encoding used to decode the stdout and
7091cb0ef41Sopenharmony_cistderr output. If <code>encoding</code> is <code>'buffer'</code>, or an unrecognized character
7101cb0ef41Sopenharmony_ciencoding, <code>Buffer</code> objects will be passed to the callback instead.</p>
7111cb0ef41Sopenharmony_ci<p>If this method is invoked as its <a href="util.html#utilpromisifyoriginal"><code>util.promisify()</code></a>ed version, it returns
7121cb0ef41Sopenharmony_cia <code>Promise</code> for an <code>Object</code> with <code>stdout</code> and <code>stderr</code> properties. The returned
7131cb0ef41Sopenharmony_ci<code>ChildProcess</code> instance is attached to the <code>Promise</code> as a <code>child</code> property. In
7141cb0ef41Sopenharmony_cicase of an error (including any error resulting in an exit code other than 0), a
7151cb0ef41Sopenharmony_cirejected promise is returned, with the same <code>error</code> object given in the
7161cb0ef41Sopenharmony_cicallback, but with two additional properties <code>stdout</code> and <code>stderr</code>.</p>
7171cb0ef41Sopenharmony_ci<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>);
7181cb0ef41Sopenharmony_ci<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>);
7191cb0ef41Sopenharmony_ci<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">getVersion</span>(<span class="hljs-params"></span>) {
7201cb0ef41Sopenharmony_ci  <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>]);
7211cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(stdout);
7221cb0ef41Sopenharmony_ci}
7231cb0ef41Sopenharmony_ci<span class="hljs-title function_">getVersion</span>();</code> <button class="copy-button">copy</button></pre>
7241cb0ef41Sopenharmony_ci<p><strong>If the <code>shell</code> option is enabled, do not pass unsanitized user input to this
7251cb0ef41Sopenharmony_cifunction. Any input containing shell metacharacters may be used to trigger
7261cb0ef41Sopenharmony_ciarbitrary command execution.</strong></p>
7271cb0ef41Sopenharmony_ci<p>If the <code>signal</code> option is enabled, calling <code>.abort()</code> on the corresponding
7281cb0ef41Sopenharmony_ci<code>AbortController</code> is similar to calling <code>.kill()</code> on the child process except
7291cb0ef41Sopenharmony_cithe error passed to the callback will be an <code>AbortError</code>:</p>
7301cb0ef41Sopenharmony_ci<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>);
7311cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>();
7321cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { signal } = controller;
7331cb0ef41Sopenharmony_ci<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> {
7341cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(error); <span class="hljs-comment">// an AbortError</span>
7351cb0ef41Sopenharmony_ci});
7361cb0ef41Sopenharmony_cicontroller.<span class="hljs-title function_">abort</span>();</code> <button class="copy-button">copy</button></pre>
7371cb0ef41Sopenharmony_ci<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>
7381cb0ef41Sopenharmony_ci<div class="api_metadata">
7391cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
7401cb0ef41Sopenharmony_ci<table>
7411cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
7421cb0ef41Sopenharmony_ci<tr><td>v17.4.0, v16.14.0</td>
7431cb0ef41Sopenharmony_ci<td><p>The <code>modulePath</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
7441cb0ef41Sopenharmony_ci<tr><td>v16.4.0, v14.18.0</td>
7451cb0ef41Sopenharmony_ci<td><p>The <code>cwd</code> option can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
7461cb0ef41Sopenharmony_ci<tr><td>v15.13.0, v14.18.0</td>
7471cb0ef41Sopenharmony_ci<td><p>timeout was added.</p></td></tr>
7481cb0ef41Sopenharmony_ci<tr><td>v15.11.0, v14.18.0</td>
7491cb0ef41Sopenharmony_ci<td><p>killSignal for AbortSignal was added.</p></td></tr>
7501cb0ef41Sopenharmony_ci<tr><td>v15.6.0, v14.17.0</td>
7511cb0ef41Sopenharmony_ci<td><p>AbortSignal support was added.</p></td></tr>
7521cb0ef41Sopenharmony_ci<tr><td>v13.2.0, v12.16.0</td>
7531cb0ef41Sopenharmony_ci<td><p>The <code>serialization</code> option is supported now.</p></td></tr>
7541cb0ef41Sopenharmony_ci<tr><td>v8.0.0</td>
7551cb0ef41Sopenharmony_ci<td><p>The <code>stdio</code> option can now be a string.</p></td></tr>
7561cb0ef41Sopenharmony_ci<tr><td>v6.4.0</td>
7571cb0ef41Sopenharmony_ci<td><p>The <code>stdio</code> option is supported now.</p></td></tr>
7581cb0ef41Sopenharmony_ci<tr><td>v0.5.0</td>
7591cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.0</span></p></td></tr>
7601cb0ef41Sopenharmony_ci</tbody></table>
7611cb0ef41Sopenharmony_ci</details>
7621cb0ef41Sopenharmony_ci</div>
7631cb0ef41Sopenharmony_ci<ul>
7641cb0ef41Sopenharmony_ci<li><code>modulePath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> The module to run in the child.</li>
7651cb0ef41Sopenharmony_ci<li><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> List of string arguments.</li>
7661cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
7671cb0ef41Sopenharmony_ci<ul>
7681cb0ef41Sopenharmony_ci<li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> Current working directory of the child process.</li>
7691cb0ef41Sopenharmony_ci<li><code>detached</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Prepare child to run independently of its parent
7701cb0ef41Sopenharmony_ciprocess. Specific behavior depends on the platform, see
7711cb0ef41Sopenharmony_ci<a href="#optionsdetached"><code>options.detached</code></a>).</li>
7721cb0ef41Sopenharmony_ci<li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> Environment key-value pairs. <strong>Default:</strong> <code>process.env</code>.</li>
7731cb0ef41Sopenharmony_ci<li><code>execPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Executable used to create the child process.</li>
7741cb0ef41Sopenharmony_ci<li><code>execArgv</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> List of string arguments passed to the executable.
7751cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>process.execArgv</code>.</li>
7761cb0ef41Sopenharmony_ci<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;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>
7771cb0ef41Sopenharmony_ci<li><code>serialization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Specify the kind of serialization used for sending
7781cb0ef41Sopenharmony_cimessages between processes. Possible values are <code>'json'</code> and <code>'advanced'</code>.
7791cb0ef41Sopenharmony_ciSee <a href="#advanced-serialization">Advanced serialization</a> for more details. <strong>Default:</strong> <code>'json'</code>.</li>
7801cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type">&#x3C;AbortSignal></a> Allows closing the child process using an
7811cb0ef41Sopenharmony_ciAbortSignal.</li>
7821cb0ef41Sopenharmony_ci<li><code>killSignal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The signal value to be used when the spawned
7831cb0ef41Sopenharmony_ciprocess will be killed by timeout or abort signal. <strong>Default:</strong> <code>'SIGTERM'</code>.</li>
7841cb0ef41Sopenharmony_ci<li><code>silent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, stdin, stdout, and stderr of the child will be
7851cb0ef41Sopenharmony_cipiped to the parent, otherwise they will be inherited from the parent, see
7861cb0ef41Sopenharmony_cithe <code>'pipe'</code> and <code>'inherit'</code> options for <a href="#child_processspawncommand-args-options"><code>child_process.spawn()</code></a>'s
7871cb0ef41Sopenharmony_ci<a href="#optionsstdio"><code>stdio</code></a> for more details. <strong>Default:</strong> <code>false</code>.</li>
7881cb0ef41Sopenharmony_ci<li><code>stdio</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&#x3C;Array></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> See <a href="#child_processspawncommand-args-options"><code>child_process.spawn()</code></a>'s <a href="#optionsstdio"><code>stdio</code></a>.
7891cb0ef41Sopenharmony_ciWhen this option is provided, it overrides <code>silent</code>. If the array variant
7901cb0ef41Sopenharmony_ciis used, it must contain exactly one item with value <code>'ipc'</code> or an error
7911cb0ef41Sopenharmony_ciwill be thrown. For instance <code>[0, 1, 2, 'ipc']</code>.</li>
7921cb0ef41Sopenharmony_ci<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;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>
7931cb0ef41Sopenharmony_ci<li><code>windowsVerbatimArguments</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> No quoting or escaping of arguments is
7941cb0ef41Sopenharmony_cidone on Windows. Ignored on Unix. <strong>Default:</strong> <code>false</code>.</li>
7951cb0ef41Sopenharmony_ci<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> In milliseconds the maximum amount of time the process
7961cb0ef41Sopenharmony_ciis allowed to run. <strong>Default:</strong> <code>undefined</code>.</li>
7971cb0ef41Sopenharmony_ci</ul>
7981cb0ef41Sopenharmony_ci</li>
7991cb0ef41Sopenharmony_ci<li>Returns: <a href="child_process.html#class-childprocess" class="type">&#x3C;ChildProcess></a></li>
8001cb0ef41Sopenharmony_ci</ul>
8011cb0ef41Sopenharmony_ci<p>The <code>child_process.fork()</code> method is a special case of
8021cb0ef41Sopenharmony_ci<a href="#child_processspawncommand-args-options"><code>child_process.spawn()</code></a> used specifically to spawn new Node.js processes.
8031cb0ef41Sopenharmony_ciLike <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
8041cb0ef41Sopenharmony_cireturned <a href="#class-childprocess"><code>ChildProcess</code></a> will have an additional communication channel
8051cb0ef41Sopenharmony_cibuilt-in that allows messages to be passed back and forth between the parent and
8061cb0ef41Sopenharmony_cichild. See <a href="#subprocesssendmessage-sendhandle-options-callback"><code>subprocess.send()</code></a> for details.</p>
8071cb0ef41Sopenharmony_ci<p>Keep in mind that spawned Node.js child processes are
8081cb0ef41Sopenharmony_ciindependent of the parent with exception of the IPC communication channel
8091cb0ef41Sopenharmony_cithat is established between the two. Each process has its own memory, with
8101cb0ef41Sopenharmony_citheir own V8 instances. Because of the additional resource allocations
8111cb0ef41Sopenharmony_cirequired, spawning a large number of child Node.js processes is not
8121cb0ef41Sopenharmony_cirecommended.</p>
8131cb0ef41Sopenharmony_ci<p>By default, <code>child_process.fork()</code> will spawn new Node.js instances using the
8141cb0ef41Sopenharmony_ci<a href="process.html#processexecpath"><code>process.execPath</code></a> of the parent process. The <code>execPath</code> property in the
8151cb0ef41Sopenharmony_ci<code>options</code> object allows for an alternative execution path to be used.</p>
8161cb0ef41Sopenharmony_ci<p>Node.js processes launched with a custom <code>execPath</code> will communicate with the
8171cb0ef41Sopenharmony_ciparent process using the file descriptor (fd) identified using the
8181cb0ef41Sopenharmony_cienvironment variable <code>NODE_CHANNEL_FD</code> on the child process.</p>
8191cb0ef41Sopenharmony_ci<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
8201cb0ef41Sopenharmony_cicurrent process.</p>
8211cb0ef41Sopenharmony_ci<p>The <code>shell</code> option available in <a href="#child_processspawncommand-args-options"><code>child_process.spawn()</code></a> is not supported by
8221cb0ef41Sopenharmony_ci<code>child_process.fork()</code> and will be ignored if set.</p>
8231cb0ef41Sopenharmony_ci<p>If the <code>signal</code> option is enabled, calling <code>.abort()</code> on the corresponding
8241cb0ef41Sopenharmony_ci<code>AbortController</code> is similar to calling <code>.kill()</code> on the child process except
8251cb0ef41Sopenharmony_cithe error passed to the callback will be an <code>AbortError</code>:</p>
8261cb0ef41Sopenharmony_ci<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>) {
8271cb0ef41Sopenharmony_ci  <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
8281cb0ef41Sopenharmony_ci    <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>);
8291cb0ef41Sopenharmony_ci  }, <span class="hljs-number">1_000</span>);
8301cb0ef41Sopenharmony_ci} <span class="hljs-keyword">else</span> {
8311cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> { fork } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:child_process'</span>);
8321cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>();
8331cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> { signal } = controller;
8341cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> child = <span class="hljs-title function_">fork</span>(__filename, [<span class="hljs-string">'child'</span>], { signal });
8351cb0ef41Sopenharmony_ci  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> {
8361cb0ef41Sopenharmony_ci    <span class="hljs-comment">// This will be called with err being an AbortError if the controller aborts</span>
8371cb0ef41Sopenharmony_ci  });
8381cb0ef41Sopenharmony_ci  controller.<span class="hljs-title function_">abort</span>(); <span class="hljs-comment">// Stops the child process</span>
8391cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
8401cb0ef41Sopenharmony_ci<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>
8411cb0ef41Sopenharmony_ci<div class="api_metadata">
8421cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
8431cb0ef41Sopenharmony_ci<table>
8441cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
8451cb0ef41Sopenharmony_ci<tr><td>v16.4.0, v14.18.0</td>
8461cb0ef41Sopenharmony_ci<td><p>The <code>cwd</code> option can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
8471cb0ef41Sopenharmony_ci<tr><td>v15.13.0, v14.18.0</td>
8481cb0ef41Sopenharmony_ci<td><p>timeout was added.</p></td></tr>
8491cb0ef41Sopenharmony_ci<tr><td>v15.11.0, v14.18.0</td>
8501cb0ef41Sopenharmony_ci<td><p>killSignal for AbortSignal was added.</p></td></tr>
8511cb0ef41Sopenharmony_ci<tr><td>v15.5.0, v14.17.0</td>
8521cb0ef41Sopenharmony_ci<td><p>AbortSignal support was added.</p></td></tr>
8531cb0ef41Sopenharmony_ci<tr><td>v13.2.0, v12.16.0</td>
8541cb0ef41Sopenharmony_ci<td><p>The <code>serialization</code> option is supported now.</p></td></tr>
8551cb0ef41Sopenharmony_ci<tr><td>v8.8.0</td>
8561cb0ef41Sopenharmony_ci<td><p>The <code>windowsHide</code> option is supported now.</p></td></tr>
8571cb0ef41Sopenharmony_ci<tr><td>v6.4.0</td>
8581cb0ef41Sopenharmony_ci<td><p>The <code>argv0</code> option is supported now.</p></td></tr>
8591cb0ef41Sopenharmony_ci<tr><td>v5.7.0</td>
8601cb0ef41Sopenharmony_ci<td><p>The <code>shell</code> option is supported now.</p></td></tr>
8611cb0ef41Sopenharmony_ci<tr><td>v0.1.90</td>
8621cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.90</span></p></td></tr>
8631cb0ef41Sopenharmony_ci</tbody></table>
8641cb0ef41Sopenharmony_ci</details>
8651cb0ef41Sopenharmony_ci</div>
8661cb0ef41Sopenharmony_ci<ul>
8671cb0ef41Sopenharmony_ci<li><code>command</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The command to run.</li>
8681cb0ef41Sopenharmony_ci<li><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> List of string arguments.</li>
8691cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
8701cb0ef41Sopenharmony_ci<ul>
8711cb0ef41Sopenharmony_ci<li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> Current working directory of the child process.</li>
8721cb0ef41Sopenharmony_ci<li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> Environment key-value pairs. <strong>Default:</strong> <code>process.env</code>.</li>
8731cb0ef41Sopenharmony_ci<li><code>argv0</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Explicitly set the value of <code>argv[0]</code> sent to the child
8741cb0ef41Sopenharmony_ciprocess. This will be set to <code>command</code> if not specified.</li>
8751cb0ef41Sopenharmony_ci<li><code>stdio</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&#x3C;Array></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Child's stdio configuration (see
8761cb0ef41Sopenharmony_ci<a href="#optionsstdio"><code>options.stdio</code></a>).</li>
8771cb0ef41Sopenharmony_ci<li><code>detached</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Prepare child to run independently of its parent
8781cb0ef41Sopenharmony_ciprocess. Specific behavior depends on the platform, see
8791cb0ef41Sopenharmony_ci<a href="#optionsdetached"><code>options.detached</code></a>).</li>
8801cb0ef41Sopenharmony_ci<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;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>
8811cb0ef41Sopenharmony_ci<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;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>
8821cb0ef41Sopenharmony_ci<li><code>serialization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Specify the kind of serialization used for sending
8831cb0ef41Sopenharmony_cimessages between processes. Possible values are <code>'json'</code> and <code>'advanced'</code>.
8841cb0ef41Sopenharmony_ciSee <a href="#advanced-serialization">Advanced serialization</a> for more details. <strong>Default:</strong> <code>'json'</code>.</li>
8851cb0ef41Sopenharmony_ci<li><code>shell</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> If <code>true</code>, runs <code>command</code> inside of a shell. Uses
8861cb0ef41Sopenharmony_ci<code>'/bin/sh'</code> on Unix, and <code>process.env.ComSpec</code> on Windows. A different
8871cb0ef41Sopenharmony_cishell can be specified as a string. See <a href="#shell-requirements">Shell requirements</a> and
8881cb0ef41Sopenharmony_ci<a href="#default-windows-shell">Default Windows shell</a>. <strong>Default:</strong> <code>false</code> (no shell).</li>
8891cb0ef41Sopenharmony_ci<li><code>windowsVerbatimArguments</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> No quoting or escaping of arguments is
8901cb0ef41Sopenharmony_cidone on Windows. Ignored on Unix. This is set to <code>true</code> automatically
8911cb0ef41Sopenharmony_ciwhen <code>shell</code> is specified and is CMD. <strong>Default:</strong> <code>false</code>.</li>
8921cb0ef41Sopenharmony_ci<li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Hide the subprocess console window that would
8931cb0ef41Sopenharmony_cinormally be created on Windows systems. <strong>Default:</strong> <code>false</code>.</li>
8941cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type">&#x3C;AbortSignal></a> allows aborting the child process using an
8951cb0ef41Sopenharmony_ciAbortSignal.</li>
8961cb0ef41Sopenharmony_ci<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> In milliseconds the maximum amount of time the process
8971cb0ef41Sopenharmony_ciis allowed to run. <strong>Default:</strong> <code>undefined</code>.</li>
8981cb0ef41Sopenharmony_ci<li><code>killSignal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The signal value to be used when the spawned
8991cb0ef41Sopenharmony_ciprocess will be killed by timeout or abort signal. <strong>Default:</strong> <code>'SIGTERM'</code>.</li>
9001cb0ef41Sopenharmony_ci</ul>
9011cb0ef41Sopenharmony_ci</li>
9021cb0ef41Sopenharmony_ci<li>Returns: <a href="child_process.html#class-childprocess" class="type">&#x3C;ChildProcess></a></li>
9031cb0ef41Sopenharmony_ci</ul>
9041cb0ef41Sopenharmony_ci<p>The <code>child_process.spawn()</code> method spawns a new process using the given
9051cb0ef41Sopenharmony_ci<code>command</code>, with command-line arguments in <code>args</code>. If omitted, <code>args</code> defaults
9061cb0ef41Sopenharmony_cito an empty array.</p>
9071cb0ef41Sopenharmony_ci<p><strong>If the <code>shell</code> option is enabled, do not pass unsanitized user input to this
9081cb0ef41Sopenharmony_cifunction. Any input containing shell metacharacters may be used to trigger
9091cb0ef41Sopenharmony_ciarbitrary command execution.</strong></p>
9101cb0ef41Sopenharmony_ci<p>A third argument may be used to specify additional options, with these defaults:</p>
9111cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> defaults = {
9121cb0ef41Sopenharmony_ci  <span class="hljs-attr">cwd</span>: <span class="hljs-literal">undefined</span>,
9131cb0ef41Sopenharmony_ci  <span class="hljs-attr">env</span>: process.<span class="hljs-property">env</span>,
9141cb0ef41Sopenharmony_ci};</code> <button class="copy-button">copy</button></pre>
9151cb0ef41Sopenharmony_ci<p>Use <code>cwd</code> to specify the working directory from which the process is spawned.
9161cb0ef41Sopenharmony_ciIf not given, the default is to inherit the current working directory. If given,
9171cb0ef41Sopenharmony_cibut the path does not exist, the child process emits an <code>ENOENT</code> error
9181cb0ef41Sopenharmony_ciand exits immediately. <code>ENOENT</code> is also emitted when the command
9191cb0ef41Sopenharmony_cidoes not exist.</p>
9201cb0ef41Sopenharmony_ci<p>Use <code>env</code> to specify environment variables that will be visible to the new
9211cb0ef41Sopenharmony_ciprocess, the default is <a href="process.html#processenv"><code>process.env</code></a>.</p>
9221cb0ef41Sopenharmony_ci<p><code>undefined</code> values in <code>env</code> will be ignored.</p>
9231cb0ef41Sopenharmony_ci<p>Example of running <code>ls -lh /usr</code>, capturing <code>stdout</code>, <code>stderr</code>, and the
9241cb0ef41Sopenharmony_ciexit code:</p>
9251cb0ef41Sopenharmony_ci<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>);
9261cb0ef41Sopenharmony_ci<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>]);
9271cb0ef41Sopenharmony_ci
9281cb0ef41Sopenharmony_cils.<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> {
9291cb0ef41Sopenharmony_ci  <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>);
9301cb0ef41Sopenharmony_ci});
9311cb0ef41Sopenharmony_ci
9321cb0ef41Sopenharmony_cils.<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> {
9331cb0ef41Sopenharmony_ci  <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>);
9341cb0ef41Sopenharmony_ci});
9351cb0ef41Sopenharmony_ci
9361cb0ef41Sopenharmony_cils.<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-function">(<span class="hljs-params">code</span>) =></span> {
9371cb0ef41Sopenharmony_ci  <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>);
9381cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
9391cb0ef41Sopenharmony_ci<p>Example: A very elaborate way to run <code>ps ax | grep ssh</code></p>
9401cb0ef41Sopenharmony_ci<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>);
9411cb0ef41Sopenharmony_ci<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>]);
9421cb0ef41Sopenharmony_ci<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>]);
9431cb0ef41Sopenharmony_ci
9441cb0ef41Sopenharmony_cips.<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> {
9451cb0ef41Sopenharmony_ci  grep.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">write</span>(data);
9461cb0ef41Sopenharmony_ci});
9471cb0ef41Sopenharmony_ci
9481cb0ef41Sopenharmony_cips.<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> {
9491cb0ef41Sopenharmony_ci  <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>);
9501cb0ef41Sopenharmony_ci});
9511cb0ef41Sopenharmony_ci
9521cb0ef41Sopenharmony_cips.<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-function">(<span class="hljs-params">code</span>) =></span> {
9531cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (code !== <span class="hljs-number">0</span>) {
9541cb0ef41Sopenharmony_ci    <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>);
9551cb0ef41Sopenharmony_ci  }
9561cb0ef41Sopenharmony_ci  grep.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">end</span>();
9571cb0ef41Sopenharmony_ci});
9581cb0ef41Sopenharmony_ci
9591cb0ef41Sopenharmony_cigrep.<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> {
9601cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(data.<span class="hljs-title function_">toString</span>());
9611cb0ef41Sopenharmony_ci});
9621cb0ef41Sopenharmony_ci
9631cb0ef41Sopenharmony_cigrep.<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> {
9641cb0ef41Sopenharmony_ci  <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>);
9651cb0ef41Sopenharmony_ci});
9661cb0ef41Sopenharmony_ci
9671cb0ef41Sopenharmony_cigrep.<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-function">(<span class="hljs-params">code</span>) =></span> {
9681cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (code !== <span class="hljs-number">0</span>) {
9691cb0ef41Sopenharmony_ci    <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>);
9701cb0ef41Sopenharmony_ci  }
9711cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
9721cb0ef41Sopenharmony_ci<p>Example of checking for failed <code>spawn</code>:</p>
9731cb0ef41Sopenharmony_ci<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>);
9741cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span>(<span class="hljs-string">'bad_command'</span>);
9751cb0ef41Sopenharmony_ci
9761cb0ef41Sopenharmony_cisubprocess.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
9771cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'Failed to start subprocess.'</span>);
9781cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
9791cb0ef41Sopenharmony_ci<p>Certain platforms (macOS, Linux) will use the value of <code>argv[0]</code> for the process
9801cb0ef41Sopenharmony_cititle while others (Windows, SunOS) will use <code>command</code>.</p>
9811cb0ef41Sopenharmony_ci<p>Node.js overwrites <code>argv[0]</code> with <code>process.execPath</code> on startup, so
9821cb0ef41Sopenharmony_ci<code>process.argv[0]</code> in a Node.js child process will not match the <code>argv0</code>
9831cb0ef41Sopenharmony_ciparameter passed to <code>spawn</code> from the parent. Retrieve it with the
9841cb0ef41Sopenharmony_ci<code>process.argv0</code> property instead.</p>
9851cb0ef41Sopenharmony_ci<p>If the <code>signal</code> option is enabled, calling <code>.abort()</code> on the corresponding
9861cb0ef41Sopenharmony_ci<code>AbortController</code> is similar to calling <code>.kill()</code> on the child process except
9871cb0ef41Sopenharmony_cithe error passed to the callback will be an <code>AbortError</code>:</p>
9881cb0ef41Sopenharmony_ci<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>);
9891cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>();
9901cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { signal } = controller;
9911cb0ef41Sopenharmony_ci<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 });
9921cb0ef41Sopenharmony_cigrep.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
9931cb0ef41Sopenharmony_ci  <span class="hljs-comment">// This will be called with err being an AbortError if the controller aborts</span>
9941cb0ef41Sopenharmony_ci});
9951cb0ef41Sopenharmony_cicontroller.<span class="hljs-title function_">abort</span>(); <span class="hljs-comment">// Stops the child process</span></code> <button class="copy-button">copy</button></pre>
9961cb0ef41Sopenharmony_ci<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>
9971cb0ef41Sopenharmony_ci<div class="api_metadata">
9981cb0ef41Sopenharmony_ci<span>Added in: v0.7.10</span>
9991cb0ef41Sopenharmony_ci</div>
10001cb0ef41Sopenharmony_ci<p>On Windows, setting <code>options.detached</code> to <code>true</code> makes it possible for the
10011cb0ef41Sopenharmony_cichild process to continue running after the parent exits. The child will have
10021cb0ef41Sopenharmony_ciits own console window. Once enabled for a child process, it cannot be
10031cb0ef41Sopenharmony_cidisabled.</p>
10041cb0ef41Sopenharmony_ci<p>On non-Windows platforms, if <code>options.detached</code> is set to <code>true</code>, the child
10051cb0ef41Sopenharmony_ciprocess will be made the leader of a new process group and session. Child
10061cb0ef41Sopenharmony_ciprocesses may continue running after the parent exits regardless of whether
10071cb0ef41Sopenharmony_cithey 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>
10081cb0ef41Sopenharmony_ci<p>By default, the parent will wait for the detached child to exit. To prevent the
10091cb0ef41Sopenharmony_ciparent from waiting for a given <code>subprocess</code> to exit, use the
10101cb0ef41Sopenharmony_ci<code>subprocess.unref()</code> method. Doing so will cause the parent's event loop to not
10111cb0ef41Sopenharmony_ciinclude the child in its reference count, allowing the parent to exit
10121cb0ef41Sopenharmony_ciindependently of the child, unless there is an established IPC channel between
10131cb0ef41Sopenharmony_cithe child and the parent.</p>
10141cb0ef41Sopenharmony_ci<p>When using the <code>detached</code> option to start a long-running process, the process
10151cb0ef41Sopenharmony_ciwill not stay running in the background after the parent exits unless it is
10161cb0ef41Sopenharmony_ciprovided with a <code>stdio</code> configuration that is not connected to the parent.
10171cb0ef41Sopenharmony_ciIf the parent's <code>stdio</code> is inherited, the child will remain attached to the
10181cb0ef41Sopenharmony_cicontrolling terminal.</p>
10191cb0ef41Sopenharmony_ci<p>Example of a long-running process, by detaching and also ignoring its parent
10201cb0ef41Sopenharmony_ci<code>stdio</code> file descriptors, in order to ignore the parent's termination:</p>
10211cb0ef41Sopenharmony_ci<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>);
10221cb0ef41Sopenharmony_ci
10231cb0ef41Sopenharmony_ci<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>], {
10241cb0ef41Sopenharmony_ci  <span class="hljs-attr">detached</span>: <span class="hljs-literal">true</span>,
10251cb0ef41Sopenharmony_ci  <span class="hljs-attr">stdio</span>: <span class="hljs-string">'ignore'</span>,
10261cb0ef41Sopenharmony_ci});
10271cb0ef41Sopenharmony_ci
10281cb0ef41Sopenharmony_cisubprocess.<span class="hljs-title function_">unref</span>();</code> <button class="copy-button">copy</button></pre>
10291cb0ef41Sopenharmony_ci<p>Alternatively one can redirect the child process' output into files:</p>
10301cb0ef41Sopenharmony_ci<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>);
10311cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:child_process'</span>);
10321cb0ef41Sopenharmony_ci<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>);
10331cb0ef41Sopenharmony_ci<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>);
10341cb0ef41Sopenharmony_ci
10351cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span>(<span class="hljs-string">'prg'</span>, [], {
10361cb0ef41Sopenharmony_ci  <span class="hljs-attr">detached</span>: <span class="hljs-literal">true</span>,
10371cb0ef41Sopenharmony_ci  <span class="hljs-attr">stdio</span>: [ <span class="hljs-string">'ignore'</span>, out, err ],
10381cb0ef41Sopenharmony_ci});
10391cb0ef41Sopenharmony_ci
10401cb0ef41Sopenharmony_cisubprocess.<span class="hljs-title function_">unref</span>();</code> <button class="copy-button">copy</button></pre>
10411cb0ef41Sopenharmony_ci<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>
10421cb0ef41Sopenharmony_ci<div class="api_metadata">
10431cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
10441cb0ef41Sopenharmony_ci<table>
10451cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
10461cb0ef41Sopenharmony_ci<tr><td>v15.6.0, v14.18.0</td>
10471cb0ef41Sopenharmony_ci<td><p>Added the <code>overlapped</code> stdio flag.</p></td></tr>
10481cb0ef41Sopenharmony_ci<tr><td>v3.3.1</td>
10491cb0ef41Sopenharmony_ci<td><p>The value <code>0</code> is now accepted as a file descriptor.</p></td></tr>
10501cb0ef41Sopenharmony_ci<tr><td>v0.7.10</td>
10511cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.7.10</span></p></td></tr>
10521cb0ef41Sopenharmony_ci</tbody></table>
10531cb0ef41Sopenharmony_ci</details>
10541cb0ef41Sopenharmony_ci</div>
10551cb0ef41Sopenharmony_ci<p>The <code>options.stdio</code> option is used to configure the pipes that are established
10561cb0ef41Sopenharmony_cibetween the parent and child process. By default, the child's stdin, stdout,
10571cb0ef41Sopenharmony_ciand stderr are redirected to corresponding <a href="#subprocessstdin"><code>subprocess.stdin</code></a>,
10581cb0ef41Sopenharmony_ci<a href="#subprocessstdout"><code>subprocess.stdout</code></a>, and <a href="#subprocessstderr"><code>subprocess.stderr</code></a> streams on the
10591cb0ef41Sopenharmony_ci<a href="#class-childprocess"><code>ChildProcess</code></a> object. This is equivalent to setting the <code>options.stdio</code>
10601cb0ef41Sopenharmony_ciequal to <code>['pipe', 'pipe', 'pipe']</code>.</p>
10611cb0ef41Sopenharmony_ci<p>For convenience, <code>options.stdio</code> may be one of the following strings:</p>
10621cb0ef41Sopenharmony_ci<ul>
10631cb0ef41Sopenharmony_ci<li><code>'pipe'</code>: equivalent to <code>['pipe', 'pipe', 'pipe']</code> (the default)</li>
10641cb0ef41Sopenharmony_ci<li><code>'overlapped'</code>: equivalent to <code>['overlapped', 'overlapped', 'overlapped']</code></li>
10651cb0ef41Sopenharmony_ci<li><code>'ignore'</code>: equivalent to <code>['ignore', 'ignore', 'ignore']</code></li>
10661cb0ef41Sopenharmony_ci<li><code>'inherit'</code>: equivalent to <code>['inherit', 'inherit', 'inherit']</code> or <code>[0, 1, 2]</code></li>
10671cb0ef41Sopenharmony_ci</ul>
10681cb0ef41Sopenharmony_ci<p>Otherwise, the value of <code>options.stdio</code> is an array where each index corresponds
10691cb0ef41Sopenharmony_cito an fd in the child. The fds 0, 1, and 2 correspond to stdin, stdout,
10701cb0ef41Sopenharmony_ciand stderr, respectively. Additional fds can be specified to create additional
10711cb0ef41Sopenharmony_cipipes between the parent and child. The value is one of the following:</p>
10721cb0ef41Sopenharmony_ci<ol>
10731cb0ef41Sopenharmony_ci<li>
10741cb0ef41Sopenharmony_ci<p><code>'pipe'</code>: Create a pipe between the child process and the parent process.
10751cb0ef41Sopenharmony_ciThe parent end of the pipe is exposed to the parent as a property on the
10761cb0ef41Sopenharmony_ci<code>child_process</code> object as <a href="#subprocessstdio"><code>subprocess.stdio[fd]</code></a>. Pipes
10771cb0ef41Sopenharmony_cicreated for fds 0, 1, and 2 are also available as <a href="#subprocessstdin"><code>subprocess.stdin</code></a>,
10781cb0ef41Sopenharmony_ci<a href="#subprocessstdout"><code>subprocess.stdout</code></a> and <a href="#subprocessstderr"><code>subprocess.stderr</code></a>, respectively.
10791cb0ef41Sopenharmony_ciThese are not actual Unix pipes and therefore the child process
10801cb0ef41Sopenharmony_cican not use them by their descriptor files,
10811cb0ef41Sopenharmony_cie.g. <code>/dev/fd/2</code> or <code>/dev/stdout</code>.</p>
10821cb0ef41Sopenharmony_ci</li>
10831cb0ef41Sopenharmony_ci<li>
10841cb0ef41Sopenharmony_ci<p><code>'overlapped'</code>: Same as <code>'pipe'</code> except that the <code>FILE_FLAG_OVERLAPPED</code> flag
10851cb0ef41Sopenharmony_ciis set on the handle. This is necessary for overlapped I/O on the child
10861cb0ef41Sopenharmony_ciprocess's stdio handles. See the
10871cb0ef41Sopenharmony_ci<a href="https://docs.microsoft.com/en-us/windows/win32/fileio/synchronous-and-asynchronous-i-o">docs</a>
10881cb0ef41Sopenharmony_cifor more details. This is exactly the same as <code>'pipe'</code> on non-Windows
10891cb0ef41Sopenharmony_cisystems.</p>
10901cb0ef41Sopenharmony_ci</li>
10911cb0ef41Sopenharmony_ci<li>
10921cb0ef41Sopenharmony_ci<p><code>'ipc'</code>: Create an IPC channel for passing messages/file descriptors
10931cb0ef41Sopenharmony_cibetween parent and child. A <a href="#class-childprocess"><code>ChildProcess</code></a> may have at most one IPC
10941cb0ef41Sopenharmony_cistdio file descriptor. Setting this option enables the
10951cb0ef41Sopenharmony_ci<a href="#subprocesssendmessage-sendhandle-options-callback"><code>subprocess.send()</code></a> method. If the child is a Node.js process, the
10961cb0ef41Sopenharmony_cipresence of an IPC channel will enable <a href="process.html#processsendmessage-sendhandle-options-callback"><code>process.send()</code></a> and
10971cb0ef41Sopenharmony_ci<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
10981cb0ef41Sopenharmony_ci<a href="process.html#event-message"><code>'message'</code></a> events within the child.</p>
10991cb0ef41Sopenharmony_ci<p>Accessing the IPC channel fd in any way other than <a href="process.html#processsendmessage-sendhandle-options-callback"><code>process.send()</code></a>
11001cb0ef41Sopenharmony_cior using the IPC channel with a child process that is not a Node.js instance
11011cb0ef41Sopenharmony_ciis not supported.</p>
11021cb0ef41Sopenharmony_ci</li>
11031cb0ef41Sopenharmony_ci<li>
11041cb0ef41Sopenharmony_ci<p><code>'ignore'</code>: Instructs Node.js to ignore the fd in the child. While Node.js
11051cb0ef41Sopenharmony_ciwill always open fds 0, 1, and 2 for the processes it spawns, setting the fd
11061cb0ef41Sopenharmony_cito <code>'ignore'</code> will cause Node.js to open <code>/dev/null</code> and attach it to the
11071cb0ef41Sopenharmony_cichild's fd.</p>
11081cb0ef41Sopenharmony_ci</li>
11091cb0ef41Sopenharmony_ci<li>
11101cb0ef41Sopenharmony_ci<p><code>'inherit'</code>: Pass through the corresponding stdio stream to/from the
11111cb0ef41Sopenharmony_ciparent process. In the first three positions, this is equivalent to
11121cb0ef41Sopenharmony_ci<code>process.stdin</code>, <code>process.stdout</code>, and <code>process.stderr</code>, respectively. In
11131cb0ef41Sopenharmony_ciany other position, equivalent to <code>'ignore'</code>.</p>
11141cb0ef41Sopenharmony_ci</li>
11151cb0ef41Sopenharmony_ci<li>
11161cb0ef41Sopenharmony_ci<p><a href="stream.html#stream" class="type">&#x3C;Stream></a> object: Share a readable or writable stream that refers to a tty,
11171cb0ef41Sopenharmony_cifile, socket, or a pipe with the child process. The stream's underlying
11181cb0ef41Sopenharmony_cifile descriptor is duplicated in the child process to the fd that
11191cb0ef41Sopenharmony_cicorresponds to the index in the <code>stdio</code> array. The stream must have an
11201cb0ef41Sopenharmony_ciunderlying descriptor (file streams do not until the <code>'open'</code> event has
11211cb0ef41Sopenharmony_cioccurred).</p>
11221cb0ef41Sopenharmony_ci</li>
11231cb0ef41Sopenharmony_ci<li>
11241cb0ef41Sopenharmony_ci<p>Positive integer: The integer value is interpreted as a file descriptor
11251cb0ef41Sopenharmony_cithat is open in the parent process. It is shared with the child
11261cb0ef41Sopenharmony_ciprocess, similar to how <a href="stream.html#stream" class="type">&#x3C;Stream></a> objects can be shared. Passing sockets
11271cb0ef41Sopenharmony_ciis not supported on Windows.</p>
11281cb0ef41Sopenharmony_ci</li>
11291cb0ef41Sopenharmony_ci<li>
11301cb0ef41Sopenharmony_ci<p><code>null</code>, <code>undefined</code>: Use default value. For stdio fds 0, 1, and 2 (in other
11311cb0ef41Sopenharmony_ciwords, stdin, stdout, and stderr) a pipe is created. For fd 3 and up, the
11321cb0ef41Sopenharmony_cidefault is <code>'ignore'</code>.</p>
11331cb0ef41Sopenharmony_ci</li>
11341cb0ef41Sopenharmony_ci</ol>
11351cb0ef41Sopenharmony_ci<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>);
11361cb0ef41Sopenharmony_ci
11371cb0ef41Sopenharmony_ci<span class="hljs-comment">// Child will use parent's stdios.</span>
11381cb0ef41Sopenharmony_ci<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> });
11391cb0ef41Sopenharmony_ci
11401cb0ef41Sopenharmony_ci<span class="hljs-comment">// Spawn child sharing only stderr.</span>
11411cb0ef41Sopenharmony_ci<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>] });
11421cb0ef41Sopenharmony_ci
11431cb0ef41Sopenharmony_ci<span class="hljs-comment">// Open an extra fd=4, to interact with programs presenting a</span>
11441cb0ef41Sopenharmony_ci<span class="hljs-comment">// startd-style interface.</span>
11451cb0ef41Sopenharmony_ci<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>
11461cb0ef41Sopenharmony_ci<p><em>It is worth noting that when an IPC channel is established between the
11471cb0ef41Sopenharmony_ciparent and child processes, and the child is a Node.js process, the child
11481cb0ef41Sopenharmony_ciis launched with the IPC channel unreferenced (using <code>unref()</code>) until the
11491cb0ef41Sopenharmony_cichild registers an event handler for the <a href="process.html#event-disconnect"><code>'disconnect'</code></a> event
11501cb0ef41Sopenharmony_cior the <a href="process.html#event-message"><code>'message'</code></a> event. This allows the child to exit
11511cb0ef41Sopenharmony_cinormally without the process being held open by the open IPC channel.</em></p>
11521cb0ef41Sopenharmony_ci<p>On Unix-like operating systems, the <a href="#child_processspawncommand-args-options"><code>child_process.spawn()</code></a> method
11531cb0ef41Sopenharmony_ciperforms memory operations synchronously before decoupling the event loop
11541cb0ef41Sopenharmony_cifrom the child. Applications with a large memory footprint may find frequent
11551cb0ef41Sopenharmony_ci<a href="#child_processspawncommand-args-options"><code>child_process.spawn()</code></a> calls to be a bottleneck. For more information,
11561cb0ef41Sopenharmony_cisee <a href="https://bugs.chromium.org/p/v8/issues/detail?id=7381">V8 issue 7381</a>.</p>
11571cb0ef41Sopenharmony_ci<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>
11581cb0ef41Sopenharmony_ci</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>
11591cb0ef41Sopenharmony_ci<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
11601cb0ef41Sopenharmony_ci<a href="#child_processexecfilesyncfile-args-options"><code>child_process.execFileSync()</code></a> methods are synchronous and will block the
11611cb0ef41Sopenharmony_ciNode.js event loop, pausing execution of any additional code until the spawned
11621cb0ef41Sopenharmony_ciprocess exits.</p>
11631cb0ef41Sopenharmony_ci<p>Blocking calls like these are mostly useful for simplifying general-purpose
11641cb0ef41Sopenharmony_ciscripting tasks and for simplifying the loading/processing of application
11651cb0ef41Sopenharmony_ciconfiguration at startup.</p>
11661cb0ef41Sopenharmony_ci<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>
11671cb0ef41Sopenharmony_ci<div class="api_metadata">
11681cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
11691cb0ef41Sopenharmony_ci<table>
11701cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
11711cb0ef41Sopenharmony_ci<tr><td>v16.4.0, v14.18.0</td>
11721cb0ef41Sopenharmony_ci<td><p>The <code>cwd</code> option can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
11731cb0ef41Sopenharmony_ci<tr><td>v10.10.0</td>
11741cb0ef41Sopenharmony_ci<td><p>The <code>input</code> option can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr>
11751cb0ef41Sopenharmony_ci<tr><td>v8.8.0</td>
11761cb0ef41Sopenharmony_ci<td><p>The <code>windowsHide</code> option is supported now.</p></td></tr>
11771cb0ef41Sopenharmony_ci<tr><td>v8.0.0</td>
11781cb0ef41Sopenharmony_ci<td><p>The <code>input</code> option can now be a <code>Uint8Array</code>.</p></td></tr>
11791cb0ef41Sopenharmony_ci<tr><td>v6.2.1, v4.5.0</td>
11801cb0ef41Sopenharmony_ci<td><p>The <code>encoding</code> option can now explicitly be set to <code>buffer</code>.</p></td></tr>
11811cb0ef41Sopenharmony_ci<tr><td>v0.11.12</td>
11821cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.12</span></p></td></tr>
11831cb0ef41Sopenharmony_ci</tbody></table>
11841cb0ef41Sopenharmony_ci</details>
11851cb0ef41Sopenharmony_ci</div>
11861cb0ef41Sopenharmony_ci<ul>
11871cb0ef41Sopenharmony_ci<li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The name or path of the executable file to run.</li>
11881cb0ef41Sopenharmony_ci<li><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> List of string arguments.</li>
11891cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
11901cb0ef41Sopenharmony_ci<ul>
11911cb0ef41Sopenharmony_ci<li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> Current working directory of the child process.</li>
11921cb0ef41Sopenharmony_ci<li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> The value which will be passed
11931cb0ef41Sopenharmony_cias stdin to the spawned process. Supplying this value will override
11941cb0ef41Sopenharmony_ci<code>stdio[0]</code>.</li>
11951cb0ef41Sopenharmony_ci<li><code>stdio</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&#x3C;Array></a> Child's stdio configuration. <code>stderr</code> by default will
11961cb0ef41Sopenharmony_cibe output to the parent process' stderr unless <code>stdio</code> is specified.
11971cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>'pipe'</code>.</li>
11981cb0ef41Sopenharmony_ci<li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> Environment key-value pairs. <strong>Default:</strong> <code>process.env</code>.</li>
11991cb0ef41Sopenharmony_ci<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;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>
12001cb0ef41Sopenharmony_ci<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;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>
12011cb0ef41Sopenharmony_ci<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> In milliseconds the maximum amount of time the process
12021cb0ef41Sopenharmony_ciis allowed to run. <strong>Default:</strong> <code>undefined</code>.</li>
12031cb0ef41Sopenharmony_ci<li><code>killSignal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The signal value to be used when the spawned
12041cb0ef41Sopenharmony_ciprocess will be killed. <strong>Default:</strong> <code>'SIGTERM'</code>.</li>
12051cb0ef41Sopenharmony_ci<li><code>maxBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Largest amount of data in bytes allowed on stdout or
12061cb0ef41Sopenharmony_cistderr. If exceeded, the child process is terminated. See caveat at
12071cb0ef41Sopenharmony_ci<a href="#maxbuffer-and-unicode"><code>maxBuffer</code> and Unicode</a>. <strong>Default:</strong> <code>1024 * 1024</code>.</li>
12081cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The encoding used for all stdio inputs and outputs.
12091cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>'buffer'</code>.</li>
12101cb0ef41Sopenharmony_ci<li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Hide the subprocess console window that would
12111cb0ef41Sopenharmony_cinormally be created on Windows systems. <strong>Default:</strong> <code>false</code>.</li>
12121cb0ef41Sopenharmony_ci<li><code>shell</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> If <code>true</code>, runs <code>command</code> inside of a shell. Uses
12131cb0ef41Sopenharmony_ci<code>'/bin/sh'</code> on Unix, and <code>process.env.ComSpec</code> on Windows. A different
12141cb0ef41Sopenharmony_cishell can be specified as a string. See <a href="#shell-requirements">Shell requirements</a> and
12151cb0ef41Sopenharmony_ci<a href="#default-windows-shell">Default Windows shell</a>. <strong>Default:</strong> <code>false</code> (no shell).</li>
12161cb0ef41Sopenharmony_ci</ul>
12171cb0ef41Sopenharmony_ci</li>
12181cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The stdout from the command.</li>
12191cb0ef41Sopenharmony_ci</ul>
12201cb0ef41Sopenharmony_ci<p>The <code>child_process.execFileSync()</code> method is generally identical to
12211cb0ef41Sopenharmony_ci<a href="#child_processexecfilefile-args-options-callback"><code>child_process.execFile()</code></a> with the exception that the method will not
12221cb0ef41Sopenharmony_cireturn until the child process has fully closed. When a timeout has been
12231cb0ef41Sopenharmony_ciencountered and <code>killSignal</code> is sent, the method won't return until the process
12241cb0ef41Sopenharmony_cihas completely exited.</p>
12251cb0ef41Sopenharmony_ci<p>If the child process intercepts and handles the <code>SIGTERM</code> signal and
12261cb0ef41Sopenharmony_cidoes not exit, the parent process will still wait until the child process has
12271cb0ef41Sopenharmony_ciexited.</p>
12281cb0ef41Sopenharmony_ci<p>If the process times out or has a non-zero exit code, this method will throw an
12291cb0ef41Sopenharmony_ci<a href="errors.html#class-error"><code>Error</code></a> that will include the full result of the underlying
12301cb0ef41Sopenharmony_ci<a href="#child_processspawnsynccommand-args-options"><code>child_process.spawnSync()</code></a>.</p>
12311cb0ef41Sopenharmony_ci<p><strong>If the <code>shell</code> option is enabled, do not pass unsanitized user input to this
12321cb0ef41Sopenharmony_cifunction. Any input containing shell metacharacters may be used to trigger
12331cb0ef41Sopenharmony_ciarbitrary command execution.</strong></p>
12341cb0ef41Sopenharmony_ci<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>
12351cb0ef41Sopenharmony_ci<div class="api_metadata">
12361cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
12371cb0ef41Sopenharmony_ci<table>
12381cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
12391cb0ef41Sopenharmony_ci<tr><td>v16.4.0, v14.18.0</td>
12401cb0ef41Sopenharmony_ci<td><p>The <code>cwd</code> option can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
12411cb0ef41Sopenharmony_ci<tr><td>v10.10.0</td>
12421cb0ef41Sopenharmony_ci<td><p>The <code>input</code> option can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr>
12431cb0ef41Sopenharmony_ci<tr><td>v8.8.0</td>
12441cb0ef41Sopenharmony_ci<td><p>The <code>windowsHide</code> option is supported now.</p></td></tr>
12451cb0ef41Sopenharmony_ci<tr><td>v8.0.0</td>
12461cb0ef41Sopenharmony_ci<td><p>The <code>input</code> option can now be a <code>Uint8Array</code>.</p></td></tr>
12471cb0ef41Sopenharmony_ci<tr><td>v0.11.12</td>
12481cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.12</span></p></td></tr>
12491cb0ef41Sopenharmony_ci</tbody></table>
12501cb0ef41Sopenharmony_ci</details>
12511cb0ef41Sopenharmony_ci</div>
12521cb0ef41Sopenharmony_ci<ul>
12531cb0ef41Sopenharmony_ci<li><code>command</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The command to run.</li>
12541cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
12551cb0ef41Sopenharmony_ci<ul>
12561cb0ef41Sopenharmony_ci<li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> Current working directory of the child process.</li>
12571cb0ef41Sopenharmony_ci<li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> The value which will be passed
12581cb0ef41Sopenharmony_cias stdin to the spawned process. Supplying this value will override
12591cb0ef41Sopenharmony_ci<code>stdio[0]</code>.</li>
12601cb0ef41Sopenharmony_ci<li><code>stdio</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&#x3C;Array></a> Child's stdio configuration. <code>stderr</code> by default will
12611cb0ef41Sopenharmony_cibe output to the parent process' stderr unless <code>stdio</code> is specified.
12621cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>'pipe'</code>.</li>
12631cb0ef41Sopenharmony_ci<li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> Environment key-value pairs. <strong>Default:</strong> <code>process.env</code>.</li>
12641cb0ef41Sopenharmony_ci<li><code>shell</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Shell to execute the command with. See
12651cb0ef41Sopenharmony_ci<a href="#shell-requirements">Shell requirements</a> and <a href="#default-windows-shell">Default Windows shell</a>. <strong>Default:</strong>
12661cb0ef41Sopenharmony_ci<code>'/bin/sh'</code> on Unix, <code>process.env.ComSpec</code> on Windows.</li>
12671cb0ef41Sopenharmony_ci<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;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>
12681cb0ef41Sopenharmony_ci<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;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>
12691cb0ef41Sopenharmony_ci<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> In milliseconds the maximum amount of time the process
12701cb0ef41Sopenharmony_ciis allowed to run. <strong>Default:</strong> <code>undefined</code>.</li>
12711cb0ef41Sopenharmony_ci<li><code>killSignal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The signal value to be used when the spawned
12721cb0ef41Sopenharmony_ciprocess will be killed. <strong>Default:</strong> <code>'SIGTERM'</code>.</li>
12731cb0ef41Sopenharmony_ci<li><code>maxBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Largest amount of data in bytes allowed on stdout or
12741cb0ef41Sopenharmony_cistderr. If exceeded, the child process is terminated and any output is
12751cb0ef41Sopenharmony_citruncated. See caveat at <a href="#maxbuffer-and-unicode"><code>maxBuffer</code> and Unicode</a>.
12761cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>1024 * 1024</code>.</li>
12771cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The encoding used for all stdio inputs and outputs.
12781cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>'buffer'</code>.</li>
12791cb0ef41Sopenharmony_ci<li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Hide the subprocess console window that would
12801cb0ef41Sopenharmony_cinormally be created on Windows systems. <strong>Default:</strong> <code>false</code>.</li>
12811cb0ef41Sopenharmony_ci</ul>
12821cb0ef41Sopenharmony_ci</li>
12831cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The stdout from the command.</li>
12841cb0ef41Sopenharmony_ci</ul>
12851cb0ef41Sopenharmony_ci<p>The <code>child_process.execSync()</code> method is generally identical to
12861cb0ef41Sopenharmony_ci<a href="#child_processexeccommand-options-callback"><code>child_process.exec()</code></a> with the exception that the method will not return
12871cb0ef41Sopenharmony_ciuntil the child process has fully closed. When a timeout has been encountered
12881cb0ef41Sopenharmony_ciand <code>killSignal</code> is sent, the method won't return until the process has
12891cb0ef41Sopenharmony_cicompletely exited. If the child process intercepts and handles the <code>SIGTERM</code>
12901cb0ef41Sopenharmony_cisignal and doesn't exit, the parent process will wait until the child process
12911cb0ef41Sopenharmony_cihas exited.</p>
12921cb0ef41Sopenharmony_ci<p>If the process times out or has a non-zero exit code, this method will throw.
12931cb0ef41Sopenharmony_ciThe <a href="errors.html#class-error"><code>Error</code></a> object will contain the entire result from
12941cb0ef41Sopenharmony_ci<a href="#child_processspawnsynccommand-args-options"><code>child_process.spawnSync()</code></a>.</p>
12951cb0ef41Sopenharmony_ci<p><strong>Never pass unsanitized user input to this function. Any input containing shell
12961cb0ef41Sopenharmony_cimetacharacters may be used to trigger arbitrary command execution.</strong></p>
12971cb0ef41Sopenharmony_ci<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>
12981cb0ef41Sopenharmony_ci<div class="api_metadata">
12991cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
13001cb0ef41Sopenharmony_ci<table>
13011cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
13021cb0ef41Sopenharmony_ci<tr><td>v16.4.0, v14.18.0</td>
13031cb0ef41Sopenharmony_ci<td><p>The <code>cwd</code> option can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
13041cb0ef41Sopenharmony_ci<tr><td>v10.10.0</td>
13051cb0ef41Sopenharmony_ci<td><p>The <code>input</code> option can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr>
13061cb0ef41Sopenharmony_ci<tr><td>v8.8.0</td>
13071cb0ef41Sopenharmony_ci<td><p>The <code>windowsHide</code> option is supported now.</p></td></tr>
13081cb0ef41Sopenharmony_ci<tr><td>v8.0.0</td>
13091cb0ef41Sopenharmony_ci<td><p>The <code>input</code> option can now be a <code>Uint8Array</code>.</p></td></tr>
13101cb0ef41Sopenharmony_ci<tr><td>v5.7.0</td>
13111cb0ef41Sopenharmony_ci<td><p>The <code>shell</code> option is supported now.</p></td></tr>
13121cb0ef41Sopenharmony_ci<tr><td>v6.2.1, v4.5.0</td>
13131cb0ef41Sopenharmony_ci<td><p>The <code>encoding</code> option can now explicitly be set to <code>buffer</code>.</p></td></tr>
13141cb0ef41Sopenharmony_ci<tr><td>v0.11.12</td>
13151cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.12</span></p></td></tr>
13161cb0ef41Sopenharmony_ci</tbody></table>
13171cb0ef41Sopenharmony_ci</details>
13181cb0ef41Sopenharmony_ci</div>
13191cb0ef41Sopenharmony_ci<ul>
13201cb0ef41Sopenharmony_ci<li><code>command</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The command to run.</li>
13211cb0ef41Sopenharmony_ci<li><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> List of string arguments.</li>
13221cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
13231cb0ef41Sopenharmony_ci<ul>
13241cb0ef41Sopenharmony_ci<li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> Current working directory of the child process.</li>
13251cb0ef41Sopenharmony_ci<li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> The value which will be passed
13261cb0ef41Sopenharmony_cias stdin to the spawned process. Supplying this value will override
13271cb0ef41Sopenharmony_ci<code>stdio[0]</code>.</li>
13281cb0ef41Sopenharmony_ci<li><code>argv0</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Explicitly set the value of <code>argv[0]</code> sent to the child
13291cb0ef41Sopenharmony_ciprocess. This will be set to <code>command</code> if not specified.</li>
13301cb0ef41Sopenharmony_ci<li><code>stdio</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&#x3C;Array></a> Child's stdio configuration.</li>
13311cb0ef41Sopenharmony_ci<li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> Environment key-value pairs. <strong>Default:</strong> <code>process.env</code>.</li>
13321cb0ef41Sopenharmony_ci<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;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>
13331cb0ef41Sopenharmony_ci<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;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>
13341cb0ef41Sopenharmony_ci<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> In milliseconds the maximum amount of time the process
13351cb0ef41Sopenharmony_ciis allowed to run. <strong>Default:</strong> <code>undefined</code>.</li>
13361cb0ef41Sopenharmony_ci<li><code>killSignal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The signal value to be used when the spawned
13371cb0ef41Sopenharmony_ciprocess will be killed. <strong>Default:</strong> <code>'SIGTERM'</code>.</li>
13381cb0ef41Sopenharmony_ci<li><code>maxBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Largest amount of data in bytes allowed on stdout or
13391cb0ef41Sopenharmony_cistderr. If exceeded, the child process is terminated and any output is
13401cb0ef41Sopenharmony_citruncated. See caveat at <a href="#maxbuffer-and-unicode"><code>maxBuffer</code> and Unicode</a>.
13411cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>1024 * 1024</code>.</li>
13421cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The encoding used for all stdio inputs and outputs.
13431cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>'buffer'</code>.</li>
13441cb0ef41Sopenharmony_ci<li><code>shell</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> If <code>true</code>, runs <code>command</code> inside of a shell. Uses
13451cb0ef41Sopenharmony_ci<code>'/bin/sh'</code> on Unix, and <code>process.env.ComSpec</code> on Windows. A different
13461cb0ef41Sopenharmony_cishell can be specified as a string. See <a href="#shell-requirements">Shell requirements</a> and
13471cb0ef41Sopenharmony_ci<a href="#default-windows-shell">Default Windows shell</a>. <strong>Default:</strong> <code>false</code> (no shell).</li>
13481cb0ef41Sopenharmony_ci<li><code>windowsVerbatimArguments</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> No quoting or escaping of arguments is
13491cb0ef41Sopenharmony_cidone on Windows. Ignored on Unix. This is set to <code>true</code> automatically
13501cb0ef41Sopenharmony_ciwhen <code>shell</code> is specified and is CMD. <strong>Default:</strong> <code>false</code>.</li>
13511cb0ef41Sopenharmony_ci<li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Hide the subprocess console window that would
13521cb0ef41Sopenharmony_cinormally be created on Windows systems. <strong>Default:</strong> <code>false</code>.</li>
13531cb0ef41Sopenharmony_ci</ul>
13541cb0ef41Sopenharmony_ci</li>
13551cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
13561cb0ef41Sopenharmony_ci<ul>
13571cb0ef41Sopenharmony_ci<li><code>pid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Pid of the child process.</li>
13581cb0ef41Sopenharmony_ci<li><code>output</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&#x3C;Array></a> Array of results from stdio output.</li>
13591cb0ef41Sopenharmony_ci<li><code>stdout</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The contents of <code>output[1]</code>.</li>
13601cb0ef41Sopenharmony_ci<li><code>stderr</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The contents of <code>output[2]</code>.</li>
13611cb0ef41Sopenharmony_ci<li><code>status</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> The exit code of the subprocess, or <code>null</code> if the
13621cb0ef41Sopenharmony_cisubprocess terminated due to a signal.</li>
13631cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> The signal used to kill the subprocess, or <code>null</code> if
13641cb0ef41Sopenharmony_cithe subprocess did not terminate due to a signal.</li>
13651cb0ef41Sopenharmony_ci<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> The error object if the child process failed or timed out.</li>
13661cb0ef41Sopenharmony_ci</ul>
13671cb0ef41Sopenharmony_ci</li>
13681cb0ef41Sopenharmony_ci</ul>
13691cb0ef41Sopenharmony_ci<p>The <code>child_process.spawnSync()</code> method is generally identical to
13701cb0ef41Sopenharmony_ci<a href="#child_processspawncommand-args-options"><code>child_process.spawn()</code></a> with the exception that the function will not return
13711cb0ef41Sopenharmony_ciuntil the child process has fully closed. When a timeout has been encountered
13721cb0ef41Sopenharmony_ciand <code>killSignal</code> is sent, the method won't return until the process has
13731cb0ef41Sopenharmony_cicompletely exited. If the process intercepts and handles the <code>SIGTERM</code> signal
13741cb0ef41Sopenharmony_ciand doesn't exit, the parent process will wait until the child process has
13751cb0ef41Sopenharmony_ciexited.</p>
13761cb0ef41Sopenharmony_ci<p><strong>If the <code>shell</code> option is enabled, do not pass unsanitized user input to this
13771cb0ef41Sopenharmony_cifunction. Any input containing shell metacharacters may be used to trigger
13781cb0ef41Sopenharmony_ciarbitrary command execution.</strong></p>
13791cb0ef41Sopenharmony_ci</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>
13801cb0ef41Sopenharmony_ci<div class="api_metadata">
13811cb0ef41Sopenharmony_ci<span>Added in: v2.2.0</span>
13821cb0ef41Sopenharmony_ci</div>
13831cb0ef41Sopenharmony_ci<ul>
13841cb0ef41Sopenharmony_ci<li>Extends: <a href="events.html#class-eventemitter" class="type">&#x3C;EventEmitter></a></li>
13851cb0ef41Sopenharmony_ci</ul>
13861cb0ef41Sopenharmony_ci<p>Instances of the <code>ChildProcess</code> represent spawned child processes.</p>
13871cb0ef41Sopenharmony_ci<p>Instances of <code>ChildProcess</code> are not intended to be created directly. Rather,
13881cb0ef41Sopenharmony_ciuse 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>,
13891cb0ef41Sopenharmony_ci<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
13901cb0ef41Sopenharmony_ciinstances of <code>ChildProcess</code>.</p>
13911cb0ef41Sopenharmony_ci<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>
13921cb0ef41Sopenharmony_ci<div class="api_metadata">
13931cb0ef41Sopenharmony_ci<span>Added in: v0.7.7</span>
13941cb0ef41Sopenharmony_ci</div>
13951cb0ef41Sopenharmony_ci<ul>
13961cb0ef41Sopenharmony_ci<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The exit code if the child exited on its own.</li>
13971cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The signal by which the child process was terminated.</li>
13981cb0ef41Sopenharmony_ci</ul>
13991cb0ef41Sopenharmony_ci<p>The <code>'close'</code> event is emitted after a process has ended <em>and</em> the stdio
14001cb0ef41Sopenharmony_cistreams of a child process have been closed. This is distinct from the
14011cb0ef41Sopenharmony_ci<a href="#event-exit"><code>'exit'</code></a> event, since multiple processes might share the same stdio
14021cb0ef41Sopenharmony_cistreams. The <code>'close'</code> event will always emit after <a href="#event-exit"><code>'exit'</code></a> was
14031cb0ef41Sopenharmony_cialready emitted, or <a href="#event-error"><code>'error'</code></a> if the child failed to spawn.</p>
14041cb0ef41Sopenharmony_ci<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>);
14051cb0ef41Sopenharmony_ci<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>]);
14061cb0ef41Sopenharmony_ci
14071cb0ef41Sopenharmony_cils.<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> {
14081cb0ef41Sopenharmony_ci  <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>);
14091cb0ef41Sopenharmony_ci});
14101cb0ef41Sopenharmony_ci
14111cb0ef41Sopenharmony_cils.<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-function">(<span class="hljs-params">code</span>) =></span> {
14121cb0ef41Sopenharmony_ci  <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>);
14131cb0ef41Sopenharmony_ci});
14141cb0ef41Sopenharmony_ci
14151cb0ef41Sopenharmony_cils.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">(<span class="hljs-params">code</span>) =></span> {
14161cb0ef41Sopenharmony_ci  <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>);
14171cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
14181cb0ef41Sopenharmony_ci<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>
14191cb0ef41Sopenharmony_ci<div class="api_metadata">
14201cb0ef41Sopenharmony_ci<span>Added in: v0.7.2</span>
14211cb0ef41Sopenharmony_ci</div>
14221cb0ef41Sopenharmony_ci<p>The <code>'disconnect'</code> event is emitted after calling the
14231cb0ef41Sopenharmony_ci<a href="#subprocessdisconnect"><code>subprocess.disconnect()</code></a> method in parent process or
14241cb0ef41Sopenharmony_ci<a href="process.html#processdisconnect"><code>process.disconnect()</code></a> in child process. After disconnecting it is no longer
14251cb0ef41Sopenharmony_cipossible to send or receive messages, and the <a href="#subprocessconnected"><code>subprocess.connected</code></a>
14261cb0ef41Sopenharmony_ciproperty is <code>false</code>.</p>
14271cb0ef41Sopenharmony_ci<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>
14281cb0ef41Sopenharmony_ci<ul>
14291cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> The error.</li>
14301cb0ef41Sopenharmony_ci</ul>
14311cb0ef41Sopenharmony_ci<p>The <code>'error'</code> event is emitted whenever:</p>
14321cb0ef41Sopenharmony_ci<ul>
14331cb0ef41Sopenharmony_ci<li>The process could not be spawned.</li>
14341cb0ef41Sopenharmony_ci<li>The process could not be killed.</li>
14351cb0ef41Sopenharmony_ci<li>Sending a message to the child process failed.</li>
14361cb0ef41Sopenharmony_ci<li>The child process was aborted via the <code>signal</code> option.</li>
14371cb0ef41Sopenharmony_ci</ul>
14381cb0ef41Sopenharmony_ci<p>The <code>'exit'</code> event may or may not fire after an error has occurred. When
14391cb0ef41Sopenharmony_cilistening to both the <code>'exit'</code> and <code>'error'</code> events, guard
14401cb0ef41Sopenharmony_ciagainst accidentally invoking handler functions multiple times.</p>
14411cb0ef41Sopenharmony_ci<p>See also <a href="#subprocesskillsignal"><code>subprocess.kill()</code></a> and <a href="#subprocesssendmessage-sendhandle-options-callback"><code>subprocess.send()</code></a>.</p>
14421cb0ef41Sopenharmony_ci<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>
14431cb0ef41Sopenharmony_ci<div class="api_metadata">
14441cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span>
14451cb0ef41Sopenharmony_ci</div>
14461cb0ef41Sopenharmony_ci<ul>
14471cb0ef41Sopenharmony_ci<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The exit code if the child exited on its own.</li>
14481cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The signal by which the child process was terminated.</li>
14491cb0ef41Sopenharmony_ci</ul>
14501cb0ef41Sopenharmony_ci<p>The <code>'exit'</code> event is emitted after the child process ends. If the process
14511cb0ef41Sopenharmony_ciexited, <code>code</code> is the final exit code of the process, otherwise <code>null</code>. If the
14521cb0ef41Sopenharmony_ciprocess terminated due to receipt of a signal, <code>signal</code> is the string name of
14531cb0ef41Sopenharmony_cithe signal, otherwise <code>null</code>. One of the two will always be non-<code>null</code>.</p>
14541cb0ef41Sopenharmony_ci<p>When the <code>'exit'</code> event is triggered, child process stdio streams might still be
14551cb0ef41Sopenharmony_ciopen.</p>
14561cb0ef41Sopenharmony_ci<p>Node.js establishes signal handlers for <code>SIGINT</code> and <code>SIGTERM</code> and Node.js
14571cb0ef41Sopenharmony_ciprocesses will not terminate immediately due to receipt of those signals.
14581cb0ef41Sopenharmony_ciRather, Node.js will perform a sequence of cleanup actions and then will
14591cb0ef41Sopenharmony_cire-raise the handled signal.</p>
14601cb0ef41Sopenharmony_ci<p>See <a href="http://man7.org/linux/man-pages/man2/waitpid.2.html"><code>waitpid(2)</code></a>.</p>
14611cb0ef41Sopenharmony_ci<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>
14621cb0ef41Sopenharmony_ci<div class="api_metadata">
14631cb0ef41Sopenharmony_ci<span>Added in: v0.5.9</span>
14641cb0ef41Sopenharmony_ci</div>
14651cb0ef41Sopenharmony_ci<ul>
14661cb0ef41Sopenharmony_ci<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> A parsed JSON object or primitive value.</li>
14671cb0ef41Sopenharmony_ci<li><code>sendHandle</code> <a href="net.html#serverlistenhandle-backlog-callback" class="type">&#x3C;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
14681cb0ef41Sopenharmony_ciundefined.</li>
14691cb0ef41Sopenharmony_ci</ul>
14701cb0ef41Sopenharmony_ci<p>The <code>'message'</code> event is triggered when a child process uses
14711cb0ef41Sopenharmony_ci<a href="process.html#processsendmessage-sendhandle-options-callback"><code>process.send()</code></a> to send messages.</p>
14721cb0ef41Sopenharmony_ci<p>The message goes through serialization and parsing. The resulting
14731cb0ef41Sopenharmony_cimessage might not be the same as what is originally sent.</p>
14741cb0ef41Sopenharmony_ci<p>If the <code>serialization</code> option was set to <code>'advanced'</code> used when spawning the
14751cb0ef41Sopenharmony_cichild process, the <code>message</code> argument can contain data that JSON is not able
14761cb0ef41Sopenharmony_cito represent.
14771cb0ef41Sopenharmony_ciSee <a href="#advanced-serialization">Advanced serialization</a> for more details.</p>
14781cb0ef41Sopenharmony_ci<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>
14791cb0ef41Sopenharmony_ci<div class="api_metadata">
14801cb0ef41Sopenharmony_ci<span>Added in: v15.1.0, v14.17.0</span>
14811cb0ef41Sopenharmony_ci</div>
14821cb0ef41Sopenharmony_ci<p>The <code>'spawn'</code> event is emitted once the child process has spawned successfully.
14831cb0ef41Sopenharmony_ciIf the child process does not spawn successfully, the <code>'spawn'</code> event is not
14841cb0ef41Sopenharmony_ciemitted and the <code>'error'</code> event is emitted instead.</p>
14851cb0ef41Sopenharmony_ci<p>If emitted, the <code>'spawn'</code> event comes before all other events and before any
14861cb0ef41Sopenharmony_cidata is received via <code>stdout</code> or <code>stderr</code>.</p>
14871cb0ef41Sopenharmony_ci<p>The <code>'spawn'</code> event will fire regardless of whether an error occurs <strong>within</strong>
14881cb0ef41Sopenharmony_cithe spawned process. For example, if <code>bash some-command</code> spawns successfully,
14891cb0ef41Sopenharmony_cithe <code>'spawn'</code> event will fire, though <code>bash</code> may fail to spawn <code>some-command</code>.
14901cb0ef41Sopenharmony_ciThis caveat also applies when using <code>{ shell: true }</code>.</p>
14911cb0ef41Sopenharmony_ci<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>
14921cb0ef41Sopenharmony_ci<div class="api_metadata">
14931cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
14941cb0ef41Sopenharmony_ci<table>
14951cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
14961cb0ef41Sopenharmony_ci<tr><td>v14.0.0</td>
14971cb0ef41Sopenharmony_ci<td><p>The object no longer accidentally exposes native C++ bindings.</p></td></tr>
14981cb0ef41Sopenharmony_ci<tr><td>v7.1.0</td>
14991cb0ef41Sopenharmony_ci<td><p><span>Added in: v7.1.0</span></p></td></tr>
15001cb0ef41Sopenharmony_ci</tbody></table>
15011cb0ef41Sopenharmony_ci</details>
15021cb0ef41Sopenharmony_ci</div>
15031cb0ef41Sopenharmony_ci<ul>
15041cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> A pipe representing the IPC channel to the child process.</li>
15051cb0ef41Sopenharmony_ci</ul>
15061cb0ef41Sopenharmony_ci<p>The <code>subprocess.channel</code> property is a reference to the child's IPC channel. If
15071cb0ef41Sopenharmony_cino IPC channel exists, this property is <code>undefined</code>.</p>
15081cb0ef41Sopenharmony_ci<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>
15091cb0ef41Sopenharmony_ci<div class="api_metadata">
15101cb0ef41Sopenharmony_ci<span>Added in: v7.1.0</span>
15111cb0ef41Sopenharmony_ci</div>
15121cb0ef41Sopenharmony_ci<p>This method makes the IPC channel keep the event loop of the parent process
15131cb0ef41Sopenharmony_cirunning if <code>.unref()</code> has been called before.</p>
15141cb0ef41Sopenharmony_ci<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>
15151cb0ef41Sopenharmony_ci<div class="api_metadata">
15161cb0ef41Sopenharmony_ci<span>Added in: v7.1.0</span>
15171cb0ef41Sopenharmony_ci</div>
15181cb0ef41Sopenharmony_ci<p>This method makes the IPC channel not keep the event loop of the parent process
15191cb0ef41Sopenharmony_cirunning, and lets it finish even while the channel is open.</p>
15201cb0ef41Sopenharmony_ci<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>
15211cb0ef41Sopenharmony_ci<div class="api_metadata">
15221cb0ef41Sopenharmony_ci<span>Added in: v0.7.2</span>
15231cb0ef41Sopenharmony_ci</div>
15241cb0ef41Sopenharmony_ci<ul>
15251cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Set to <code>false</code> after <code>subprocess.disconnect()</code> is called.</li>
15261cb0ef41Sopenharmony_ci</ul>
15271cb0ef41Sopenharmony_ci<p>The <code>subprocess.connected</code> property indicates whether it is still possible to
15281cb0ef41Sopenharmony_cisend and receive messages from a child process. When <code>subprocess.connected</code> is
15291cb0ef41Sopenharmony_ci<code>false</code>, it is no longer possible to send or receive messages.</p>
15301cb0ef41Sopenharmony_ci<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>
15311cb0ef41Sopenharmony_ci<div class="api_metadata">
15321cb0ef41Sopenharmony_ci<span>Added in: v0.7.2</span>
15331cb0ef41Sopenharmony_ci</div>
15341cb0ef41Sopenharmony_ci<p>Closes the IPC channel between parent and child, allowing the child to exit
15351cb0ef41Sopenharmony_cigracefully once there are no other connections keeping it alive. After calling
15361cb0ef41Sopenharmony_cithis method the <code>subprocess.connected</code> and <code>process.connected</code> properties in
15371cb0ef41Sopenharmony_ciboth the parent and child (respectively) will be set to <code>false</code>, and it will be
15381cb0ef41Sopenharmony_cino longer possible to pass messages between the processes.</p>
15391cb0ef41Sopenharmony_ci<p>The <code>'disconnect'</code> event will be emitted when there are no messages in the
15401cb0ef41Sopenharmony_ciprocess of being received. This will most often be triggered immediately after
15411cb0ef41Sopenharmony_cicalling <code>subprocess.disconnect()</code>.</p>
15421cb0ef41Sopenharmony_ci<p>When the child process is a Node.js instance (e.g. spawned using
15431cb0ef41Sopenharmony_ci<a href="#child_processforkmodulepath-args-options"><code>child_process.fork()</code></a>), the <code>process.disconnect()</code> method can be invoked
15441cb0ef41Sopenharmony_ciwithin the child process to close the IPC channel as well.</p>
15451cb0ef41Sopenharmony_ci<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>
15461cb0ef41Sopenharmony_ci<ul>
15471cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
15481cb0ef41Sopenharmony_ci</ul>
15491cb0ef41Sopenharmony_ci<p>The <code>subprocess.exitCode</code> property indicates the exit code of the child process.
15501cb0ef41Sopenharmony_ciIf the child process is still running, the field will be <code>null</code>.</p>
15511cb0ef41Sopenharmony_ci<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>
15521cb0ef41Sopenharmony_ci<div class="api_metadata">
15531cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span>
15541cb0ef41Sopenharmony_ci</div>
15551cb0ef41Sopenharmony_ci<ul>
15561cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
15571cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
15581cb0ef41Sopenharmony_ci</ul>
15591cb0ef41Sopenharmony_ci<p>The <code>subprocess.kill()</code> method sends a signal to the child process. If no
15601cb0ef41Sopenharmony_ciargument is given, the process will be sent the <code>'SIGTERM'</code> signal. See
15611cb0ef41Sopenharmony_ci<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
15621cb0ef41Sopenharmony_ci<a href="http://man7.org/linux/man-pages/man2/kill.2.html"><code>kill(2)</code></a> succeeds, and <code>false</code> otherwise.</p>
15631cb0ef41Sopenharmony_ci<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>);
15641cb0ef41Sopenharmony_ci<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>]);
15651cb0ef41Sopenharmony_ci
15661cb0ef41Sopenharmony_cigrep.<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> {
15671cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(
15681cb0ef41Sopenharmony_ci    <span class="hljs-string">`child process terminated due to receipt of signal <span class="hljs-subst">${signal}</span>`</span>);
15691cb0ef41Sopenharmony_ci});
15701cb0ef41Sopenharmony_ci
15711cb0ef41Sopenharmony_ci<span class="hljs-comment">// Send SIGHUP to process.</span>
15721cb0ef41Sopenharmony_cigrep.<span class="hljs-title function_">kill</span>(<span class="hljs-string">'SIGHUP'</span>);</code> <button class="copy-button">copy</button></pre>
15731cb0ef41Sopenharmony_ci<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
15741cb0ef41Sopenharmony_cicannot be delivered. Sending a signal to a child process that has already exited
15751cb0ef41Sopenharmony_ciis not an error but may have unforeseen consequences. Specifically, if the
15761cb0ef41Sopenharmony_ciprocess identifier (PID) has been reassigned to another process, the signal will
15771cb0ef41Sopenharmony_cibe delivered to that process instead which can have unexpected results.</p>
15781cb0ef41Sopenharmony_ci<p>While the function is called <code>kill</code>, the signal delivered to the child process
15791cb0ef41Sopenharmony_cimay not actually terminate the process.</p>
15801cb0ef41Sopenharmony_ci<p>See <a href="http://man7.org/linux/man-pages/man2/kill.2.html"><code>kill(2)</code></a> for reference.</p>
15811cb0ef41Sopenharmony_ci<p>On Windows, where POSIX signals do not exist, the <code>signal</code> argument will be
15821cb0ef41Sopenharmony_ciignored, and the process will be killed forcefully and abruptly (similar to
15831cb0ef41Sopenharmony_ci<code>'SIGKILL'</code>).
15841cb0ef41Sopenharmony_ciSee <a href="process.html#signal-events">Signal Events</a> for more details.</p>
15851cb0ef41Sopenharmony_ci<p>On Linux, child processes of child processes will not be terminated
15861cb0ef41Sopenharmony_ciwhen attempting to kill their parent. This is likely to happen when running a
15871cb0ef41Sopenharmony_cinew process in a shell or with the use of the <code>shell</code> option of <code>ChildProcess</code>:</p>
15881cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-meta">'use strict'</span>;
15891cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:child_process'</span>);
15901cb0ef41Sopenharmony_ci
15911cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span>(
15921cb0ef41Sopenharmony_ci  <span class="hljs-string">'sh'</span>,
15931cb0ef41Sopenharmony_ci  [
15941cb0ef41Sopenharmony_ci    <span class="hljs-string">'-c'</span>,
15951cb0ef41Sopenharmony_ci    <span class="hljs-string">`node -e "setInterval(() => {
15961cb0ef41Sopenharmony_ci      console.log(process.pid, 'is alive')
15971cb0ef41Sopenharmony_ci    }, 500);"`</span>,
15981cb0ef41Sopenharmony_ci  ], {
15991cb0ef41Sopenharmony_ci    <span class="hljs-attr">stdio</span>: [<span class="hljs-string">'inherit'</span>, <span class="hljs-string">'inherit'</span>, <span class="hljs-string">'inherit'</span>],
16001cb0ef41Sopenharmony_ci  },
16011cb0ef41Sopenharmony_ci);
16021cb0ef41Sopenharmony_ci
16031cb0ef41Sopenharmony_ci<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
16041cb0ef41Sopenharmony_ci  subprocess.<span class="hljs-title function_">kill</span>(); <span class="hljs-comment">// Does not terminate the Node.js process in the shell.</span>
16051cb0ef41Sopenharmony_ci}, <span class="hljs-number">2000</span>);</code> <button class="copy-button">copy</button></pre>
16061cb0ef41Sopenharmony_ci<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>
16071cb0ef41Sopenharmony_ci<div class="api_metadata">
16081cb0ef41Sopenharmony_ci<span>Added in: v18.18.0</span>
16091cb0ef41Sopenharmony_ci</div>
16101cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
16111cb0ef41Sopenharmony_ci<p>Calls <a href="#subprocesskillsignal"><code>subprocess.kill()</code></a> with <code>'SIGTERM'</code>.</p>
16121cb0ef41Sopenharmony_ci<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>
16131cb0ef41Sopenharmony_ci<div class="api_metadata">
16141cb0ef41Sopenharmony_ci<span>Added in: v0.5.10</span>
16151cb0ef41Sopenharmony_ci</div>
16161cb0ef41Sopenharmony_ci<ul>
16171cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Set to <code>true</code> after <code>subprocess.kill()</code> is used to successfully
16181cb0ef41Sopenharmony_cisend a signal to the child process.</li>
16191cb0ef41Sopenharmony_ci</ul>
16201cb0ef41Sopenharmony_ci<p>The <code>subprocess.killed</code> property indicates whether the child process
16211cb0ef41Sopenharmony_cisuccessfully received a signal from <code>subprocess.kill()</code>. The <code>killed</code> property
16221cb0ef41Sopenharmony_cidoes not indicate that the child process has been terminated.</p>
16231cb0ef41Sopenharmony_ci<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>
16241cb0ef41Sopenharmony_ci<div class="api_metadata">
16251cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span>
16261cb0ef41Sopenharmony_ci</div>
16271cb0ef41Sopenharmony_ci<ul>
16281cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a></li>
16291cb0ef41Sopenharmony_ci</ul>
16301cb0ef41Sopenharmony_ci<p>Returns the process identifier (PID) of the child process. If the child process
16311cb0ef41Sopenharmony_cifails to spawn due to errors, then the value is <code>undefined</code> and <code>error</code> is
16321cb0ef41Sopenharmony_ciemitted.</p>
16331cb0ef41Sopenharmony_ci<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>);
16341cb0ef41Sopenharmony_ci<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>]);
16351cb0ef41Sopenharmony_ci
16361cb0ef41Sopenharmony_ci<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>);
16371cb0ef41Sopenharmony_cigrep.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">end</span>();</code> <button class="copy-button">copy</button></pre>
16381cb0ef41Sopenharmony_ci<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>
16391cb0ef41Sopenharmony_ci<div class="api_metadata">
16401cb0ef41Sopenharmony_ci<span>Added in: v0.7.10</span>
16411cb0ef41Sopenharmony_ci</div>
16421cb0ef41Sopenharmony_ci<p>Calling <code>subprocess.ref()</code> after making a call to <code>subprocess.unref()</code> will
16431cb0ef41Sopenharmony_cirestore the removed reference count for the child process, forcing the parent
16441cb0ef41Sopenharmony_cito wait for the child to exit before exiting itself.</p>
16451cb0ef41Sopenharmony_ci<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>);
16461cb0ef41Sopenharmony_ci
16471cb0ef41Sopenharmony_ci<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>], {
16481cb0ef41Sopenharmony_ci  <span class="hljs-attr">detached</span>: <span class="hljs-literal">true</span>,
16491cb0ef41Sopenharmony_ci  <span class="hljs-attr">stdio</span>: <span class="hljs-string">'ignore'</span>,
16501cb0ef41Sopenharmony_ci});
16511cb0ef41Sopenharmony_ci
16521cb0ef41Sopenharmony_cisubprocess.<span class="hljs-title function_">unref</span>();
16531cb0ef41Sopenharmony_cisubprocess.<span class="hljs-title function_">ref</span>();</code> <button class="copy-button">copy</button></pre>
16541cb0ef41Sopenharmony_ci<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>
16551cb0ef41Sopenharmony_ci<div class="api_metadata">
16561cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
16571cb0ef41Sopenharmony_ci<table>
16581cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
16591cb0ef41Sopenharmony_ci<tr><td>v5.8.0</td>
16601cb0ef41Sopenharmony_ci<td><p>The <code>options</code> parameter, and the <code>keepOpen</code> option in particular, is supported now.</p></td></tr>
16611cb0ef41Sopenharmony_ci<tr><td>v5.0.0</td>
16621cb0ef41Sopenharmony_ci<td><p>This method returns a boolean for flow control now.</p></td></tr>
16631cb0ef41Sopenharmony_ci<tr><td>v4.0.0</td>
16641cb0ef41Sopenharmony_ci<td><p>The <code>callback</code> parameter is supported now.</p></td></tr>
16651cb0ef41Sopenharmony_ci<tr><td>v0.5.9</td>
16661cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.9</span></p></td></tr>
16671cb0ef41Sopenharmony_ci</tbody></table>
16681cb0ef41Sopenharmony_ci</details>
16691cb0ef41Sopenharmony_ci</div>
16701cb0ef41Sopenharmony_ci<ul>
16711cb0ef41Sopenharmony_ci<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
16721cb0ef41Sopenharmony_ci<li><code>sendHandle</code> <a href="net.html#serverlistenhandle-backlog-callback" class="type">&#x3C;Handle></a></li>
16731cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> The <code>options</code> argument, if present, is an object used to
16741cb0ef41Sopenharmony_ciparameterize the sending of certain types of handles. <code>options</code> supports
16751cb0ef41Sopenharmony_cithe following properties:
16761cb0ef41Sopenharmony_ci<ul>
16771cb0ef41Sopenharmony_ci<li><code>keepOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> A value that can be used when passing instances of
16781cb0ef41Sopenharmony_ci<code>net.Socket</code>. When <code>true</code>, the socket is kept open in the sending process.
16791cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>false</code>.</li>
16801cb0ef41Sopenharmony_ci</ul>
16811cb0ef41Sopenharmony_ci</li>
16821cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
16831cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
16841cb0ef41Sopenharmony_ci</ul>
16851cb0ef41Sopenharmony_ci<p>When an IPC channel has been established between the parent and child (
16861cb0ef41Sopenharmony_cii.e. when using <a href="#child_processforkmodulepath-args-options"><code>child_process.fork()</code></a>), the <code>subprocess.send()</code> method can
16871cb0ef41Sopenharmony_cibe used to send messages to the child process. When the child process is a
16881cb0ef41Sopenharmony_ciNode.js instance, these messages can be received via the <a href="process.html#event-message"><code>'message'</code></a> event.</p>
16891cb0ef41Sopenharmony_ci<p>The message goes through serialization and parsing. The resulting
16901cb0ef41Sopenharmony_cimessage might not be the same as what is originally sent.</p>
16911cb0ef41Sopenharmony_ci<p>For example, in the parent script:</p>
16921cb0ef41Sopenharmony_ci<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>);
16931cb0ef41Sopenharmony_ci<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>);
16941cb0ef41Sopenharmony_ci
16951cb0ef41Sopenharmony_cin.<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function">(<span class="hljs-params">m</span>) =></span> {
16961cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'PARENT got message:'</span>, m);
16971cb0ef41Sopenharmony_ci});
16981cb0ef41Sopenharmony_ci
16991cb0ef41Sopenharmony_ci<span class="hljs-comment">// Causes the child to print: CHILD got message: { hello: 'world' }</span>
17001cb0ef41Sopenharmony_cin.<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>
17011cb0ef41Sopenharmony_ci<p>And then the child script, <code>'sub.js'</code> might look like this:</p>
17021cb0ef41Sopenharmony_ci<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> {
17031cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'CHILD got message:'</span>, m);
17041cb0ef41Sopenharmony_ci});
17051cb0ef41Sopenharmony_ci
17061cb0ef41Sopenharmony_ci<span class="hljs-comment">// Causes the parent to print: PARENT got message: { foo: 'bar', baz: null }</span>
17071cb0ef41Sopenharmony_ciprocess.<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>
17081cb0ef41Sopenharmony_ci<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
17091cb0ef41Sopenharmony_cithat allows the child to send messages back to the parent.</p>
17101cb0ef41Sopenharmony_ci<p>There is a special case when sending a <code>{cmd: 'NODE_foo'}</code> message. Messages
17111cb0ef41Sopenharmony_cicontaining a <code>NODE_</code> prefix in the <code>cmd</code> property are reserved for use within
17121cb0ef41Sopenharmony_ciNode.js core and will not be emitted in the child's <a href="process.html#event-message"><code>'message'</code></a>
17131cb0ef41Sopenharmony_cievent. Rather, such messages are emitted using the
17141cb0ef41Sopenharmony_ci<code>'internalMessage'</code> event and are consumed internally by Node.js.
17151cb0ef41Sopenharmony_ciApplications should avoid using such messages or listening for
17161cb0ef41Sopenharmony_ci<code>'internalMessage'</code> events as it is subject to change without notice.</p>
17171cb0ef41Sopenharmony_ci<p>The optional <code>sendHandle</code> argument that may be passed to <code>subprocess.send()</code> is
17181cb0ef41Sopenharmony_cifor passing a TCP server or socket object to the child process. The child will
17191cb0ef41Sopenharmony_cireceive the object as the second argument passed to the callback function
17201cb0ef41Sopenharmony_ciregistered on the <a href="process.html#event-message"><code>'message'</code></a> event. Any data that is received
17211cb0ef41Sopenharmony_ciand buffered in the socket will not be sent to the child.</p>
17221cb0ef41Sopenharmony_ci<p>The optional <code>callback</code> is a function that is invoked after the message is
17231cb0ef41Sopenharmony_cisent but before the child may have received it. The function is called with a
17241cb0ef41Sopenharmony_cisingle argument: <code>null</code> on success, or an <a href="errors.html#class-error"><code>Error</code></a> object on failure.</p>
17251cb0ef41Sopenharmony_ci<p>If no <code>callback</code> function is provided and the message cannot be sent, an
17261cb0ef41Sopenharmony_ci<code>'error'</code> event will be emitted by the <a href="#class-childprocess"><code>ChildProcess</code></a> object. This can
17271cb0ef41Sopenharmony_cihappen, for instance, when the child process has already exited.</p>
17281cb0ef41Sopenharmony_ci<p><code>subprocess.send()</code> will return <code>false</code> if the channel has closed or when the
17291cb0ef41Sopenharmony_cibacklog of unsent messages exceeds a threshold that makes it unwise to send
17301cb0ef41Sopenharmony_cimore. Otherwise, the method returns <code>true</code>. The <code>callback</code> function can be
17311cb0ef41Sopenharmony_ciused to implement flow control.</p>
17321cb0ef41Sopenharmony_ci<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>
17331cb0ef41Sopenharmony_ci<p>The <code>sendHandle</code> argument can be used, for instance, to pass the handle of
17341cb0ef41Sopenharmony_cia TCP server object to the child process as illustrated in the example below:</p>
17351cb0ef41Sopenharmony_ci<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>);
17361cb0ef41Sopenharmony_ci
17371cb0ef41Sopenharmony_ci<span class="hljs-comment">// Open up the server object and send the handle.</span>
17381cb0ef41Sopenharmony_ci<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>();
17391cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">on</span>(<span class="hljs-string">'connection'</span>, <span class="hljs-function">(<span class="hljs-params">socket</span>) =></span> {
17401cb0ef41Sopenharmony_ci  socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'handled by parent'</span>);
17411cb0ef41Sopenharmony_ci});
17421cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>, <span class="hljs-function">() =></span> {
17431cb0ef41Sopenharmony_ci  subprocess.<span class="hljs-title function_">send</span>(<span class="hljs-string">'server'</span>, server);
17441cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
17451cb0ef41Sopenharmony_ci<p>The child would then receive the server object as:</p>
17461cb0ef41Sopenharmony_ci<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> {
17471cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (m === <span class="hljs-string">'server'</span>) {
17481cb0ef41Sopenharmony_ci    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> {
17491cb0ef41Sopenharmony_ci      socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'handled by child'</span>);
17501cb0ef41Sopenharmony_ci    });
17511cb0ef41Sopenharmony_ci  }
17521cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
17531cb0ef41Sopenharmony_ci<p>Once the server is now shared between the parent and child, some connections
17541cb0ef41Sopenharmony_cican be handled by the parent and some by the child.</p>
17551cb0ef41Sopenharmony_ci<p>While the example above uses a server created using the <code>node:net</code> module,
17561cb0ef41Sopenharmony_ci<code>node:dgram</code> module servers use exactly the same workflow with the exceptions of
17571cb0ef41Sopenharmony_cilistening on a <code>'message'</code> event instead of <code>'connection'</code> and using
17581cb0ef41Sopenharmony_ci<code>server.bind()</code> instead of <code>server.listen()</code>. This is, however, only
17591cb0ef41Sopenharmony_cisupported on Unix platforms.</p>
17601cb0ef41Sopenharmony_ci<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>
17611cb0ef41Sopenharmony_ci<p>Similarly, the <code>sendHandler</code> argument can be used to pass the handle of a
17621cb0ef41Sopenharmony_cisocket to the child process. The example below spawns two children that each
17631cb0ef41Sopenharmony_cihandle connections with "normal" or "special" priority:</p>
17641cb0ef41Sopenharmony_ci<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>);
17651cb0ef41Sopenharmony_ci<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>]);
17661cb0ef41Sopenharmony_ci<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>]);
17671cb0ef41Sopenharmony_ci
17681cb0ef41Sopenharmony_ci<span class="hljs-comment">// Open up the server and send sockets to child. Use pauseOnConnect to prevent</span>
17691cb0ef41Sopenharmony_ci<span class="hljs-comment">// the sockets from being read before they are sent to the child process.</span>
17701cb0ef41Sopenharmony_ci<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> });
17711cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">on</span>(<span class="hljs-string">'connection'</span>, <span class="hljs-function">(<span class="hljs-params">socket</span>) =></span> {
17721cb0ef41Sopenharmony_ci
17731cb0ef41Sopenharmony_ci  <span class="hljs-comment">// If this is special priority...</span>
17741cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (socket.<span class="hljs-property">remoteAddress</span> === <span class="hljs-string">'74.125.127.100'</span>) {
17751cb0ef41Sopenharmony_ci    special.<span class="hljs-title function_">send</span>(<span class="hljs-string">'socket'</span>, socket);
17761cb0ef41Sopenharmony_ci    <span class="hljs-keyword">return</span>;
17771cb0ef41Sopenharmony_ci  }
17781cb0ef41Sopenharmony_ci  <span class="hljs-comment">// This is normal priority.</span>
17791cb0ef41Sopenharmony_ci  normal.<span class="hljs-title function_">send</span>(<span class="hljs-string">'socket'</span>, socket);
17801cb0ef41Sopenharmony_ci});
17811cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>);</code> <button class="copy-button">copy</button></pre>
17821cb0ef41Sopenharmony_ci<p>The <code>subprocess.js</code> would receive the socket handle as the second argument
17831cb0ef41Sopenharmony_cipassed to the event callback function:</p>
17841cb0ef41Sopenharmony_ci<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> {
17851cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (m === <span class="hljs-string">'socket'</span>) {
17861cb0ef41Sopenharmony_ci    <span class="hljs-keyword">if</span> (socket) {
17871cb0ef41Sopenharmony_ci      <span class="hljs-comment">// Check that the client socket exists.</span>
17881cb0ef41Sopenharmony_ci      <span class="hljs-comment">// It is possible for the socket to be closed between the time it is</span>
17891cb0ef41Sopenharmony_ci      <span class="hljs-comment">// sent and the time it is received in the child process.</span>
17901cb0ef41Sopenharmony_ci      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>);
17911cb0ef41Sopenharmony_ci    }
17921cb0ef41Sopenharmony_ci  }
17931cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
17941cb0ef41Sopenharmony_ci<p>Do not use <code>.maxConnections</code> on a socket that has been passed to a subprocess.
17951cb0ef41Sopenharmony_ciThe parent cannot track when the socket is destroyed.</p>
17961cb0ef41Sopenharmony_ci<p>Any <code>'message'</code> handlers in the subprocess should verify that <code>socket</code> exists,
17971cb0ef41Sopenharmony_cias the connection may have been closed during the time it takes to send the
17981cb0ef41Sopenharmony_ciconnection to the child.</p>
17991cb0ef41Sopenharmony_ci<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>
18001cb0ef41Sopenharmony_ci<ul>
18011cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a></li>
18021cb0ef41Sopenharmony_ci</ul>
18031cb0ef41Sopenharmony_ci<p>The <code>subprocess.signalCode</code> property indicates the signal received by
18041cb0ef41Sopenharmony_cithe child process if any, else <code>null</code>.</p>
18051cb0ef41Sopenharmony_ci<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>
18061cb0ef41Sopenharmony_ci<ul>
18071cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&#x3C;Array></a></li>
18081cb0ef41Sopenharmony_ci</ul>
18091cb0ef41Sopenharmony_ci<p>The <code>subprocess.spawnargs</code> property represents the full list of command-line
18101cb0ef41Sopenharmony_ciarguments the child process was launched with.</p>
18111cb0ef41Sopenharmony_ci<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>
18121cb0ef41Sopenharmony_ci<ul>
18131cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
18141cb0ef41Sopenharmony_ci</ul>
18151cb0ef41Sopenharmony_ci<p>The <code>subprocess.spawnfile</code> property indicates the executable file name of
18161cb0ef41Sopenharmony_cithe child process that is launched.</p>
18171cb0ef41Sopenharmony_ci<p>For <a href="#child_processforkmodulepath-args-options"><code>child_process.fork()</code></a>, its value will be equal to
18181cb0ef41Sopenharmony_ci<a href="process.html#processexecpath"><code>process.execPath</code></a>.
18191cb0ef41Sopenharmony_ciFor <a href="#child_processspawncommand-args-options"><code>child_process.spawn()</code></a>, its value will be the name of
18201cb0ef41Sopenharmony_cithe executable file.
18211cb0ef41Sopenharmony_ciFor <a href="#child_processexeccommand-options-callback"><code>child_process.exec()</code></a>,  its value will be the name of the shell
18221cb0ef41Sopenharmony_ciin which the child process is launched.</p>
18231cb0ef41Sopenharmony_ci<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>
18241cb0ef41Sopenharmony_ci<div class="api_metadata">
18251cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span>
18261cb0ef41Sopenharmony_ci</div>
18271cb0ef41Sopenharmony_ci<ul>
18281cb0ef41Sopenharmony_ci<li><a href="stream.html#class-streamreadable" class="type">&#x3C;stream.Readable></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a></li>
18291cb0ef41Sopenharmony_ci</ul>
18301cb0ef41Sopenharmony_ci<p>A <code>Readable Stream</code> that represents the child process's <code>stderr</code>.</p>
18311cb0ef41Sopenharmony_ci<p>If the child was spawned with <code>stdio[2]</code> set to anything other than <code>'pipe'</code>,
18321cb0ef41Sopenharmony_cithen this will be <code>null</code>.</p>
18331cb0ef41Sopenharmony_ci<p><code>subprocess.stderr</code> is an alias for <code>subprocess.stdio[2]</code>. Both properties will
18341cb0ef41Sopenharmony_cirefer to the same value.</p>
18351cb0ef41Sopenharmony_ci<p>The <code>subprocess.stderr</code> property can be <code>null</code> or <code>undefined</code>
18361cb0ef41Sopenharmony_ciif the child process could not be successfully spawned.</p>
18371cb0ef41Sopenharmony_ci<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>
18381cb0ef41Sopenharmony_ci<div class="api_metadata">
18391cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span>
18401cb0ef41Sopenharmony_ci</div>
18411cb0ef41Sopenharmony_ci<ul>
18421cb0ef41Sopenharmony_ci<li><a href="stream.html#class-streamwritable" class="type">&#x3C;stream.Writable></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a></li>
18431cb0ef41Sopenharmony_ci</ul>
18441cb0ef41Sopenharmony_ci<p>A <code>Writable Stream</code> that represents the child process's <code>stdin</code>.</p>
18451cb0ef41Sopenharmony_ci<p>If a child process waits to read all of its input, the child will not continue
18461cb0ef41Sopenharmony_ciuntil this stream has been closed via <code>end()</code>.</p>
18471cb0ef41Sopenharmony_ci<p>If the child was spawned with <code>stdio[0]</code> set to anything other than <code>'pipe'</code>,
18481cb0ef41Sopenharmony_cithen this will be <code>null</code>.</p>
18491cb0ef41Sopenharmony_ci<p><code>subprocess.stdin</code> is an alias for <code>subprocess.stdio[0]</code>. Both properties will
18501cb0ef41Sopenharmony_cirefer to the same value.</p>
18511cb0ef41Sopenharmony_ci<p>The <code>subprocess.stdin</code> property can be <code>null</code> or <code>undefined</code>
18521cb0ef41Sopenharmony_ciif the child process could not be successfully spawned.</p>
18531cb0ef41Sopenharmony_ci<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>
18541cb0ef41Sopenharmony_ci<div class="api_metadata">
18551cb0ef41Sopenharmony_ci<span>Added in: v0.7.10</span>
18561cb0ef41Sopenharmony_ci</div>
18571cb0ef41Sopenharmony_ci<ul>
18581cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&#x3C;Array></a></li>
18591cb0ef41Sopenharmony_ci</ul>
18601cb0ef41Sopenharmony_ci<p>A sparse array of pipes to the child process, corresponding with positions in
18611cb0ef41Sopenharmony_cithe <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
18621cb0ef41Sopenharmony_cito the value <code>'pipe'</code>. <code>subprocess.stdio[0]</code>, <code>subprocess.stdio[1]</code>, and
18631cb0ef41Sopenharmony_ci<code>subprocess.stdio[2]</code> are also available as <code>subprocess.stdin</code>,
18641cb0ef41Sopenharmony_ci<code>subprocess.stdout</code>, and <code>subprocess.stderr</code>, respectively.</p>
18651cb0ef41Sopenharmony_ci<p>In the following example, only the child's fd <code>1</code> (stdout) is configured as a
18661cb0ef41Sopenharmony_cipipe, so only the parent's <code>subprocess.stdio[1]</code> is a stream, all other values
18671cb0ef41Sopenharmony_ciin the array are <code>null</code>.</p>
18681cb0ef41Sopenharmony_ci<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>);
18691cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
18701cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> child_process = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:child_process'</span>);
18711cb0ef41Sopenharmony_ci
18721cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> subprocess = child_process.<span class="hljs-title function_">spawn</span>(<span class="hljs-string">'ls'</span>, {
18731cb0ef41Sopenharmony_ci  <span class="hljs-attr">stdio</span>: [
18741cb0ef41Sopenharmony_ci    <span class="hljs-number">0</span>, <span class="hljs-comment">// Use parent's stdin for child.</span>
18751cb0ef41Sopenharmony_ci    <span class="hljs-string">'pipe'</span>, <span class="hljs-comment">// Pipe child's stdout to parent.</span>
18761cb0ef41Sopenharmony_ci    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>
18771cb0ef41Sopenharmony_ci  ],
18781cb0ef41Sopenharmony_ci});
18791cb0ef41Sopenharmony_ci
18801cb0ef41Sopenharmony_ciassert.<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>);
18811cb0ef41Sopenharmony_ciassert.<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>);
18821cb0ef41Sopenharmony_ci
18831cb0ef41Sopenharmony_ci<span class="hljs-title function_">assert</span>(subprocess.<span class="hljs-property">stdout</span>);
18841cb0ef41Sopenharmony_ciassert.<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>);
18851cb0ef41Sopenharmony_ci
18861cb0ef41Sopenharmony_ciassert.<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>);
18871cb0ef41Sopenharmony_ciassert.<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>
18881cb0ef41Sopenharmony_ci<p>The <code>subprocess.stdio</code> property can be <code>undefined</code> if the child process could
18891cb0ef41Sopenharmony_cinot be successfully spawned.</p>
18901cb0ef41Sopenharmony_ci<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>
18911cb0ef41Sopenharmony_ci<div class="api_metadata">
18921cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span>
18931cb0ef41Sopenharmony_ci</div>
18941cb0ef41Sopenharmony_ci<ul>
18951cb0ef41Sopenharmony_ci<li><a href="stream.html#class-streamreadable" class="type">&#x3C;stream.Readable></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a></li>
18961cb0ef41Sopenharmony_ci</ul>
18971cb0ef41Sopenharmony_ci<p>A <code>Readable Stream</code> that represents the child process's <code>stdout</code>.</p>
18981cb0ef41Sopenharmony_ci<p>If the child was spawned with <code>stdio[1]</code> set to anything other than <code>'pipe'</code>,
18991cb0ef41Sopenharmony_cithen this will be <code>null</code>.</p>
19001cb0ef41Sopenharmony_ci<p><code>subprocess.stdout</code> is an alias for <code>subprocess.stdio[1]</code>. Both properties will
19011cb0ef41Sopenharmony_cirefer to the same value.</p>
19021cb0ef41Sopenharmony_ci<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>);
19031cb0ef41Sopenharmony_ci
19041cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span>(<span class="hljs-string">'ls'</span>);
19051cb0ef41Sopenharmony_ci
19061cb0ef41Sopenharmony_cisubprocess.<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> {
19071cb0ef41Sopenharmony_ci  <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>);
19081cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
19091cb0ef41Sopenharmony_ci<p>The <code>subprocess.stdout</code> property can be <code>null</code> or <code>undefined</code>
19101cb0ef41Sopenharmony_ciif the child process could not be successfully spawned.</p>
19111cb0ef41Sopenharmony_ci<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>
19121cb0ef41Sopenharmony_ci<div class="api_metadata">
19131cb0ef41Sopenharmony_ci<span>Added in: v0.7.10</span>
19141cb0ef41Sopenharmony_ci</div>
19151cb0ef41Sopenharmony_ci<p>By default, the parent will wait for the detached child to exit. To prevent the
19161cb0ef41Sopenharmony_ciparent from waiting for a given <code>subprocess</code> to exit, use the
19171cb0ef41Sopenharmony_ci<code>subprocess.unref()</code> method. Doing so will cause the parent's event loop to not
19181cb0ef41Sopenharmony_ciinclude the child in its reference count, allowing the parent to exit
19191cb0ef41Sopenharmony_ciindependently of the child, unless there is an established IPC channel between
19201cb0ef41Sopenharmony_cithe child and the parent.</p>
19211cb0ef41Sopenharmony_ci<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>);
19221cb0ef41Sopenharmony_ci
19231cb0ef41Sopenharmony_ci<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>], {
19241cb0ef41Sopenharmony_ci  <span class="hljs-attr">detached</span>: <span class="hljs-literal">true</span>,
19251cb0ef41Sopenharmony_ci  <span class="hljs-attr">stdio</span>: <span class="hljs-string">'ignore'</span>,
19261cb0ef41Sopenharmony_ci});
19271cb0ef41Sopenharmony_ci
19281cb0ef41Sopenharmony_cisubprocess.<span class="hljs-title function_">unref</span>();</code> <button class="copy-button">copy</button></pre>
19291cb0ef41Sopenharmony_ci</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>
19301cb0ef41Sopenharmony_ci<p>The <code>maxBuffer</code> option specifies the largest number of bytes allowed on <code>stdout</code>
19311cb0ef41Sopenharmony_cior <code>stderr</code>. If this value is exceeded, then the child process is terminated.
19321cb0ef41Sopenharmony_ciThis impacts output that includes multibyte character encodings such as UTF-8 or
19331cb0ef41Sopenharmony_ciUTF-16. For instance, <code>console.log('中文测试')</code> will send 13 UTF-8 encoded bytes
19341cb0ef41Sopenharmony_cito <code>stdout</code> although there are only 4 characters.</p>
19351cb0ef41Sopenharmony_ci</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>
19361cb0ef41Sopenharmony_ci<p>The shell should understand the <code>-c</code> switch. If the shell is <code>'cmd.exe'</code>, it
19371cb0ef41Sopenharmony_cishould understand the <code>/d /s /c</code> switches and command-line parsing should be
19381cb0ef41Sopenharmony_cicompatible.</p>
19391cb0ef41Sopenharmony_ci</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>
19401cb0ef41Sopenharmony_ci<p>Although Microsoft specifies <code>%COMSPEC%</code> must contain the path to
19411cb0ef41Sopenharmony_ci<code>'cmd.exe'</code> in the root environment, child processes are not always subject to
19421cb0ef41Sopenharmony_cithe same requirement. Thus, in <code>child_process</code> functions where a shell can be
19431cb0ef41Sopenharmony_cispawned, <code>'cmd.exe'</code> is used as a fallback if <code>process.env.ComSpec</code> is
19441cb0ef41Sopenharmony_ciunavailable.</p>
19451cb0ef41Sopenharmony_ci</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>
19461cb0ef41Sopenharmony_ci<div class="api_metadata">
19471cb0ef41Sopenharmony_ci<span>Added in: v13.2.0, v12.16.0</span>
19481cb0ef41Sopenharmony_ci</div>
19491cb0ef41Sopenharmony_ci<p>Child processes support a serialization mechanism for IPC that is based on the
19501cb0ef41Sopenharmony_ci<a href="v8.html#serialization-api">serialization API of the <code>node:v8</code> module</a>, based on the
19511cb0ef41Sopenharmony_ci<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
19521cb0ef41Sopenharmony_cisupports more built-in JavaScript object types, such as <code>BigInt</code>, <code>Map</code>
19531cb0ef41Sopenharmony_ciand <code>Set</code>, <code>ArrayBuffer</code> and <code>TypedArray</code>, <code>Buffer</code>, <code>Error</code>, <code>RegExp</code> etc.</p>
19541cb0ef41Sopenharmony_ci<p>However, this format is not a full superset of JSON, and e.g. properties set on
19551cb0ef41Sopenharmony_ciobjects of such built-in types will not be passed on through the serialization
19561cb0ef41Sopenharmony_cistep. Additionally, performance may not be equivalent to that of JSON, depending
19571cb0ef41Sopenharmony_cion the structure of the passed data.
19581cb0ef41Sopenharmony_ciTherefore, this feature requires opting in by setting the
19591cb0ef41Sopenharmony_ci<code>serialization</code> option to <code>'advanced'</code> when calling <a href="#child_processspawncommand-args-options"><code>child_process.spawn()</code></a>
19601cb0ef41Sopenharmony_cior <a href="#child_processforkmodulepath-args-options"><code>child_process.fork()</code></a>.</p></section>
19611cb0ef41Sopenharmony_ci        <!-- API END -->
19621cb0ef41Sopenharmony_ci      </div>
19631cb0ef41Sopenharmony_ci    </div>
19641cb0ef41Sopenharmony_ci  </div>
19651cb0ef41Sopenharmony_ci</body>
19661cb0ef41Sopenharmony_ci</html>
1967