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