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.20.1"> 7 <title>Process | Node.js v14.20.1 Documentation</title> 8 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 9 <link rel="stylesheet" href="assets/style.css"> 10 <link rel="stylesheet" href="assets/hljs.css"> 11 <link rel="canonical" href="https://nodejs.org/api/process.html"> 12</head> 13<body class="alt apidoc" id="api-section-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">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 active">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="process" class="interior"> 90 <header> 91 <div class="header-container"> 92 <h1>Node.js v14.20.1 documentation</h1> 93 <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 94 <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 95 <path fill="none" d="M0 0h24v24H0z" /> 96 <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/> 97 <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/> 98 </svg> 99 <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 100 <path d="M0 0h24v24H0z" fill="none" /> 101 <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/> 102 </svg> 103 </button> 104 </div> 105 <div id="gtoc"> 106 <ul> 107 <li> 108 <a href="index.html">Index</a> 109 </li> 110 <li> 111 <a href="all.html">View on single page</a> 112 </li> 113 <li> 114 <a href="process.json">View as JSON</a> 115 </li> 116 117 <li class="version-picker"> 118 <a href="#">View another version <span>▼</span></a> 119 <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v18.x/api/process.html">18.x</a></li> 120<li><a href="https://nodejs.org/docs/latest-v17.x/api/process.html">17.x</a></li> 121<li><a href="https://nodejs.org/docs/latest-v16.x/api/process.html">16.x <b>LTS</b></a></li> 122<li><a href="https://nodejs.org/docs/latest-v15.x/api/process.html">15.x</a></li> 123<li><a href="https://nodejs.org/docs/latest-v14.x/api/process.html">14.x <b>LTS</b></a></li> 124<li><a href="https://nodejs.org/docs/latest-v13.x/api/process.html">13.x</a></li> 125<li><a href="https://nodejs.org/docs/latest-v12.x/api/process.html">12.x <b>LTS</b></a></li> 126<li><a href="https://nodejs.org/docs/latest-v11.x/api/process.html">11.x</a></li> 127<li><a href="https://nodejs.org/docs/latest-v10.x/api/process.html">10.x</a></li> 128<li><a href="https://nodejs.org/docs/latest-v9.x/api/process.html">9.x</a></li> 129<li><a href="https://nodejs.org/docs/latest-v8.x/api/process.html">8.x</a></li> 130<li><a href="https://nodejs.org/docs/latest-v7.x/api/process.html">7.x</a></li> 131<li><a href="https://nodejs.org/docs/latest-v6.x/api/process.html">6.x</a></li> 132<li><a href="https://nodejs.org/docs/latest-v5.x/api/process.html">5.x</a></li> 133<li><a href="https://nodejs.org/docs/latest-v4.x/api/process.html">4.x</a></li> 134<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/process.html">0.12.x</a></li> 135<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/process.html">0.10.x</a></li></ol> 136 </li> 137 138 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/process.md">Edit on GitHub</a></li> 139 </ul> 140 </div> 141 <hr> 142 </header> 143 144 <details id="toc" open><summary>Table of contents</summary><ul> 145<li><a href="#process_process">Process</a> 146<ul> 147<li><a href="#process_process_events">Process events</a> 148<ul> 149<li><a href="#process_event_beforeexit">Event: <code>'beforeExit'</code></a></li> 150<li><a href="#process_event_disconnect">Event: <code>'disconnect'</code></a></li> 151<li><a href="#process_event_exit">Event: <code>'exit'</code></a></li> 152<li><a href="#process_event_message">Event: <code>'message'</code></a></li> 153<li><a href="#process_event_multipleresolves">Event: <code>'multipleResolves'</code></a></li> 154<li><a href="#process_event_rejectionhandled">Event: <code>'rejectionHandled'</code></a></li> 155<li><a href="#process_event_uncaughtexception">Event: <code>'uncaughtException'</code></a> 156<ul> 157<li><a href="#process_warning_using_uncaughtexception_correctly">Warning: Using <code>'uncaughtException'</code> correctly</a></li> 158</ul> 159</li> 160<li><a href="#process_event_uncaughtexceptionmonitor">Event: <code>'uncaughtExceptionMonitor'</code></a></li> 161<li><a href="#process_event_unhandledrejection">Event: <code>'unhandledRejection'</code></a></li> 162<li><a href="#process_event_warning">Event: <code>'warning'</code></a></li> 163<li><a href="#process_event_worker">Event: <code>'worker'</code></a> 164<ul> 165<li><a href="#process_emitting_custom_warnings">Emitting custom warnings</a></li> 166<li><a href="#process_node_js_warning_names">Node.js warning names</a></li> 167</ul> 168</li> 169<li><a href="#process_signal_events">Signal events</a></li> 170</ul> 171</li> 172<li><a href="#process_process_abort"><code>process.abort()</code></a></li> 173<li><a href="#process_process_allowednodeenvironmentflags"><code>process.allowedNodeEnvironmentFlags</code></a></li> 174<li><a href="#process_process_arch"><code>process.arch</code></a></li> 175<li><a href="#process_process_argv"><code>process.argv</code></a></li> 176<li><a href="#process_process_argv0"><code>process.argv0</code></a></li> 177<li><a href="#process_process_channel"><code>process.channel</code></a> 178<ul> 179<li><a href="#process_process_channel_ref"><code>process.channel.ref()</code></a></li> 180<li><a href="#process_process_channel_unref"><code>process.channel.unref()</code></a></li> 181</ul> 182</li> 183<li><a href="#process_process_chdir_directory"><code>process.chdir(directory)</code></a></li> 184<li><a href="#process_process_config"><code>process.config</code></a></li> 185<li><a href="#process_process_connected"><code>process.connected</code></a></li> 186<li><a href="#process_process_cpuusage_previousvalue"><code>process.cpuUsage([previousValue])</code></a></li> 187<li><a href="#process_process_cwd"><code>process.cwd()</code></a></li> 188<li><a href="#process_process_debugport"><code>process.debugPort</code></a></li> 189<li><a href="#process_process_disconnect"><code>process.disconnect()</code></a></li> 190<li><a href="#process_process_dlopen_module_filename_flags"><code>process.dlopen(module, filename[, flags])</code></a></li> 191<li><a href="#process_process_emitwarning_warning_options"><code>process.emitWarning(warning[, options])</code></a></li> 192<li><a href="#process_process_emitwarning_warning_type_code_ctor"><code>process.emitWarning(warning[, type[, code]][, ctor])</code></a> 193<ul> 194<li><a href="#process_avoiding_duplicate_warnings">Avoiding duplicate warnings</a></li> 195</ul> 196</li> 197<li><a href="#process_process_env"><code>process.env</code></a></li> 198<li><a href="#process_process_execargv"><code>process.execArgv</code></a></li> 199<li><a href="#process_process_execpath"><code>process.execPath</code></a></li> 200<li><a href="#process_process_exit_code"><code>process.exit([code])</code></a></li> 201<li><a href="#process_process_exitcode"><code>process.exitCode</code></a></li> 202<li><a href="#process_process_getegid"><code>process.getegid()</code></a></li> 203<li><a href="#process_process_geteuid"><code>process.geteuid()</code></a></li> 204<li><a href="#process_process_getgid"><code>process.getgid()</code></a></li> 205<li><a href="#process_process_getgroups"><code>process.getgroups()</code></a></li> 206<li><a href="#process_process_getuid"><code>process.getuid()</code></a></li> 207<li><a href="#process_process_hasuncaughtexceptioncapturecallback"><code>process.hasUncaughtExceptionCaptureCallback()</code></a></li> 208<li><span class="stability_3"><a href="#process_process_hrtime_time"><code>process.hrtime([time])</code></a></span></li> 209<li><a href="#process_process_hrtime_bigint"><code>process.hrtime.bigint()</code></a></li> 210<li><a href="#process_process_initgroups_user_extragroup"><code>process.initgroups(user, extraGroup)</code></a></li> 211<li><a href="#process_process_kill_pid_signal"><code>process.kill(pid[, signal])</code></a></li> 212<li><span class="stability_0"><a href="#process_process_mainmodule"><code>process.mainModule</code></a></span></li> 213<li><a href="#process_process_memoryusage"><code>process.memoryUsage()</code></a></li> 214<li><a href="#process_process_memoryusage_rss"><code>process.memoryUsage.rss()</code></a></li> 215<li><a href="#process_process_nexttick_callback_args"><code>process.nextTick(callback[, ...args])</code></a> 216<ul> 217<li><a href="#process_when_to_use_queuemicrotask_vs_process_nexttick">When to use <code>queueMicrotask()</code> vs. <code>process.nextTick()</code></a></li> 218</ul> 219</li> 220<li><a href="#process_process_nodeprecation"><code>process.noDeprecation</code></a></li> 221<li><a href="#process_process_pid"><code>process.pid</code></a></li> 222<li><a href="#process_process_platform"><code>process.platform</code></a></li> 223<li><a href="#process_process_ppid"><code>process.ppid</code></a></li> 224<li><a href="#process_process_release"><code>process.release</code></a></li> 225<li><a href="#process_process_report"><code>process.report</code></a> 226<ul> 227<li><a href="#process_process_report_compact"><code>process.report.compact</code></a></li> 228<li><a href="#process_process_report_directory"><code>process.report.directory</code></a></li> 229<li><a href="#process_process_report_filename"><code>process.report.filename</code></a></li> 230<li><a href="#process_process_report_getreport_err"><code>process.report.getReport([err])</code></a></li> 231<li><a href="#process_process_report_reportonfatalerror"><code>process.report.reportOnFatalError</code></a></li> 232<li><a href="#process_process_report_reportonsignal"><code>process.report.reportOnSignal</code></a></li> 233<li><a href="#process_process_report_reportonuncaughtexception"><code>process.report.reportOnUncaughtException</code></a></li> 234<li><a href="#process_process_report_signal"><code>process.report.signal</code></a></li> 235<li><a href="#process_process_report_writereport_filename_err"><code>process.report.writeReport([filename][, err])</code></a></li> 236</ul> 237</li> 238<li><a href="#process_process_resourceusage"><code>process.resourceUsage()</code></a></li> 239<li><a href="#process_process_send_message_sendhandle_options_callback"><code>process.send(message[, sendHandle[, options]][, callback])</code></a></li> 240<li><a href="#process_process_setegid_id"><code>process.setegid(id)</code></a></li> 241<li><a href="#process_process_seteuid_id"><code>process.seteuid(id)</code></a></li> 242<li><a href="#process_process_setgid_id"><code>process.setgid(id)</code></a></li> 243<li><a href="#process_process_setgroups_groups"><code>process.setgroups(groups)</code></a></li> 244<li><a href="#process_process_setuid_id"><code>process.setuid(id)</code></a></li> 245<li><span class="stability_1"><a href="#process_process_setsourcemapsenabled_val"><code>process.setSourceMapsEnabled(val)</code></a></span></li> 246<li><a href="#process_process_setuncaughtexceptioncapturecallback_fn"><code>process.setUncaughtExceptionCaptureCallback(fn)</code></a></li> 247<li><a href="#process_process_stderr"><code>process.stderr</code></a> 248<ul> 249<li><a href="#process_process_stderr_fd"><code>process.stderr.fd</code></a></li> 250</ul> 251</li> 252<li><a href="#process_process_stdin"><code>process.stdin</code></a> 253<ul> 254<li><a href="#process_process_stdin_fd"><code>process.stdin.fd</code></a></li> 255</ul> 256</li> 257<li><a href="#process_process_stdout"><code>process.stdout</code></a> 258<ul> 259<li><a href="#process_process_stdout_fd"><code>process.stdout.fd</code></a></li> 260<li><a href="#process_a_note_on_process_i_o">A note on process I/O</a></li> 261</ul> 262</li> 263<li><a href="#process_process_throwdeprecation"><code>process.throwDeprecation</code></a></li> 264<li><a href="#process_process_title"><code>process.title</code></a></li> 265<li><a href="#process_process_tracedeprecation"><code>process.traceDeprecation</code></a></li> 266<li><span class="stability_0"><a href="#process_process_umask"><code>process.umask()</code></a></span></li> 267<li><a href="#process_process_umask_mask"><code>process.umask(mask)</code></a></li> 268<li><a href="#process_process_uptime"><code>process.uptime()</code></a></li> 269<li><a href="#process_process_version"><code>process.version</code></a></li> 270<li><a href="#process_process_versions"><code>process.versions</code></a></li> 271<li><a href="#process_exit_codes">Exit codes</a></li> 272</ul> 273</li> 274</ul></details> 275 276 <div id="apicontent"> 277 <h2>Process<span><a class="mark" href="#process_process" id="process_process">#</a></span></h2> 278 279 280<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v14.20.1/lib/process.js">lib/process.js</a></p> 281<p>The <code>process</code> object is a <code>global</code> that provides information about, and control 282over, the current Node.js process. As a global, it is always available to 283Node.js applications without using <code>require()</code>. It can also be explicitly 284accessed using <code>require()</code>:</p> 285<pre><code class="language-js"><span class="hljs-keyword">const</span> process = <span class="hljs-built_in">require</span>(<span class="hljs-string">'process'</span>);</code></pre> 286<section><h3>Process events<span><a class="mark" href="#process_process_events" id="process_process_events">#</a></span></h3> 287<p>The <code>process</code> object is an instance of <a href="events.html#events_class_eventemitter"><code>EventEmitter</code></a>.</p> 288<h4>Event: <code>'beforeExit'</code><span><a class="mark" href="#process_event_beforeexit" id="process_event_beforeexit">#</a></span></h4> 289<div class="api_metadata"> 290<span>Added in: v0.11.12</span> 291</div> 292<p>The <code>'beforeExit'</code> event is emitted when Node.js empties its event loop and has 293no additional work to schedule. Normally, the Node.js process will exit when 294there is no work scheduled, but a listener registered on the <code>'beforeExit'</code> 295event can make asynchronous calls, and thereby cause the Node.js process to 296continue.</p> 297<p>The listener callback function is invoked with the value of 298<a href="#process_process_exitcode"><code>process.exitCode</code></a> passed as the only argument.</p> 299<p>The <code>'beforeExit'</code> event is <em>not</em> emitted for conditions causing explicit 300termination, such as calling <a href="#process_process_exit_code"><code>process.exit()</code></a> or uncaught exceptions.</p> 301<p>The <code>'beforeExit'</code> should <em>not</em> be used as an alternative to the <code>'exit'</code> event 302unless the intention is to schedule additional work.</p> 303<pre><code class="language-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'beforeExit'</span>, <span class="hljs-function">(<span class="hljs-params">code</span>) =></span> { 304 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Process beforeExit event with code: '</span>, code); 305}); 306 307process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">(<span class="hljs-params">code</span>) =></span> { 308 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Process exit event with code: '</span>, code); 309}); 310 311<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'This message is displayed first.'</span>); 312 313<span class="hljs-comment">// Prints:</span> 314<span class="hljs-comment">// This message is displayed first.</span> 315<span class="hljs-comment">// Process beforeExit event with code: 0</span> 316<span class="hljs-comment">// Process exit event with code: 0</span></code></pre> 317<h4>Event: <code>'disconnect'</code><span><a class="mark" href="#process_event_disconnect" id="process_event_disconnect">#</a></span></h4> 318<div class="api_metadata"> 319<span>Added in: v0.7.7</span> 320</div> 321<p>If the Node.js process is spawned with an IPC channel (see the <a href="child_process.html">Child Process</a> 322and <a href="cluster.html">Cluster</a> documentation), the <code>'disconnect'</code> event will be emitted when 323the IPC channel is closed.</p> 324<h4>Event: <code>'exit'</code><span><a class="mark" href="#process_event_exit" id="process_event_exit">#</a></span></h4> 325<div class="api_metadata"> 326<span>Added in: v0.1.7</span> 327</div> 328<ul> 329<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 330</ul> 331<p>The <code>'exit'</code> event is emitted when the Node.js process is about to exit as a 332result of either:</p> 333<ul> 334<li>The <code>process.exit()</code> method being called explicitly;</li> 335<li>The Node.js event loop no longer having any additional work to perform.</li> 336</ul> 337<p>There is no way to prevent the exiting of the event loop at this point, and once 338all <code>'exit'</code> listeners have finished running the Node.js process will terminate.</p> 339<p>The listener callback function is invoked with the exit code specified either 340by the <a href="#process_process_exitcode"><code>process.exitCode</code></a> property, or the <code>exitCode</code> argument passed to the 341<a href="#process_process_exit_code"><code>process.exit()</code></a> method.</p> 342<pre><code class="language-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">(<span class="hljs-params">code</span>) =></span> { 343 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`About to exit with code: <span class="hljs-subst">${code}</span>`</span>); 344});</code></pre> 345<p>Listener functions <strong>must</strong> only perform <strong>synchronous</strong> operations. The Node.js 346process will exit immediately after calling the <code>'exit'</code> event listeners 347causing any additional work still queued in the event loop to be abandoned. 348In the following example, for instance, the timeout will never occur:</p> 349<pre><code class="language-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">(<span class="hljs-params">code</span>) =></span> { 350 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> { 351 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'This will not run'</span>); 352 }, <span class="hljs-number">0</span>); 353});</code></pre> 354<h4>Event: <code>'message'</code><span><a class="mark" href="#process_event_message" id="process_event_message">#</a></span></h4> 355<div class="api_metadata"> 356<span>Added in: v0.5.10</span> 357</div> 358<ul> 359<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> a parsed JSON object 360or a serializable primitive value.</li> 361<li><code>sendHandle</code> <a href="net.html#net_class_net_server" class="type"><net.Server></a> | <a href="net.html#net_class_net_socket" class="type"><net.Socket></a> a <a href="net.html#net_class_net_server"><code>net.Server</code></a> or <a href="net.html#net_class_net_socket"><code>net.Socket</code></a> 362object, or undefined.</li> 363</ul> 364<p>If the Node.js process is spawned with an IPC channel (see the <a href="child_process.html">Child Process</a> 365and <a href="cluster.html">Cluster</a> documentation), the <code>'message'</code> event is emitted whenever a 366message sent by a parent process using <a href="child_process.html#child_process_subprocess_send_message_sendhandle_options_callback"><code>childprocess.send()</code></a> is received by 367the child process.</p> 368<p>The message goes through serialization and parsing. The resulting message might 369not be the same as what is originally sent.</p> 370<p>If the <code>serialization</code> option was set to <code>advanced</code> used when spawning the 371process, the <code>message</code> argument can contain data that JSON is not able 372to represent. 373See <a href="child_process.html#child_process_advanced_serialization">Advanced serialization for <code>child_process</code></a> for more details.</p> 374<h4>Event: <code>'multipleResolves'</code><span><a class="mark" href="#process_event_multipleresolves" id="process_event_multipleresolves">#</a></span></h4> 375<div class="api_metadata"> 376<span>Added in: v10.12.0</span> 377</div> 378<ul> 379<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The resolution type. One of <code>'resolve'</code> or <code>'reject'</code>.</li> 380<li><code>promise</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> The promise that resolved or rejected more than once.</li> 381<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> The value with which the promise was either resolved or 382rejected after the original resolve.</li> 383</ul> 384<p>The <code>'multipleResolves'</code> event is emitted whenever a <code>Promise</code> has been either:</p> 385<ul> 386<li>Resolved more than once.</li> 387<li>Rejected more than once.</li> 388<li>Rejected after resolve.</li> 389<li>Resolved after reject.</li> 390</ul> 391<p>This is useful for tracking potential errors in an application while using the 392<code>Promise</code> constructor, as multiple resolutions are silently swallowed. However, 393the occurrence of this event does not necessarily indicate an error. For 394example, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race"><code>Promise.race()</code></a> can trigger a <code>'multipleResolves'</code> event.</p> 395<pre><code class="language-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'multipleResolves'</span>, <span class="hljs-function">(<span class="hljs-params">type, promise, reason</span>) =></span> { 396 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(type, promise, reason); 397 <span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> process.<span class="hljs-title function_">exit</span>(<span class="hljs-number">1</span>)); 398}); 399 400<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">main</span>(<span class="hljs-params"></span>) { 401 <span class="hljs-keyword">try</span> { 402 <span class="hljs-keyword">return</span> <span class="hljs-keyword">await</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve, reject</span>) =></span> { 403 <span class="hljs-title function_">resolve</span>(<span class="hljs-string">'First call'</span>); 404 <span class="hljs-title function_">resolve</span>(<span class="hljs-string">'Swallowed resolve'</span>); 405 <span class="hljs-title function_">reject</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Swallowed reject'</span>)); 406 }); 407 } <span class="hljs-keyword">catch</span> { 408 <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Failed'</span>); 409 } 410} 411 412<span class="hljs-title function_">main</span>().<span class="hljs-title function_">then</span>(<span class="hljs-variable language_">console</span>.<span class="hljs-property">log</span>); 413<span class="hljs-comment">// resolve: Promise { 'First call' } 'Swallowed resolve'</span> 414<span class="hljs-comment">// reject: Promise { 'First call' } Error: Swallowed reject</span> 415<span class="hljs-comment">// at Promise (*)</span> 416<span class="hljs-comment">// at new Promise (<anonymous>)</span> 417<span class="hljs-comment">// at main (*)</span> 418<span class="hljs-comment">// First call</span></code></pre> 419<h4>Event: <code>'rejectionHandled'</code><span><a class="mark" href="#process_event_rejectionhandled" id="process_event_rejectionhandled">#</a></span></h4> 420<div class="api_metadata"> 421<span>Added in: v1.4.1</span> 422</div> 423<ul> 424<li><code>promise</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> The late handled promise.</li> 425</ul> 426<p>The <code>'rejectionHandled'</code> event is emitted whenever a <code>Promise</code> has been rejected 427and an error handler was attached to it (using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch"><code>promise.catch()</code></a>, for 428example) later than one turn of the Node.js event loop.</p> 429<p>The <code>Promise</code> object would have previously been emitted in an 430<code>'unhandledRejection'</code> event, but during the course of processing gained a 431rejection handler.</p> 432<p>There is no notion of a top level for a <code>Promise</code> chain at which rejections can 433always be handled. Being inherently asynchronous in nature, a <code>Promise</code> 434rejection can be handled at a future point in time, possibly much later than 435the event loop turn it takes for the <code>'unhandledRejection'</code> event to be emitted.</p> 436<p>Another way of stating this is that, unlike in synchronous code where there is 437an ever-growing list of unhandled exceptions, with Promises there can be a 438growing-and-shrinking list of unhandled rejections.</p> 439<p>In synchronous code, the <code>'uncaughtException'</code> event is emitted when the list of 440unhandled exceptions grows.</p> 441<p>In asynchronous code, the <code>'unhandledRejection'</code> event is emitted when the list 442of unhandled rejections grows, and the <code>'rejectionHandled'</code> event is emitted 443when the list of unhandled rejections shrinks.</p> 444<pre><code class="language-js"><span class="hljs-keyword">const</span> unhandledRejections = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Map</span>(); 445process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'unhandledRejection'</span>, <span class="hljs-function">(<span class="hljs-params">reason, promise</span>) =></span> { 446 unhandledRejections.<span class="hljs-title function_">set</span>(promise, reason); 447}); 448process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'rejectionHandled'</span>, <span class="hljs-function">(<span class="hljs-params">promise</span>) =></span> { 449 unhandledRejections.<span class="hljs-title function_">delete</span>(promise); 450});</code></pre> 451<p>In this example, the <code>unhandledRejections</code> <code>Map</code> will grow and shrink over time, 452reflecting rejections that start unhandled and then become handled. It is 453possible to record such errors in an error log, either periodically (which is 454likely best for long-running application) or upon process exit (which is likely 455most convenient for scripts).</p> 456<h4>Event: <code>'uncaughtException'</code><span><a class="mark" href="#process_event_uncaughtexception" id="process_event_uncaughtexception">#</a></span></h4> 457<div class="api_metadata"> 458<details class="changelog"><summary>History</summary> 459<table> 460<tbody><tr><th>Version</th><th>Changes</th></tr> 461<tr><td>v12.0.0, v10.17.0</td> 462<td><p>Added the <code>origin</code> argument.</p></td></tr> 463<tr><td>v0.1.18</td> 464<td><p><span>Added in: v0.1.18</span></p></td></tr> 465</tbody></table> 466</details> 467</div> 468<ul> 469<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> The uncaught exception.</li> 470<li><code>origin</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Indicates if the exception originates from an unhandled 471rejection or from an synchronous error. Can either be <code>'uncaughtException'</code> or 472<code>'unhandledRejection'</code>. The latter is only used in conjunction with the 473<a href="cli.html#cli_unhandled_rejections_mode"><code>--unhandled-rejections</code></a> flag set to <code>strict</code> or <code>throw</code> and 474an unhandled rejection.</li> 475</ul> 476<p>The <code>'uncaughtException'</code> event is emitted when an uncaught JavaScript 477exception bubbles all the way back to the event loop. By default, Node.js 478handles such exceptions by printing the stack trace to <code>stderr</code> and exiting 479with code 1, overriding any previously set <a href="#process_process_exitcode"><code>process.exitCode</code></a>. 480Adding a handler for the <code>'uncaughtException'</code> event overrides this default 481behavior. Alternatively, change the <a href="#process_process_exitcode"><code>process.exitCode</code></a> in the 482<code>'uncaughtException'</code> handler which will result in the process exiting with the 483provided exit code. Otherwise, in the presence of such handler the process will 484exit with 0.</p> 485<pre><code class="language-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'uncaughtException'</span>, <span class="hljs-function">(<span class="hljs-params">err, origin</span>) =></span> { 486 fs.<span class="hljs-title function_">writeSync</span>( 487 process.<span class="hljs-property">stderr</span>.<span class="hljs-property">fd</span>, 488 <span class="hljs-string">`Caught exception: <span class="hljs-subst">${err}</span>\n`</span> + 489 <span class="hljs-string">`Exception origin: <span class="hljs-subst">${origin}</span>`</span> 490 ); 491}); 492 493<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> { 494 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'This will still run.'</span>); 495}, <span class="hljs-number">500</span>); 496 497<span class="hljs-comment">// Intentionally cause an exception, but don't catch it.</span> 498<span class="hljs-title function_">nonexistentFunc</span>(); 499<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'This will not run.'</span>);</code></pre> 500<p>It is possible to monitor <code>'uncaughtException'</code> events without overriding the 501default behavior to exit the process by installing a 502<code>'uncaughtExceptionMonitor'</code> listener.</p> 503<h5>Warning: Using <code>'uncaughtException'</code> correctly<span><a class="mark" href="#process_warning_using_uncaughtexception_correctly" id="process_warning_using_uncaughtexception_correctly">#</a></span></h5> 504<p><code>'uncaughtException'</code> is a crude mechanism for exception handling 505intended to be used only as a last resort. The event <em>should not</em> be used as 506an equivalent to <code>On Error Resume Next</code>. Unhandled exceptions inherently mean 507that an application is in an undefined state. Attempting to resume application 508code without properly recovering from the exception can cause additional 509unforeseen and unpredictable issues.</p> 510<p>Exceptions thrown from within the event handler will not be caught. Instead the 511process will exit with a non-zero exit code and the stack trace will be printed. 512This is to avoid infinite recursion.</p> 513<p>Attempting to resume normally after an uncaught exception can be similar to 514pulling out the power cord when upgrading a computer. Nine out of ten 515times, nothing happens. But the tenth time, the system becomes corrupted.</p> 516<p>The correct use of <code>'uncaughtException'</code> is to perform synchronous cleanup 517of allocated resources (e.g. file descriptors, handles, etc) before shutting 518down the process. <strong>It is not safe to resume normal operation after 519<code>'uncaughtException'</code>.</strong></p> 520<p>To restart a crashed application in a more reliable way, whether 521<code>'uncaughtException'</code> is emitted or not, an external monitor should be employed 522in a separate process to detect application failures and recover or restart as 523needed.</p> 524<h4>Event: <code>'uncaughtExceptionMonitor'</code><span><a class="mark" href="#process_event_uncaughtexceptionmonitor" id="process_event_uncaughtexceptionmonitor">#</a></span></h4> 525<div class="api_metadata"> 526<span>Added in: v13.7.0</span> 527</div> 528<ul> 529<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> The uncaught exception.</li> 530<li><code>origin</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Indicates if the exception originates from an unhandled 531rejection or from synchronous errors. Can either be <code>'uncaughtException'</code> or 532<code>'unhandledRejection'</code>. The latter is only used in conjunction with the 533<a href="cli.html#cli_unhandled_rejections_mode"><code>--unhandled-rejections</code></a> flag set to <code>strict</code> or <code>throw</code> and 534an unhandled rejection.</li> 535</ul> 536<p>The <code>'uncaughtExceptionMonitor'</code> event is emitted before an 537<code>'uncaughtException'</code> event is emitted or a hook installed via 538<a href="#process_process_setuncaughtexceptioncapturecallback_fn"><code>process.setUncaughtExceptionCaptureCallback()</code></a> is called.</p> 539<p>Installing an <code>'uncaughtExceptionMonitor'</code> listener does not change the behavior 540once an <code>'uncaughtException'</code> event is emitted. The process will 541still crash if no <code>'uncaughtException'</code> listener is installed.</p> 542<pre><code class="language-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'uncaughtExceptionMonitor'</span>, <span class="hljs-function">(<span class="hljs-params">err, origin</span>) =></span> { 543 <span class="hljs-title class_">MyMonitoringTool</span>.<span class="hljs-title function_">logSync</span>(err, origin); 544}); 545 546<span class="hljs-comment">// Intentionally cause an exception, but don't catch it.</span> 547<span class="hljs-title function_">nonexistentFunc</span>(); 548<span class="hljs-comment">// Still crashes Node.js</span></code></pre> 549<h4>Event: <code>'unhandledRejection'</code><span><a class="mark" href="#process_event_unhandledrejection" id="process_event_unhandledrejection">#</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>v7.0.0</td> 555<td><p>Not handling <code>Promise</code> rejections is deprecated.</p></td></tr> 556<tr><td>v6.6.0</td> 557<td><p>Unhandled <code>Promise</code> rejections will now emit a process warning.</p></td></tr> 558<tr><td>v1.4.1</td> 559<td><p><span>Added in: v1.4.1</span></p></td></tr> 560</tbody></table> 561</details> 562</div> 563<ul> 564<li><code>reason</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> The object with which the promise was rejected 565(typically an <a href="errors.html#errors_class_error"><code>Error</code></a> object).</li> 566<li><code>promise</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> The rejected promise.</li> 567</ul> 568<p>The <code>'unhandledRejection'</code> event is emitted whenever a <code>Promise</code> is rejected and 569no error handler is attached to the promise within a turn of the event loop. 570When programming with Promises, exceptions are encapsulated as "rejected 571promises". Rejections can be caught and handled using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch"><code>promise.catch()</code></a> and 572are propagated through a <code>Promise</code> chain. The <code>'unhandledRejection'</code> event is 573useful for detecting and keeping track of promises that were rejected whose 574rejections have not yet been handled.</p> 575<pre><code class="language-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'unhandledRejection'</span>, <span class="hljs-function">(<span class="hljs-params">reason, promise</span>) =></span> { 576 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Unhandled Rejection at:'</span>, promise, <span class="hljs-string">'reason:'</span>, reason); 577 <span class="hljs-comment">// Application specific logging, throwing an error, or other logic here</span> 578}); 579 580somePromise.<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-params">res</span>) =></span> { 581 <span class="hljs-keyword">return</span> <span class="hljs-title function_">reportToUser</span>(<span class="hljs-variable constant_">JSON</span>.<span class="hljs-title function_">pasre</span>(res)); <span class="hljs-comment">// Note the typo (`pasre`)</span> 582}); <span class="hljs-comment">// No `.catch()` or `.then()`</span></code></pre> 583<p>The following will also trigger the <code>'unhandledRejection'</code> event to be 584emitted:</p> 585<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">SomeResource</span>(<span class="hljs-params"></span>) { 586 <span class="hljs-comment">// Initially set the loaded status to a rejected promise</span> 587 <span class="hljs-variable language_">this</span>.<span class="hljs-property">loaded</span> = <span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">reject</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Resource not yet loaded!'</span>)); 588} 589 590<span class="hljs-keyword">const</span> resource = <span class="hljs-keyword">new</span> <span class="hljs-title class_">SomeResource</span>(); 591<span class="hljs-comment">// no .catch or .then on resource.loaded for at least a turn</span></code></pre> 592<p>In this example case, it is possible to track the rejection as a developer error 593as would typically be the case for other <code>'unhandledRejection'</code> events. To 594address such failures, a non-operational 595<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch"><code>.catch(() => { })</code></a> handler may be attached to 596<code>resource.loaded</code>, which would prevent the <code>'unhandledRejection'</code> event from 597being emitted.</p> 598<h4>Event: <code>'warning'</code><span><a class="mark" href="#process_event_warning" id="process_event_warning">#</a></span></h4> 599<div class="api_metadata"> 600<span>Added in: v6.0.0</span> 601</div> 602<ul> 603<li><code>warning</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> Key properties of the warning are: 604<ul> 605<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The name of the warning. <strong>Default:</strong> <code>'Warning'</code>.</li> 606<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A system-provided description of the warning.</li> 607<li><code>stack</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A stack trace to the location in the code where the warning 608was issued.</li> 609</ul> 610</li> 611</ul> 612<p>The <code>'warning'</code> event is emitted whenever Node.js emits a process warning.</p> 613<p>A process warning is similar to an error in that it describes exceptional 614conditions that are being brought to the user's attention. However, warnings 615are not part of the normal Node.js and JavaScript error handling flow. 616Node.js can emit warnings whenever it detects bad coding practices that could 617lead to sub-optimal application performance, bugs, or security vulnerabilities.</p> 618<pre><code class="language-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'warning'</span>, <span class="hljs-function">(<span class="hljs-params">warning</span>) =></span> { 619 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span class="hljs-property">name</span>); <span class="hljs-comment">// Print the warning name</span> 620 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span class="hljs-property">message</span>); <span class="hljs-comment">// Print the warning message</span> 621 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span class="hljs-property">stack</span>); <span class="hljs-comment">// Print the stack trace</span> 622});</code></pre> 623<p>By default, Node.js will print process warnings to <code>stderr</code>. The <code>--no-warnings</code> 624command-line option can be used to suppress the default console output but the 625<code>'warning'</code> event will still be emitted by the <code>process</code> object.</p> 626<p>The following example illustrates the warning that is printed to <code>stderr</code> when 627too many listeners have been added to an event:</p> 628<pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">node</span> 629<span class="hljs-meta">> </span><span class="language-bash">events.defaultMaxListeners = 1;</span> 630<span class="hljs-meta">> </span><span class="language-bash">process.on(<span class="hljs-string">'foo'</span>, () => {});</span> 631<span class="hljs-meta">> </span><span class="language-bash">process.on(<span class="hljs-string">'foo'</span>, () => {});</span> 632<span class="hljs-meta">> </span><span class="language-bash">(node:38638) MaxListenersExceededWarning: Possible EventEmitter memory leak</span> 633detected. 2 foo listeners added. Use emitter.setMaxListeners() to increase limit</code></pre> 634<p>In contrast, the following example turns off the default warning output and 635adds a custom handler to the <code>'warning'</code> event:</p> 636<pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">node --no-warnings</span> 637<span class="hljs-meta">> </span><span class="language-bash">const p = process.on(<span class="hljs-string">'warning'</span>, (warning) => console.warn(<span class="hljs-string">'Do not do that!'</span>));</span> 638<span class="hljs-meta">> </span><span class="language-bash">events.defaultMaxListeners = 1;</span> 639<span class="hljs-meta">> </span><span class="language-bash">process.on(<span class="hljs-string">'foo'</span>, () => {});</span> 640<span class="hljs-meta">> </span><span class="language-bash">process.on(<span class="hljs-string">'foo'</span>, () => {});</span> 641<span class="hljs-meta">> </span><span class="language-bash">Do not <span class="hljs-keyword">do</span> that!</span></code></pre> 642<p>The <code>--trace-warnings</code> command-line option can be used to have the default 643console output for warnings include the full stack trace of the warning.</p> 644<p>Launching Node.js using the <code>--throw-deprecation</code> command-line flag will 645cause custom deprecation warnings to be thrown as exceptions.</p> 646<p>Using the <code>--trace-deprecation</code> command-line flag will cause the custom 647deprecation to be printed to <code>stderr</code> along with the stack trace.</p> 648<p>Using the <code>--no-deprecation</code> command-line flag will suppress all reporting 649of the custom deprecation.</p> 650<p>The <code>*-deprecation</code> command-line flags only affect warnings that use the name 651<code>'DeprecationWarning'</code>.</p> 652<h4>Event: <code>'worker'</code><span><a class="mark" href="#process_event_worker" id="process_event_worker">#</a></span></h4> 653<div class="api_metadata"> 654<span>Added in: v14.18.0</span> 655</div> 656<ul> 657<li><code>worker</code> <a href="worker_threads.html#worker_threads_class_worker" class="type"><Worker></a> The <a href="worker_threads.html#worker_threads_class_worker" class="type"><Worker></a> that was created.</li> 658</ul> 659<p>The <code>'worker'</code> event is emitted after a new <a href="worker_threads.html#worker_threads_class_worker" class="type"><Worker></a> thread has been created.</p> 660<h5>Emitting custom warnings<span><a class="mark" href="#process_emitting_custom_warnings" id="process_emitting_custom_warnings">#</a></span></h5> 661<p>See the <a href="#process_process_emitwarning_warning_type_code_ctor"><code>process.emitWarning()</code></a> method for issuing 662custom or application-specific warnings.</p> 663<h5>Node.js warning names<span><a class="mark" href="#process_node_js_warning_names" id="process_node_js_warning_names">#</a></span></h5> 664<p>There are no strict guidelines for warning types (as identified by the <code>name</code> 665property) emitted by Node.js. New types of warnings can be added at any time. 666A few of the warning types that are most common include:</p> 667<ul> 668<li><code>'DeprecationWarning'</code> - Indicates use of a deprecated Node.js API or feature. 669Such warnings must include a <code>'code'</code> property identifying the 670<a href="deprecations.html">deprecation code</a>.</li> 671<li><code>'ExperimentalWarning'</code> - Indicates use of an experimental Node.js API or 672feature. Such features must be used with caution as they may change at any 673time and are not subject to the same strict semantic-versioning and long-term 674support policies as supported features.</li> 675<li><code>'MaxListenersExceededWarning'</code> - Indicates that too many listeners for a 676given event have been registered on either an <code>EventEmitter</code> or <code>EventTarget</code>. 677This is often an indication of a memory leak.</li> 678<li><code>'TimeoutOverflowWarning'</code> - Indicates that a numeric value that cannot fit 679within a 32-bit signed integer has been provided to either the <code>setTimeout()</code> 680or <code>setInterval()</code> functions.</li> 681<li><code>'UnsupportedWarning'</code> - Indicates use of an unsupported option or feature 682that will be ignored rather than treated as an error. One example is use of 683the HTTP response status message when using the HTTP/2 compatibility API.</li> 684</ul> 685<h4>Signal events<span><a class="mark" href="#process_signal_events" id="process_signal_events">#</a></span></h4> 686 687 688<p>Signal events will be emitted when the Node.js process receives a signal. Please 689refer to <a href="http://man7.org/linux/man-pages/man7/signal.7.html"><code>signal(7)</code></a> for a listing of standard POSIX signal names such as 690<code>'SIGINT'</code>, <code>'SIGHUP'</code>, etc.</p> 691<p>Signals are not available on <a href="worker_threads.html#worker_threads_class_worker"><code>Worker</code></a> threads.</p> 692<p>The signal handler will receive the signal's name (<code>'SIGINT'</code>, 693<code>'SIGTERM'</code>, etc.) as the first argument.</p> 694<p>The name of each event will be the uppercase common name for the signal (e.g. 695<code>'SIGINT'</code> for <code>SIGINT</code> signals).</p> 696<pre><code class="language-js"><span class="hljs-comment">// Begin reading from stdin so the process does not exit.</span> 697process.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">resume</span>(); 698 699process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'SIGINT'</span>, <span class="hljs-function">() =></span> { 700 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Received SIGINT. Press Control-D to exit.'</span>); 701}); 702 703<span class="hljs-comment">// Using a single function to handle multiple signals</span> 704<span class="hljs-keyword">function</span> <span class="hljs-title function_">handle</span>(<span class="hljs-params">signal</span>) { 705 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Received <span class="hljs-subst">${signal}</span>`</span>); 706} 707 708process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'SIGINT'</span>, handle); 709process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'SIGTERM'</span>, handle);</code></pre> 710<ul> 711<li><code>'SIGUSR1'</code> is reserved by Node.js to start the <a href="debugger.html">debugger</a>. It's possible to 712install a listener but doing so might interfere with the debugger.</li> 713<li><code>'SIGTERM'</code> and <code>'SIGINT'</code> have default handlers on non-Windows platforms that 714reset the terminal mode before exiting with code <code>128 + signal number</code>. If one 715of these signals has a listener installed, its default behavior will be 716removed (Node.js will no longer exit).</li> 717<li><code>'SIGPIPE'</code> is ignored by default. It can have a listener installed.</li> 718<li><code>'SIGHUP'</code> is generated on Windows when the console window is closed, and on 719other platforms under various similar conditions. See <a href="http://man7.org/linux/man-pages/man7/signal.7.html"><code>signal(7)</code></a>. It can have a 720listener installed, however Node.js will be unconditionally terminated by 721Windows about 10 seconds later. On non-Windows platforms, the default 722behavior of <code>SIGHUP</code> is to terminate Node.js, but once a listener has been 723installed its default behavior will be removed.</li> 724<li><code>'SIGTERM'</code> is not supported on Windows, it can be listened on.</li> 725<li><code>'SIGINT'</code> from the terminal is supported on all platforms, and can usually be 726generated with <kbd>Ctrl</kbd>+<kbd>C</kbd> (though this may be configurable). 727It is not generated when <a href="tty.html#tty_readstream_setrawmode_mode">terminal raw mode</a> is enabled and 728<kbd>Ctrl</kbd>+<kbd>C</kbd> is used.</li> 729<li><code>'SIGBREAK'</code> is delivered on Windows when <kbd>Ctrl</kbd>+<kbd>Break</kbd> is 730pressed. On non-Windows platforms, it can be listened on, but there is no way 731to send or generate it.</li> 732<li><code>'SIGWINCH'</code> is delivered when the console has been resized. On Windows, this 733will only happen on write to the console when the cursor is being moved, or 734when a readable tty is used in raw mode.</li> 735<li><code>'SIGKILL'</code> cannot have a listener installed, it will unconditionally 736terminate Node.js on all platforms.</li> 737<li><code>'SIGSTOP'</code> cannot have a listener installed.</li> 738<li><code>'SIGBUS'</code>, <code>'SIGFPE'</code>, <code>'SIGSEGV'</code> and <code>'SIGILL'</code>, when not raised 739artificially using <a href="http://man7.org/linux/man-pages/man2/kill.2.html"><code>kill(2)</code></a>, inherently leave the process in a state from 740which it is not safe to call JS listeners. Doing so might cause the process 741to stop responding.</li> 742<li><code>0</code> can be sent to test for the existence of a process, it has no effect if 743the process exists, but will throw an error if the process does not exist.</li> 744</ul> 745<p>Windows does not support signals so has no equivalent to termination by signal, 746but Node.js offers some emulation with <a href="#process_process_kill_pid_signal"><code>process.kill()</code></a>, and 747<a href="child_process.html#child_process_subprocess_kill_signal"><code>subprocess.kill()</code></a>:</p> 748<ul> 749<li>Sending <code>SIGINT</code>, <code>SIGTERM</code>, and <code>SIGKILL</code> will cause the unconditional 750termination of the target process, and afterwards, subprocess will report that 751the process was terminated by signal.</li> 752<li>Sending signal <code>0</code> can be used as a platform independent way to test for the 753existence of a process.</li> 754</ul> 755</section><section><h3><code>process.abort()</code><span><a class="mark" href="#process_process_abort" id="process_process_abort">#</a></span></h3> 756<div class="api_metadata"> 757<span>Added in: v0.7.0</span> 758</div> 759<p>The <code>process.abort()</code> method causes the Node.js process to exit immediately and 760generate a core file.</p> 761<p>This feature is not available in <a href="worker_threads.html#worker_threads_class_worker"><code>Worker</code></a> threads.</p> 762</section><section><h3><code>process.allowedNodeEnvironmentFlags</code><span><a class="mark" href="#process_process_allowednodeenvironmentflags" id="process_process_allowednodeenvironmentflags">#</a></span></h3> 763<div class="api_metadata"> 764<span>Added in: v10.10.0</span> 765</div> 766<ul> 767<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set" class="type"><Set></a></li> 768</ul> 769<p>The <code>process.allowedNodeEnvironmentFlags</code> property is a special, 770read-only <code>Set</code> of flags allowable within the <a href="cli.html#cli_node_options_options"><code>NODE_OPTIONS</code></a> 771environment variable.</p> 772<p><code>process.allowedNodeEnvironmentFlags</code> extends <code>Set</code>, but overrides 773<code>Set.prototype.has</code> to recognize several different possible flag 774representations. <code>process.allowedNodeEnvironmentFlags.has()</code> will 775return <code>true</code> in the following cases:</p> 776<ul> 777<li>Flags may omit leading single (<code>-</code>) or double (<code>--</code>) dashes; e.g., 778<code>inspect-brk</code> for <code>--inspect-brk</code>, or <code>r</code> for <code>-r</code>.</li> 779<li>Flags passed through to V8 (as listed in <code>--v8-options</code>) may replace 780one or more <em>non-leading</em> dashes for an underscore, or vice-versa; 781e.g., <code>--perf_basic_prof</code>, <code>--perf-basic-prof</code>, <code>--perf_basic-prof</code>, 782etc.</li> 783<li>Flags may contain one or more equals (<code>=</code>) characters; all 784characters after and including the first equals will be ignored; 785e.g., <code>--stack-trace-limit=100</code>.</li> 786<li>Flags <em>must</em> be allowable within <a href="cli.html#cli_node_options_options"><code>NODE_OPTIONS</code></a>.</li> 787</ul> 788<p>When iterating over <code>process.allowedNodeEnvironmentFlags</code>, flags will 789appear only <em>once</em>; each will begin with one or more dashes. Flags 790passed through to V8 will contain underscores instead of non-leading 791dashes:</p> 792<pre><code class="language-js">process.<span class="hljs-property">allowedNodeEnvironmentFlags</span>.<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class="hljs-params">flag</span>) =></span> { 793 <span class="hljs-comment">// -r</span> 794 <span class="hljs-comment">// --inspect-brk</span> 795 <span class="hljs-comment">// --abort_on_uncaught_exception</span> 796 <span class="hljs-comment">// ...</span> 797});</code></pre> 798<p>The methods <code>add()</code>, <code>clear()</code>, and <code>delete()</code> of 799<code>process.allowedNodeEnvironmentFlags</code> do nothing, and will fail 800silently.</p> 801<p>If Node.js was compiled <em>without</em> <a href="cli.html#cli_node_options_options"><code>NODE_OPTIONS</code></a> support (shown in 802<a href="#process_process_config"><code>process.config</code></a>), <code>process.allowedNodeEnvironmentFlags</code> will 803contain what <em>would have</em> been allowable.</p> 804</section><section><h3><code>process.arch</code><span><a class="mark" href="#process_process_arch" id="process_process_arch">#</a></span></h3> 805<div class="api_metadata"> 806<span>Added in: v0.5.0</span> 807</div> 808<ul> 809<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 810</ul> 811<p>The operating system CPU architecture for which the Node.js binary was compiled. 812Possible values are: <code>'arm'</code>, <code>'arm64'</code>, <code>'ia32'</code>, <code>'mips'</code>,<code>'mipsel'</code>, <code>'ppc'</code>, 813<code>'ppc64'</code>, <code>'s390'</code>, <code>'s390x'</code>, <code>'x32'</code>, and <code>'x64'</code>.</p> 814<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`This processor architecture is <span class="hljs-subst">${process.arch}</span>`</span>);</code></pre> 815</section><section><h3><code>process.argv</code><span><a class="mark" href="#process_process_argv" id="process_process_argv">#</a></span></h3> 816<div class="api_metadata"> 817<span>Added in: v0.1.27</span> 818</div> 819<ul> 820<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 821</ul> 822<p>The <code>process.argv</code> property returns an array containing the command-line 823arguments passed when the Node.js process was launched. The first element will 824be <a href="#process_process_execpath"><code>process.execPath</code></a>. See <code>process.argv0</code> if access to the original value 825of <code>argv[0]</code> is needed. The second element will be the path to the JavaScript 826file being executed. The remaining elements will be any additional command-line 827arguments.</p> 828<p>For example, assuming the following script for <code>process-args.js</code>:</p> 829<pre><code class="language-js"><span class="hljs-comment">// print process.argv</span> 830process.<span class="hljs-property">argv</span>.<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class="hljs-params">val, index</span>) =></span> { 831 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${index}</span>: <span class="hljs-subst">${val}</span>`</span>); 832});</code></pre> 833<p>Launching the Node.js process as:</p> 834<pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">node process-args.js one two=three four</span></code></pre> 835<p>Would generate the output:</p> 836<pre><code class="language-text">0: /usr/local/bin/node 8371: /Users/mjr/work/node/process-args.js 8382: one 8393: two=three 8404: four</code></pre> 841</section><section><h3><code>process.argv0</code><span><a class="mark" href="#process_process_argv0" id="process_process_argv0">#</a></span></h3> 842<div class="api_metadata"> 843<span>Added in: v6.4.0</span> 844</div> 845<ul> 846<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 847</ul> 848<p>The <code>process.argv0</code> property stores a read-only copy of the original value of 849<code>argv[0]</code> passed when Node.js starts.</p> 850<pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">bash -c <span class="hljs-string">'exec -a customArgv0 ./node'</span></span> 851<span class="hljs-meta">> </span><span class="language-bash">process.argv[0]</span> 852'/Volumes/code/external/node/out/Release/node' 853<span class="hljs-meta">> </span><span class="language-bash">process.argv0</span> 854'customArgv0'</code></pre> 855</section><section><h3><code>process.channel</code><span><a class="mark" href="#process_process_channel" id="process_process_channel">#</a></span></h3> 856<div class="api_metadata"> 857<details class="changelog"><summary>History</summary> 858<table> 859<tbody><tr><th>Version</th><th>Changes</th></tr> 860<tr><td>v14.0.0</td> 861<td><p>The object no longer accidentally exposes native C++ bindings.</p></td></tr> 862<tr><td>v7.1.0</td> 863<td><p><span>Added in: v7.1.0</span></p></td></tr> 864</tbody></table> 865</details> 866</div> 867<ul> 868<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 869</ul> 870<p>If the Node.js process was spawned with an IPC channel (see the 871<a href="child_process.html">Child Process</a> documentation), the <code>process.channel</code> 872property is a reference to the IPC channel. If no IPC channel exists, this 873property is <code>undefined</code>.</p> 874<h4><code>process.channel.ref()</code><span><a class="mark" href="#process_process_channel_ref" id="process_process_channel_ref">#</a></span></h4> 875<div class="api_metadata"> 876<span>Added in: v7.1.0</span> 877</div> 878<p>This method makes the IPC channel keep the event loop of the process 879running if <code>.unref()</code> has been called before.</p> 880<p>Typically, this is managed through the number of <code>'disconnect'</code> and <code>'message'</code> 881listeners on the <code>process</code> object. However, this method can be used to 882explicitly request a specific behavior.</p> 883<h4><code>process.channel.unref()</code><span><a class="mark" href="#process_process_channel_unref" id="process_process_channel_unref">#</a></span></h4> 884<div class="api_metadata"> 885<span>Added in: v7.1.0</span> 886</div> 887<p>This method makes the IPC channel not keep the event loop of the process 888running, and lets it finish even while the channel is open.</p> 889<p>Typically, this is managed through the number of <code>'disconnect'</code> and <code>'message'</code> 890listeners on the <code>process</code> object. However, this method can be used to 891explicitly request a specific behavior.</p> 892</section><section><h3><code>process.chdir(directory)</code><span><a class="mark" href="#process_process_chdir_directory" id="process_process_chdir_directory">#</a></span></h3> 893<div class="api_metadata"> 894<span>Added in: v0.1.17</span> 895</div> 896<ul> 897<li><code>directory</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 898</ul> 899<p>The <code>process.chdir()</code> method changes the current working directory of the 900Node.js process or throws an exception if doing so fails (for instance, if 901the specified <code>directory</code> does not exist).</p> 902<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Starting directory: <span class="hljs-subst">${process.cwd()}</span>`</span>); 903<span class="hljs-keyword">try</span> { 904 process.<span class="hljs-title function_">chdir</span>(<span class="hljs-string">'/tmp'</span>); 905 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`New directory: <span class="hljs-subst">${process.cwd()}</span>`</span>); 906} <span class="hljs-keyword">catch</span> (err) { 907 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`chdir: <span class="hljs-subst">${err}</span>`</span>); 908}</code></pre> 909<p>This feature is not available in <a href="worker_threads.html#worker_threads_class_worker"><code>Worker</code></a> threads.</p> 910</section><section><h3><code>process.config</code><span><a class="mark" href="#process_process_config" id="process_process_config">#</a></span></h3> 911<div class="api_metadata"> 912<span>Added in: v0.7.7</span> 913</div> 914<ul> 915<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 916</ul> 917<p>The <code>process.config</code> property returns an <code>Object</code> containing the JavaScript 918representation of the configure options used to compile the current Node.js 919executable. This is the same as the <code>config.gypi</code> file that was produced when 920running the <code>./configure</code> script.</p> 921<p>An example of the possible output looks like:</p> 922<!-- eslint-skip --> 923<pre><code class="language-js">{ 924 <span class="hljs-attr">target_defaults</span>: 925 { <span class="hljs-attr">cflags</span>: [], 926 <span class="hljs-attr">default_configuration</span>: <span class="hljs-string">'Release'</span>, 927 <span class="hljs-attr">defines</span>: [], 928 <span class="hljs-attr">include_dirs</span>: [], 929 <span class="hljs-attr">libraries</span>: [] }, 930 <span class="hljs-attr">variables</span>: 931 { 932 <span class="hljs-attr">host_arch</span>: <span class="hljs-string">'x64'</span>, 933 <span class="hljs-attr">napi_build_version</span>: <span class="hljs-number">5</span>, 934 <span class="hljs-attr">node_install_npm</span>: <span class="hljs-string">'true'</span>, 935 <span class="hljs-attr">node_prefix</span>: <span class="hljs-string">''</span>, 936 <span class="hljs-attr">node_shared_cares</span>: <span class="hljs-string">'false'</span>, 937 <span class="hljs-attr">node_shared_http_parser</span>: <span class="hljs-string">'false'</span>, 938 <span class="hljs-attr">node_shared_libuv</span>: <span class="hljs-string">'false'</span>, 939 <span class="hljs-attr">node_shared_zlib</span>: <span class="hljs-string">'false'</span>, 940 <span class="hljs-attr">node_use_dtrace</span>: <span class="hljs-string">'false'</span>, 941 <span class="hljs-attr">node_use_openssl</span>: <span class="hljs-string">'true'</span>, 942 <span class="hljs-attr">node_shared_openssl</span>: <span class="hljs-string">'false'</span>, 943 <span class="hljs-attr">strict_aliasing</span>: <span class="hljs-string">'true'</span>, 944 <span class="hljs-attr">target_arch</span>: <span class="hljs-string">'x64'</span>, 945 <span class="hljs-attr">v8_use_snapshot</span>: <span class="hljs-number">1</span> 946 } 947}</code></pre> 948<p>The <code>process.config</code> property is <strong>not</strong> read-only and there are existing 949modules in the ecosystem that are known to extend, modify, or entirely replace 950the value of <code>process.config</code>.</p> 951</section><section><h3><code>process.connected</code><span><a class="mark" href="#process_process_connected" id="process_process_connected">#</a></span></h3> 952<div class="api_metadata"> 953<span>Added in: v0.7.2</span> 954</div> 955<ul> 956<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 957</ul> 958<p>If the Node.js process is spawned with an IPC channel (see the <a href="child_process.html">Child Process</a> 959and <a href="cluster.html">Cluster</a> documentation), the <code>process.connected</code> property will return 960<code>true</code> so long as the IPC channel is connected and will return <code>false</code> after 961<code>process.disconnect()</code> is called.</p> 962<p>Once <code>process.connected</code> is <code>false</code>, it is no longer possible to send messages 963over the IPC channel using <code>process.send()</code>.</p> 964</section><section><h3><code>process.cpuUsage([previousValue])</code><span><a class="mark" href="#process_process_cpuusage_previousvalue" id="process_process_cpuusage_previousvalue">#</a></span></h3> 965<div class="api_metadata"> 966<span>Added in: v6.1.0</span> 967</div> 968<ul> 969<li><code>previousValue</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> A previous return value from calling 970<code>process.cpuUsage()</code></li> 971<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 972<ul> 973<li><code>user</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 974<li><code>system</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 975</ul> 976</li> 977</ul> 978<p>The <code>process.cpuUsage()</code> method returns the user and system CPU time usage of 979the current process, in an object with properties <code>user</code> and <code>system</code>, whose 980values are microsecond values (millionth of a second). These values measure time 981spent in user and system code respectively, and may end up being greater than 982actual elapsed time if multiple CPU cores are performing work for this process.</p> 983<p>The result of a previous call to <code>process.cpuUsage()</code> can be passed as the 984argument to the function, to get a diff reading.</p> 985<pre><code class="language-js"><span class="hljs-keyword">const</span> startUsage = process.<span class="hljs-title function_">cpuUsage</span>(); 986<span class="hljs-comment">// { user: 38579, system: 6986 }</span> 987 988<span class="hljs-comment">// spin the CPU for 500 milliseconds</span> 989<span class="hljs-keyword">const</span> now = <span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(); 990<span class="hljs-keyword">while</span> (<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>() - now < <span class="hljs-number">500</span>); 991 992<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span class="hljs-title function_">cpuUsage</span>(startUsage)); 993<span class="hljs-comment">// { user: 514883, system: 11226 }</span></code></pre> 994</section><section><h3><code>process.cwd()</code><span><a class="mark" href="#process_process_cwd" id="process_process_cwd">#</a></span></h3> 995<div class="api_metadata"> 996<span>Added in: v0.1.8</span> 997</div> 998<ul> 999<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1000</ul> 1001<p>The <code>process.cwd()</code> method returns the current working directory of the Node.js 1002process.</p> 1003<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Current directory: <span class="hljs-subst">${process.cwd()}</span>`</span>);</code></pre> 1004</section><section><h3><code>process.debugPort</code><span><a class="mark" href="#process_process_debugport" id="process_process_debugport">#</a></span></h3> 1005<div class="api_metadata"> 1006<span>Added in: v0.7.2</span> 1007</div> 1008<ul> 1009<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1010</ul> 1011<p>The port used by the Node.js debugger when enabled.</p> 1012<pre><code class="language-js">process.<span class="hljs-property">debugPort</span> = <span class="hljs-number">5858</span>;</code></pre> 1013</section><section><h3><code>process.disconnect()</code><span><a class="mark" href="#process_process_disconnect" id="process_process_disconnect">#</a></span></h3> 1014<div class="api_metadata"> 1015<span>Added in: v0.7.2</span> 1016</div> 1017<p>If the Node.js process is spawned with an IPC channel (see the <a href="child_process.html">Child Process</a> 1018and <a href="cluster.html">Cluster</a> documentation), the <code>process.disconnect()</code> method will close the 1019IPC channel to the parent process, allowing the child process to exit gracefully 1020once there are no other connections keeping it alive.</p> 1021<p>The effect of calling <code>process.disconnect()</code> is the same as calling 1022<a href="child_process.html#child_process_subprocess_disconnect"><code>ChildProcess.disconnect()</code></a> from the parent process.</p> 1023<p>If the Node.js process was not spawned with an IPC channel, 1024<code>process.disconnect()</code> will be <code>undefined</code>.</p> 1025</section><section><h3><code>process.dlopen(module, filename[, flags])</code><span><a class="mark" href="#process_process_dlopen_module_filename_flags" id="process_process_dlopen_module_filename_flags">#</a></span></h3> 1026<div class="api_metadata"> 1027<details class="changelog"><summary>History</summary> 1028<table> 1029<tbody><tr><th>Version</th><th>Changes</th></tr> 1030<tr><td>v9.0.0</td> 1031<td><p>Added support for the <code>flags</code> argument.</p></td></tr> 1032<tr><td>v0.1.16</td> 1033<td><p><span>Added in: v0.1.16</span></p></td></tr> 1034</tbody></table> 1035</details> 1036</div> 1037<ul> 1038<li><code>module</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 1039<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1040<li><code>flags</code> <a href="os.html#os_dlopen_constants" class="type"><os.constants.dlopen></a> <strong>Default:</strong> <code>os.constants.dlopen.RTLD_LAZY</code></li> 1041</ul> 1042<p>The <code>process.dlopen()</code> method allows dynamically loading shared objects. It is 1043primarily used by <code>require()</code> to load C++ Addons, and should not be used 1044directly, except in special cases. In other words, <a href="globals.html#globals_require"><code>require()</code></a> should be 1045preferred over <code>process.dlopen()</code> unless there are specific reasons such as 1046custom dlopen flags or loading from ES modules.</p> 1047<p>The <code>flags</code> argument is an integer that allows to specify dlopen 1048behavior. See the <a href="os.html#os_dlopen_constants"><code>os.constants.dlopen</code></a> documentation for details.</p> 1049<p>An important requirement when calling <code>process.dlopen()</code> is that the <code>module</code> 1050instance must be passed. Functions exported by the C++ Addon are then 1051accessible via <code>module.exports</code>.</p> 1052<p>The example below shows how to load a C++ Addon, named <code>local.node</code>, 1053that exports a <code>foo</code> function. All the symbols are loaded before 1054the call returns, by passing the <code>RTLD_NOW</code> constant. In this example 1055the constant is assumed to be available.</p> 1056<pre><code class="language-js"><span class="hljs-keyword">const</span> os = <span class="hljs-built_in">require</span>(<span class="hljs-string">'os'</span>); 1057<span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">'path'</span>); 1058<span class="hljs-keyword">const</span> <span class="hljs-variable language_">module</span> = { <span class="hljs-attr">exports</span>: {} }; 1059process.<span class="hljs-title function_">dlopen</span>(<span class="hljs-variable language_">module</span>, path.<span class="hljs-title function_">join</span>(__dirname, <span class="hljs-string">'local.node'</span>), 1060 os.<span class="hljs-property">constants</span>.<span class="hljs-property">dlopen</span>.<span class="hljs-property">RTLD_NOW</span>); 1061<span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span>.<span class="hljs-title function_">foo</span>();</code></pre> 1062</section><section><h3><code>process.emitWarning(warning[, options])</code><span><a class="mark" href="#process_process_emitwarning_warning_options" id="process_process_emitwarning_warning_options">#</a></span></h3> 1063<div class="api_metadata"> 1064<span>Added in: v8.0.0</span> 1065</div> 1066<ul> 1067<li><code>warning</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> The warning to emit.</li> 1068<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1069<ul> 1070<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> When <code>warning</code> is a <code>String</code>, <code>type</code> is the name to use 1071for the <em>type</em> of warning being emitted. <strong>Default:</strong> <code>'Warning'</code>.</li> 1072<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A unique identifier for the warning instance being emitted.</li> 1073<li><code>ctor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> When <code>warning</code> is a <code>String</code>, <code>ctor</code> is an optional 1074function used to limit the generated stack trace. <strong>Default:</strong> 1075<code>process.emitWarning</code>.</li> 1076<li><code>detail</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Additional text to include with the error.</li> 1077</ul> 1078</li> 1079</ul> 1080<p>The <code>process.emitWarning()</code> method can be used to emit custom or application 1081specific process warnings. These can be listened for by adding a handler to the 1082<a href="#process_event_warning"><code>'warning'</code></a> event.</p> 1083<pre><code class="language-js"><span class="hljs-comment">// Emit a warning with a code and additional detail.</span> 1084process.<span class="hljs-title function_">emitWarning</span>(<span class="hljs-string">'Something happened!'</span>, { 1085 <span class="hljs-attr">code</span>: <span class="hljs-string">'MY_WARNING'</span>, 1086 <span class="hljs-attr">detail</span>: <span class="hljs-string">'This is some additional information'</span> 1087}); 1088<span class="hljs-comment">// Emits:</span> 1089<span class="hljs-comment">// (node:56338) [MY_WARNING] Warning: Something happened!</span> 1090<span class="hljs-comment">// This is some additional information</span></code></pre> 1091<p>In this example, an <code>Error</code> object is generated internally by 1092<code>process.emitWarning()</code> and passed through to the 1093<a href="#process_event_warning"><code>'warning'</code></a> handler.</p> 1094<pre><code class="language-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'warning'</span>, <span class="hljs-function">(<span class="hljs-params">warning</span>) =></span> { 1095 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span class="hljs-property">name</span>); <span class="hljs-comment">// 'Warning'</span> 1096 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span class="hljs-property">message</span>); <span class="hljs-comment">// 'Something happened!'</span> 1097 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span class="hljs-property">code</span>); <span class="hljs-comment">// 'MY_WARNING'</span> 1098 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span class="hljs-property">stack</span>); <span class="hljs-comment">// Stack trace</span> 1099 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span class="hljs-property">detail</span>); <span class="hljs-comment">// 'This is some additional information'</span> 1100});</code></pre> 1101<p>If <code>warning</code> is passed as an <code>Error</code> object, the <code>options</code> argument is ignored.</p> 1102</section><section><h3><code>process.emitWarning(warning[, type[, code]][, ctor])</code><span><a class="mark" href="#process_process_emitwarning_warning_type_code_ctor" id="process_process_emitwarning_warning_type_code_ctor">#</a></span></h3> 1103<div class="api_metadata"> 1104<span>Added in: v6.0.0</span> 1105</div> 1106<ul> 1107<li><code>warning</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> The warning to emit.</li> 1108<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> When <code>warning</code> is a <code>String</code>, <code>type</code> is the name to use 1109for the <em>type</em> of warning being emitted. <strong>Default:</strong> <code>'Warning'</code>.</li> 1110<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A unique identifier for the warning instance being emitted.</li> 1111<li><code>ctor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> When <code>warning</code> is a <code>String</code>, <code>ctor</code> is an optional 1112function used to limit the generated stack trace. <strong>Default:</strong> 1113<code>process.emitWarning</code>.</li> 1114</ul> 1115<p>The <code>process.emitWarning()</code> method can be used to emit custom or application 1116specific process warnings. These can be listened for by adding a handler to the 1117<a href="#process_event_warning"><code>'warning'</code></a> event.</p> 1118<pre><code class="language-js"><span class="hljs-comment">// Emit a warning using a string.</span> 1119process.<span class="hljs-title function_">emitWarning</span>(<span class="hljs-string">'Something happened!'</span>); 1120<span class="hljs-comment">// Emits: (node: 56338) Warning: Something happened!</span></code></pre> 1121<pre><code class="language-js"><span class="hljs-comment">// Emit a warning using a string and a type.</span> 1122process.<span class="hljs-title function_">emitWarning</span>(<span class="hljs-string">'Something Happened!'</span>, <span class="hljs-string">'CustomWarning'</span>); 1123<span class="hljs-comment">// Emits: (node:56338) CustomWarning: Something Happened!</span></code></pre> 1124<pre><code class="language-js">process.<span class="hljs-title function_">emitWarning</span>(<span class="hljs-string">'Something happened!'</span>, <span class="hljs-string">'CustomWarning'</span>, <span class="hljs-string">'WARN001'</span>); 1125<span class="hljs-comment">// Emits: (node:56338) [WARN001] CustomWarning: Something happened!</span></code></pre> 1126<p>In each of the previous examples, an <code>Error</code> object is generated internally by 1127<code>process.emitWarning()</code> and passed through to the <a href="#process_event_warning"><code>'warning'</code></a> 1128handler.</p> 1129<pre><code class="language-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'warning'</span>, <span class="hljs-function">(<span class="hljs-params">warning</span>) =></span> { 1130 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span class="hljs-property">name</span>); 1131 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span class="hljs-property">message</span>); 1132 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span class="hljs-property">code</span>); 1133 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span class="hljs-property">stack</span>); 1134});</code></pre> 1135<p>If <code>warning</code> is passed as an <code>Error</code> object, it will be passed through to the 1136<code>'warning'</code> event handler unmodified (and the optional <code>type</code>, 1137<code>code</code> and <code>ctor</code> arguments will be ignored):</p> 1138<pre><code class="language-js"><span class="hljs-comment">// Emit a warning using an Error object.</span> 1139<span class="hljs-keyword">const</span> myWarning = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Something happened!'</span>); 1140<span class="hljs-comment">// Use the Error name property to specify the type name</span> 1141myWarning.<span class="hljs-property">name</span> = <span class="hljs-string">'CustomWarning'</span>; 1142myWarning.<span class="hljs-property">code</span> = <span class="hljs-string">'WARN001'</span>; 1143 1144process.<span class="hljs-title function_">emitWarning</span>(myWarning); 1145<span class="hljs-comment">// Emits: (node:56338) [WARN001] CustomWarning: Something happened!</span></code></pre> 1146<p>A <code>TypeError</code> is thrown if <code>warning</code> is anything other than a string or <code>Error</code> 1147object.</p> 1148<p>While process warnings use <code>Error</code> objects, the process warning 1149mechanism is <strong>not</strong> a replacement for normal error handling mechanisms.</p> 1150<p>The following additional handling is implemented if the warning <code>type</code> is 1151<code>'DeprecationWarning'</code>:</p> 1152<ul> 1153<li>If the <code>--throw-deprecation</code> command-line flag is used, the deprecation 1154warning is thrown as an exception rather than being emitted as an event.</li> 1155<li>If the <code>--no-deprecation</code> command-line flag is used, the deprecation 1156warning is suppressed.</li> 1157<li>If the <code>--trace-deprecation</code> command-line flag is used, the deprecation 1158warning is printed to <code>stderr</code> along with the full stack trace.</li> 1159</ul> 1160<h4>Avoiding duplicate warnings<span><a class="mark" href="#process_avoiding_duplicate_warnings" id="process_avoiding_duplicate_warnings">#</a></span></h4> 1161<p>As a best practice, warnings should be emitted only once per process. To do 1162so, it is recommended to place the <code>emitWarning()</code> behind a simple boolean 1163flag as illustrated in the example below:</p> 1164<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">emitMyWarning</span>(<span class="hljs-params"></span>) { 1165 <span class="hljs-keyword">if</span> (!emitMyWarning.<span class="hljs-property">warned</span>) { 1166 emitMyWarning.<span class="hljs-property">warned</span> = <span class="hljs-literal">true</span>; 1167 process.<span class="hljs-title function_">emitWarning</span>(<span class="hljs-string">'Only warn once!'</span>); 1168 } 1169} 1170<span class="hljs-title function_">emitMyWarning</span>(); 1171<span class="hljs-comment">// Emits: (node: 56339) Warning: Only warn once!</span> 1172<span class="hljs-title function_">emitMyWarning</span>(); 1173<span class="hljs-comment">// Emits nothing</span></code></pre> 1174</section><section><h3><code>process.env</code><span><a class="mark" href="#process_process_env" id="process_process_env">#</a></span></h3> 1175<div class="api_metadata"> 1176<details class="changelog"><summary>History</summary> 1177<table> 1178<tbody><tr><th>Version</th><th>Changes</th></tr> 1179<tr><td>v11.14.0</td> 1180<td><p>Worker threads will now use a copy of the parent thread’s <code>process.env</code> by default, configurable through the <code>env</code> option of the <code>Worker</code> constructor.</p></td></tr> 1181<tr><td>v10.0.0</td> 1182<td><p>Implicit conversion of variable value to string is deprecated.</p></td></tr> 1183<tr><td>v0.1.27</td> 1184<td><p><span>Added in: v0.1.27</span></p></td></tr> 1185</tbody></table> 1186</details> 1187</div> 1188<ul> 1189<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 1190</ul> 1191<p>The <code>process.env</code> property returns an object containing the user environment. 1192See <a href="http://man7.org/linux/man-pages/man7/environ.7.html"><code>environ(7)</code></a>.</p> 1193<p>An example of this object looks like:</p> 1194<!-- eslint-skip --> 1195<pre><code class="language-js">{ 1196 <span class="hljs-attr">TERM</span>: <span class="hljs-string">'xterm-256color'</span>, 1197 <span class="hljs-attr">SHELL</span>: <span class="hljs-string">'/usr/local/bin/bash'</span>, 1198 <span class="hljs-attr">USER</span>: <span class="hljs-string">'maciej'</span>, 1199 <span class="hljs-attr">PATH</span>: <span class="hljs-string">'~/.bin/:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'</span>, 1200 <span class="hljs-attr">PWD</span>: <span class="hljs-string">'/Users/maciej'</span>, 1201 <span class="hljs-attr">EDITOR</span>: <span class="hljs-string">'vim'</span>, 1202 <span class="hljs-attr">SHLVL</span>: <span class="hljs-string">'1'</span>, 1203 <span class="hljs-attr">HOME</span>: <span class="hljs-string">'/Users/maciej'</span>, 1204 <span class="hljs-attr">LOGNAME</span>: <span class="hljs-string">'maciej'</span>, 1205 <span class="hljs-attr">_</span>: <span class="hljs-string">'/usr/local/bin/node'</span> 1206}</code></pre> 1207<p>It is possible to modify this object, but such modifications will not be 1208reflected outside the Node.js process, or (unless explicitly requested) 1209to other <a href="worker_threads.html#worker_threads_class_worker"><code>Worker</code></a> threads. 1210In other words, the following example would not work:</p> 1211<pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">node -e <span class="hljs-string">'process.env.foo = "bar"'</span> && <span class="hljs-built_in">echo</span> <span class="hljs-variable">$foo</span></span></code></pre> 1212<p>While the following will:</p> 1213<pre><code class="language-js">process.<span class="hljs-property">env</span>.<span class="hljs-property">foo</span> = <span class="hljs-string">'bar'</span>; 1214<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span class="hljs-property">env</span>.<span class="hljs-property">foo</span>);</code></pre> 1215<p>Assigning a property on <code>process.env</code> will implicitly convert the value 1216to a string. <strong>This behavior is deprecated.</strong> Future versions of Node.js may 1217throw an error when the value is not a string, number, or boolean.</p> 1218<pre><code class="language-js">process.<span class="hljs-property">env</span>.<span class="hljs-property">test</span> = <span class="hljs-literal">null</span>; 1219<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span class="hljs-property">env</span>.<span class="hljs-property">test</span>); 1220<span class="hljs-comment">// => 'null'</span> 1221process.<span class="hljs-property">env</span>.<span class="hljs-property">test</span> = <span class="hljs-literal">undefined</span>; 1222<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span class="hljs-property">env</span>.<span class="hljs-property">test</span>); 1223<span class="hljs-comment">// => 'undefined'</span></code></pre> 1224<p>Use <code>delete</code> to delete a property from <code>process.env</code>.</p> 1225<pre><code class="language-js">process.<span class="hljs-property">env</span>.<span class="hljs-property">TEST</span> = <span class="hljs-number">1</span>; 1226<span class="hljs-keyword">delete</span> process.<span class="hljs-property">env</span>.<span class="hljs-property">TEST</span>; 1227<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span class="hljs-property">env</span>.<span class="hljs-property">TEST</span>); 1228<span class="hljs-comment">// => undefined</span></code></pre> 1229<p>On Windows operating systems, environment variables are case-insensitive.</p> 1230<pre><code class="language-js">process.<span class="hljs-property">env</span>.<span class="hljs-property">TEST</span> = <span class="hljs-number">1</span>; 1231<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span class="hljs-property">env</span>.<span class="hljs-property">test</span>); 1232<span class="hljs-comment">// => 1</span></code></pre> 1233<p>Unless explicitly specified when creating a <a href="worker_threads.html#worker_threads_class_worker"><code>Worker</code></a> instance, 1234each <a href="worker_threads.html#worker_threads_class_worker"><code>Worker</code></a> thread has its own copy of <code>process.env</code>, based on its 1235parent thread’s <code>process.env</code>, or whatever was specified as the <code>env</code> option 1236to the <a href="worker_threads.html#worker_threads_class_worker"><code>Worker</code></a> constructor. Changes to <code>process.env</code> will not be visible 1237across <a href="worker_threads.html#worker_threads_class_worker"><code>Worker</code></a> threads, and only the main thread can make changes that 1238are visible to the operating system or to native add-ons.</p> 1239</section><section><h3><code>process.execArgv</code><span><a class="mark" href="#process_process_execargv" id="process_process_execargv">#</a></span></h3> 1240<div class="api_metadata"> 1241<span>Added in: v0.7.7</span> 1242</div> 1243<ul> 1244<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 1245</ul> 1246<p>The <code>process.execArgv</code> property returns the set of Node.js-specific command-line 1247options passed when the Node.js process was launched. These options do not 1248appear in the array returned by the <a href="#process_process_argv"><code>process.argv</code></a> property, and do not 1249include the Node.js executable, the name of the script, or any options following 1250the script name. These options are useful in order to spawn child processes with 1251the same execution environment as the parent.</p> 1252<pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">node --harmony script.js --version</span></code></pre> 1253<p>Results in <code>process.execArgv</code>:</p> 1254<!-- eslint-disable semi --> 1255<pre><code class="language-js">[<span class="hljs-string">'--harmony'</span>]</code></pre> 1256<p>And <code>process.argv</code>:</p> 1257<!-- eslint-disable semi --> 1258<pre><code class="language-js">[<span class="hljs-string">'/usr/local/bin/node'</span>, <span class="hljs-string">'script.js'</span>, <span class="hljs-string">'--version'</span>]</code></pre> 1259<p>Refer to <a href="worker_threads.html#worker_threads_new_worker_filename_options"><code>Worker</code> constructor</a> for the detailed behavior of worker 1260threads with this property.</p> 1261</section><section><h3><code>process.execPath</code><span><a class="mark" href="#process_process_execpath" id="process_process_execpath">#</a></span></h3> 1262<div class="api_metadata"> 1263<span>Added in: v0.1.100</span> 1264</div> 1265<ul> 1266<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1267</ul> 1268<p>The <code>process.execPath</code> property returns the absolute pathname of the executable 1269that started the Node.js process. Symbolic links, if any, are resolved.</p> 1270<!-- eslint-disable semi --> 1271<pre><code class="language-js"><span class="hljs-string">'/usr/local/bin/node'</span></code></pre> 1272</section><section><h3><code>process.exit([code])</code><span><a class="mark" href="#process_process_exit_code" id="process_process_exit_code">#</a></span></h3> 1273<div class="api_metadata"> 1274<span>Added in: v0.1.13</span> 1275</div> 1276<ul> 1277<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The exit code. <strong>Default:</strong> <code>0</code>.</li> 1278</ul> 1279<p>The <code>process.exit()</code> method instructs Node.js to terminate the process 1280synchronously with an exit status of <code>code</code>. If <code>code</code> is omitted, exit uses 1281either the 'success' code <code>0</code> or the value of <code>process.exitCode</code> if it has been 1282set. Node.js will not terminate until all the <a href="#process_event_exit"><code>'exit'</code></a> event listeners are 1283called.</p> 1284<p>To exit with a 'failure' code:</p> 1285<pre><code class="language-js">process.<span class="hljs-title function_">exit</span>(<span class="hljs-number">1</span>);</code></pre> 1286<p>The shell that executed Node.js should see the exit code as <code>1</code>.</p> 1287<p>Calling <code>process.exit()</code> will force the process to exit as quickly as possible 1288even if there are still asynchronous operations pending that have not yet 1289completed fully, including I/O operations to <code>process.stdout</code> and 1290<code>process.stderr</code>.</p> 1291<p>In most situations, it is not actually necessary to call <code>process.exit()</code> 1292explicitly. The Node.js process will exit on its own <em>if there is no additional 1293work pending</em> in the event loop. The <code>process.exitCode</code> property can be set to 1294tell the process which exit code to use when the process exits gracefully.</p> 1295<p>For instance, the following example illustrates a <em>misuse</em> of the 1296<code>process.exit()</code> method that could lead to data printed to stdout being 1297truncated and lost:</p> 1298<pre><code class="language-js"><span class="hljs-comment">// This is an example of what *not* to do:</span> 1299<span class="hljs-keyword">if</span> (<span class="hljs-title function_">someConditionNotMet</span>()) { 1300 <span class="hljs-title function_">printUsageToStdout</span>(); 1301 process.<span class="hljs-title function_">exit</span>(<span class="hljs-number">1</span>); 1302}</code></pre> 1303<p>The reason this is problematic is because writes to <code>process.stdout</code> in Node.js 1304are sometimes <em>asynchronous</em> and may occur over multiple ticks of the Node.js 1305event loop. Calling <code>process.exit()</code>, however, forces the process to exit 1306<em>before</em> those additional writes to <code>stdout</code> can be performed.</p> 1307<p>Rather than calling <code>process.exit()</code> directly, the code <em>should</em> set the 1308<code>process.exitCode</code> and allow the process to exit naturally by avoiding 1309scheduling any additional work for the event loop:</p> 1310<pre><code class="language-js"><span class="hljs-comment">// How to properly set the exit code while letting</span> 1311<span class="hljs-comment">// the process exit gracefully.</span> 1312<span class="hljs-keyword">if</span> (<span class="hljs-title function_">someConditionNotMet</span>()) { 1313 <span class="hljs-title function_">printUsageToStdout</span>(); 1314 process.<span class="hljs-property">exitCode</span> = <span class="hljs-number">1</span>; 1315}</code></pre> 1316<p>If it is necessary to terminate the Node.js process due to an error condition, 1317throwing an <em>uncaught</em> error and allowing the process to terminate accordingly 1318is safer than calling <code>process.exit()</code>.</p> 1319<p>In <a href="worker_threads.html#worker_threads_class_worker"><code>Worker</code></a> threads, this function stops the current thread rather 1320than the current process.</p> 1321</section><section><h3><code>process.exitCode</code><span><a class="mark" href="#process_process_exitcode" id="process_process_exitcode">#</a></span></h3> 1322<div class="api_metadata"> 1323<span>Added in: v0.11.8</span> 1324</div> 1325<ul> 1326<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1327</ul> 1328<p>A number which will be the process exit code, when the process either 1329exits gracefully, or is exited via <a href="#process_process_exit_code"><code>process.exit()</code></a> without specifying 1330a code.</p> 1331<p>Specifying a code to <a href="#process_process_exit_code"><code>process.exit(code)</code></a> will override any 1332previous setting of <code>process.exitCode</code>.</p> 1333</section><section><h3><code>process.getegid()</code><span><a class="mark" href="#process_process_getegid" id="process_process_getegid">#</a></span></h3> 1334<div class="api_metadata"> 1335<span>Added in: v2.0.0</span> 1336</div> 1337<p>The <code>process.getegid()</code> method returns the numerical effective group identity 1338of the Node.js process. (See <a href="http://man7.org/linux/man-pages/man2/getegid.2.html"><code>getegid(2)</code></a>.)</p> 1339<pre><code class="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-property">getegid</span>) { 1340 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Current gid: <span class="hljs-subst">${process.getegid()}</span>`</span>); 1341}</code></pre> 1342<p>This function is only available on POSIX platforms (i.e. not Windows or 1343Android).</p> 1344</section><section><h3><code>process.geteuid()</code><span><a class="mark" href="#process_process_geteuid" id="process_process_geteuid">#</a></span></h3> 1345<div class="api_metadata"> 1346<span>Added in: v2.0.0</span> 1347</div> 1348<ul> 1349<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 1350</ul> 1351<p>The <code>process.geteuid()</code> method returns the numerical effective user identity of 1352the process. (See <a href="http://man7.org/linux/man-pages/man2/geteuid.2.html"><code>geteuid(2)</code></a>.)</p> 1353<pre><code class="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-property">geteuid</span>) { 1354 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Current uid: <span class="hljs-subst">${process.geteuid()}</span>`</span>); 1355}</code></pre> 1356<p>This function is only available on POSIX platforms (i.e. not Windows or 1357Android).</p> 1358</section><section><h3><code>process.getgid()</code><span><a class="mark" href="#process_process_getgid" id="process_process_getgid">#</a></span></h3> 1359<div class="api_metadata"> 1360<span>Added in: v0.1.31</span> 1361</div> 1362<ul> 1363<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 1364</ul> 1365<p>The <code>process.getgid()</code> method returns the numerical group identity of the 1366process. (See <a href="http://man7.org/linux/man-pages/man2/getgid.2.html"><code>getgid(2)</code></a>.)</p> 1367<pre><code class="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-property">getgid</span>) { 1368 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Current gid: <span class="hljs-subst">${process.getgid()}</span>`</span>); 1369}</code></pre> 1370<p>This function is only available on POSIX platforms (i.e. not Windows or 1371Android).</p> 1372</section><section><h3><code>process.getgroups()</code><span><a class="mark" href="#process_process_getgroups" id="process_process_getgroups">#</a></span></h3> 1373<div class="api_metadata"> 1374<span>Added in: v0.9.4</span> 1375</div> 1376<ul> 1377<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer[]></a></li> 1378</ul> 1379<p>The <code>process.getgroups()</code> method returns an array with the supplementary group 1380IDs. POSIX leaves it unspecified if the effective group ID is included but 1381Node.js ensures it always is.</p> 1382<pre><code class="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-property">getgroups</span>) { 1383 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span class="hljs-title function_">getgroups</span>()); <span class="hljs-comment">// [ 16, 21, 297 ]</span> 1384}</code></pre> 1385<p>This function is only available on POSIX platforms (i.e. not Windows or 1386Android).</p> 1387</section><section><h3><code>process.getuid()</code><span><a class="mark" href="#process_process_getuid" id="process_process_getuid">#</a></span></h3> 1388<div class="api_metadata"> 1389<span>Added in: v0.1.28</span> 1390</div> 1391<ul> 1392<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1393</ul> 1394<p>The <code>process.getuid()</code> method returns the numeric user identity of the process. 1395(See <a href="http://man7.org/linux/man-pages/man2/getuid.2.html"><code>getuid(2)</code></a>.)</p> 1396<pre><code class="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-property">getuid</span>) { 1397 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Current uid: <span class="hljs-subst">${process.getuid()}</span>`</span>); 1398}</code></pre> 1399<p>This function is only available on POSIX platforms (i.e. not Windows or 1400Android).</p> 1401</section><section><h3><code>process.hasUncaughtExceptionCaptureCallback()</code><span><a class="mark" href="#process_process_hasuncaughtexceptioncapturecallback" id="process_process_hasuncaughtexceptioncapturecallback">#</a></span></h3> 1402<div class="api_metadata"> 1403<span>Added in: v9.3.0</span> 1404</div> 1405<ul> 1406<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1407</ul> 1408<p>Indicates whether a callback has been set using 1409<a href="#process_process_setuncaughtexceptioncapturecallback_fn"><code>process.setUncaughtExceptionCaptureCallback()</code></a>.</p> 1410</section><section><h3><code>process.hrtime([time])</code><span><a class="mark" href="#process_process_hrtime_time" id="process_process_hrtime_time">#</a></span></h3> 1411<div class="api_metadata"> 1412<span>Added in: v0.7.6</span> 1413</div> 1414<p></p><div class="api_stability api_stability_3"><a href="documentation.html#documentation_stability_index">Stability: 3</a> - Legacy. Use <a href="#process_process_hrtime_bigint"><code>process.hrtime.bigint()</code></a> instead.</div><p></p> 1415<ul> 1416<li><code>time</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer[]></a> The result of a previous call to <code>process.hrtime()</code></li> 1417<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer[]></a></li> 1418</ul> 1419<p>This is the legacy version of <a href="#process_process_hrtime_bigint"><code>process.hrtime.bigint()</code></a> 1420before <code>bigint</code> was introduced in JavaScript.</p> 1421<p>The <code>process.hrtime()</code> method returns the current high-resolution real time 1422in a <code>[seconds, nanoseconds]</code> tuple <code>Array</code>, where <code>nanoseconds</code> is the 1423remaining part of the real time that can't be represented in second precision.</p> 1424<p><code>time</code> is an optional parameter that must be the result of a previous 1425<code>process.hrtime()</code> call to diff with the current time. If the parameter 1426passed in is not a tuple <code>Array</code>, a <code>TypeError</code> will be thrown. Passing in a 1427user-defined array instead of the result of a previous call to 1428<code>process.hrtime()</code> will lead to undefined behavior.</p> 1429<p>These times are relative to an arbitrary time in the 1430past, and not related to the time of day and therefore not subject to clock 1431drift. The primary use is for measuring performance between intervals:</p> 1432<pre><code class="language-js"><span class="hljs-keyword">const</span> <span class="hljs-variable constant_">NS_PER_SEC</span> = <span class="hljs-number">1e9</span>; 1433<span class="hljs-keyword">const</span> time = process.<span class="hljs-title function_">hrtime</span>(); 1434<span class="hljs-comment">// [ 1800216, 25 ]</span> 1435 1436<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> { 1437 <span class="hljs-keyword">const</span> diff = process.<span class="hljs-title function_">hrtime</span>(time); 1438 <span class="hljs-comment">// [ 1, 552 ]</span> 1439 1440 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Benchmark took <span class="hljs-subst">${diff[<span class="hljs-number">0</span>] * NS_PER_SEC + diff[<span class="hljs-number">1</span>]}</span> nanoseconds`</span>); 1441 <span class="hljs-comment">// Benchmark took 1000000552 nanoseconds</span> 1442}, <span class="hljs-number">1000</span>);</code></pre> 1443</section><section><h3><code>process.hrtime.bigint()</code><span><a class="mark" href="#process_process_hrtime_bigint" id="process_process_hrtime_bigint">#</a></span></h3> 1444<div class="api_metadata"> 1445<span>Added in: v10.7.0</span> 1446</div> 1447<ul> 1448<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 1449</ul> 1450<p>The <code>bigint</code> version of the <a href="#process_process_hrtime_time"><code>process.hrtime()</code></a> method returning the 1451current high-resolution real time in nanoseconds as a <code>bigint</code>.</p> 1452<p>Unlike <a href="#process_process_hrtime_time"><code>process.hrtime()</code></a>, it does not support an additional <code>time</code> 1453argument since the difference can just be computed directly 1454by subtraction of the two <code>bigint</code>s.</p> 1455<pre><code class="language-js"><span class="hljs-keyword">const</span> start = process.<span class="hljs-property">hrtime</span>.<span class="hljs-title function_">bigint</span>(); 1456<span class="hljs-comment">// 191051479007711n</span> 1457 1458<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> { 1459 <span class="hljs-keyword">const</span> end = process.<span class="hljs-property">hrtime</span>.<span class="hljs-title function_">bigint</span>(); 1460 <span class="hljs-comment">// 191052633396993n</span> 1461 1462 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Benchmark took <span class="hljs-subst">${end - start}</span> nanoseconds`</span>); 1463 <span class="hljs-comment">// Benchmark took 1154389282 nanoseconds</span> 1464}, <span class="hljs-number">1000</span>);</code></pre> 1465</section><section><h3><code>process.initgroups(user, extraGroup)</code><span><a class="mark" href="#process_process_initgroups_user_extragroup" id="process_process_initgroups_user_extragroup">#</a></span></h3> 1466<div class="api_metadata"> 1467<span>Added in: v0.9.4</span> 1468</div> 1469<ul> 1470<li><code>user</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The user name or numeric identifier.</li> 1471<li><code>extraGroup</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> A group name or numeric identifier.</li> 1472</ul> 1473<p>The <code>process.initgroups()</code> method reads the <code>/etc/group</code> file and initializes 1474the group access list, using all groups of which the user is a member. This is 1475a privileged operation that requires that the Node.js process either have <code>root</code> 1476access or the <code>CAP_SETGID</code> capability.</p> 1477<p>Use care when dropping privileges:</p> 1478<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span class="hljs-title function_">getgroups</span>()); <span class="hljs-comment">// [ 0 ]</span> 1479process.<span class="hljs-title function_">initgroups</span>(<span class="hljs-string">'nodeuser'</span>, <span class="hljs-number">1000</span>); <span class="hljs-comment">// switch user</span> 1480<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span class="hljs-title function_">getgroups</span>()); <span class="hljs-comment">// [ 27, 30, 46, 1000, 0 ]</span> 1481process.<span class="hljs-title function_">setgid</span>(<span class="hljs-number">1000</span>); <span class="hljs-comment">// drop root gid</span> 1482<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span class="hljs-title function_">getgroups</span>()); <span class="hljs-comment">// [ 27, 30, 46, 1000 ]</span></code></pre> 1483<p>This function is only available on POSIX platforms (i.e. not Windows or 1484Android). 1485This feature is not available in <a href="worker_threads.html#worker_threads_class_worker"><code>Worker</code></a> threads.</p> 1486</section><section><h3><code>process.kill(pid[, signal])</code><span><a class="mark" href="#process_process_kill_pid_signal" id="process_process_kill_pid_signal">#</a></span></h3> 1487<div class="api_metadata"> 1488<span>Added in: v0.0.6</span> 1489</div> 1490<ul> 1491<li><code>pid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> A process ID</li> 1492<li><code>signal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The signal to send, either as a string or number. 1493<strong>Default:</strong> <code>'SIGTERM'</code>.</li> 1494</ul> 1495<p>The <code>process.kill()</code> method sends the <code>signal</code> to the process identified by 1496<code>pid</code>.</p> 1497<p>Signal names are strings such as <code>'SIGINT'</code> or <code>'SIGHUP'</code>. See <a href="#process_signal_events">Signal Events</a> 1498and <a href="http://man7.org/linux/man-pages/man2/kill.2.html"><code>kill(2)</code></a> for more information.</p> 1499<p>This method will throw an error if the target <code>pid</code> does not exist. As a special 1500case, a signal of <code>0</code> can be used to test for the existence of a process. 1501Windows platforms will throw an error if the <code>pid</code> is used to kill a process 1502group.</p> 1503<p>Even though the name of this function is <code>process.kill()</code>, it is really just a 1504signal sender, like the <code>kill</code> system call. The signal sent may do something 1505other than kill the target process.</p> 1506<pre><code class="language-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'SIGHUP'</span>, <span class="hljs-function">() =></span> { 1507 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Got SIGHUP signal.'</span>); 1508}); 1509 1510<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> { 1511 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Exiting.'</span>); 1512 process.<span class="hljs-title function_">exit</span>(<span class="hljs-number">0</span>); 1513}, <span class="hljs-number">100</span>); 1514 1515process.<span class="hljs-title function_">kill</span>(process.<span class="hljs-property">pid</span>, <span class="hljs-string">'SIGHUP'</span>);</code></pre> 1516<p>When <code>SIGUSR1</code> is received by a Node.js process, Node.js will start the 1517debugger. See <a href="#process_signal_events">Signal Events</a>.</p> 1518</section><section><h3><code>process.mainModule</code><span><a class="mark" href="#process_process_mainmodule" id="process_process_mainmodule">#</a></span></h3> 1519<div class="api_metadata"> 1520<span>Added in: v0.1.17</span><span>Deprecated since: v14.0.0</span> 1521</div> 1522<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="modules.html#modules_accessing_the_main_module"><code>require.main</code></a> instead.</div><p></p> 1523<ul> 1524<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 1525</ul> 1526<p>The <code>process.mainModule</code> property provides an alternative way of retrieving 1527<a href="modules.html#modules_accessing_the_main_module"><code>require.main</code></a>. The difference is that if the main module changes at 1528runtime, <a href="modules.html#modules_accessing_the_main_module"><code>require.main</code></a> may still refer to the original main module in 1529modules that were required before the change occurred. Generally, it's 1530safe to assume that the two refer to the same module.</p> 1531<p>As with <a href="modules.html#modules_accessing_the_main_module"><code>require.main</code></a>, <code>process.mainModule</code> will be <code>undefined</code> if there 1532is no entry script.</p> 1533</section><section><h3><code>process.memoryUsage()</code><span><a class="mark" href="#process_process_memoryusage" id="process_process_memoryusage">#</a></span></h3> 1534<div class="api_metadata"> 1535<details class="changelog"><summary>History</summary> 1536<table> 1537<tbody><tr><th>Version</th><th>Changes</th></tr> 1538<tr><td>v13.9.0</td> 1539<td><p>Added <code>arrayBuffers</code> to the returned object.</p></td></tr> 1540<tr><td>v7.2.0</td> 1541<td><p>Added <code>external</code> to the returned object.</p></td></tr> 1542<tr><td>v0.1.16</td> 1543<td><p><span>Added in: v0.1.16</span></p></td></tr> 1544</tbody></table> 1545</details> 1546</div> 1547<ul> 1548<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1549<ul> 1550<li><code>rss</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1551<li><code>heapTotal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1552<li><code>heapUsed</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1553<li><code>external</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1554<li><code>arrayBuffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1555</ul> 1556</li> 1557</ul> 1558<p>Returns an object describing the memory usage of the Node.js process measured in 1559bytes.</p> 1560<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span class="hljs-title function_">memoryUsage</span>()); 1561<span class="hljs-comment">// Prints:</span> 1562<span class="hljs-comment">// {</span> 1563<span class="hljs-comment">// rss: 4935680,</span> 1564<span class="hljs-comment">// heapTotal: 1826816,</span> 1565<span class="hljs-comment">// heapUsed: 650472,</span> 1566<span class="hljs-comment">// external: 49879,</span> 1567<span class="hljs-comment">// arrayBuffers: 9386</span> 1568<span class="hljs-comment">// }</span></code></pre> 1569<ul> 1570<li><code>heapTotal</code> and <code>heapUsed</code> refer to V8's memory usage.</li> 1571<li><code>external</code> refers to the memory usage of C++ objects bound to JavaScript 1572objects managed by V8.</li> 1573<li><code>rss</code>, Resident Set Size, is the amount of space occupied in the main 1574memory device (that is a subset of the total allocated memory) for the 1575process, including all C++ and JavaScript objects and code.</li> 1576<li><code>arrayBuffers</code> refers to memory allocated for <code>ArrayBuffer</code>s and 1577<code>SharedArrayBuffer</code>s, including all Node.js <a href="buffer.html"><code>Buffer</code></a>s. 1578This is also included in the <code>external</code> value. When Node.js is used as an 1579embedded library, this value may be <code>0</code> because allocations for <code>ArrayBuffer</code>s 1580may not be tracked in that case.</li> 1581</ul> 1582<p>When using <a href="worker_threads.html#worker_threads_class_worker"><code>Worker</code></a> threads, <code>rss</code> will be a value that is valid for the 1583entire process, while the other fields will only refer to the current thread.</p> 1584<p>The <code>process.memoryUsage()</code> method iterates over each page to gather 1585information about memory usage which might be slow depending on the 1586program memory allocations.</p> 1587</section><section><h3><code>process.memoryUsage.rss()</code><span><a class="mark" href="#process_process_memoryusage_rss" id="process_process_memoryusage_rss">#</a></span></h3> 1588<div class="api_metadata"> 1589<span>Added in: v14.18.0</span> 1590</div> 1591<ul> 1592<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1593</ul> 1594<p>The <code>process.memoryUsage.rss()</code> method returns an integer representing the 1595Resident Set Size (RSS) in bytes.</p> 1596<p>The Resident Set Size, is the amount of space occupied in the main 1597memory device (that is a subset of the total allocated memory) for the 1598process, including all C++ and JavaScript objects and code.</p> 1599<p>This is the same value as the <code>rss</code> property provided by <code>process.memoryUsage()</code> 1600but <code>process.memoryUsage.rss()</code> is faster.</p> 1601<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span class="hljs-property">memoryUsage</span>.<span class="hljs-title function_">rss</span>()); 1602<span class="hljs-comment">// 35655680</span></code></pre> 1603</section><section><h3><code>process.nextTick(callback[, ...args])</code><span><a class="mark" href="#process_process_nexttick_callback_args" id="process_process_nexttick_callback_args">#</a></span></h3> 1604<div class="api_metadata"> 1605<details class="changelog"><summary>History</summary> 1606<table> 1607<tbody><tr><th>Version</th><th>Changes</th></tr> 1608<tr><td>v1.8.1</td> 1609<td><p>Additional arguments after <code>callback</code> are now supported.</p></td></tr> 1610<tr><td>v0.1.26</td> 1611<td><p><span>Added in: v0.1.26</span></p></td></tr> 1612</tbody></table> 1613</details> 1614</div> 1615<ul> 1616<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 1617<li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> Additional arguments to pass when invoking the <code>callback</code></li> 1618</ul> 1619<p><code>process.nextTick()</code> adds <code>callback</code> to the "next tick queue". This queue is 1620fully drained after the current operation on the JavaScript stack runs to 1621completion and before the event loop is allowed to continue. It's possible to 1622create an infinite loop if one were to recursively call <code>process.nextTick()</code>. 1623See the <a href="https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#process-nexttick">Event Loop</a> guide for more background.</p> 1624<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'start'</span>); 1625process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> { 1626 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'nextTick callback'</span>); 1627}); 1628<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'scheduled'</span>); 1629<span class="hljs-comment">// Output:</span> 1630<span class="hljs-comment">// start</span> 1631<span class="hljs-comment">// scheduled</span> 1632<span class="hljs-comment">// nextTick callback</span></code></pre> 1633<p>This is important when developing APIs in order to give users the opportunity 1634to assign event handlers <em>after</em> an object has been constructed but before any 1635I/O has occurred:</p> 1636<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">MyThing</span>(<span class="hljs-params">options</span>) { 1637 <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">setupOptions</span>(options); 1638 1639 process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> { 1640 <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">startDoingStuff</span>(); 1641 }); 1642} 1643 1644<span class="hljs-keyword">const</span> thing = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyThing</span>(); 1645thing.<span class="hljs-title function_">getReadyForStuff</span>(); 1646 1647<span class="hljs-comment">// thing.startDoingStuff() gets called now, not before.</span></code></pre> 1648<p>It is very important for APIs to be either 100% synchronous or 100% 1649asynchronous. Consider this example:</p> 1650<pre><code class="language-js"><span class="hljs-comment">// WARNING! DO NOT USE! BAD UNSAFE HAZARD!</span> 1651<span class="hljs-keyword">function</span> <span class="hljs-title function_">maybeSync</span>(<span class="hljs-params">arg, cb</span>) { 1652 <span class="hljs-keyword">if</span> (arg) { 1653 <span class="hljs-title function_">cb</span>(); 1654 <span class="hljs-keyword">return</span>; 1655 } 1656 1657 fs.<span class="hljs-title function_">stat</span>(<span class="hljs-string">'file'</span>, cb); 1658}</code></pre> 1659<p>This API is hazardous because in the following case:</p> 1660<pre><code class="language-js"><span class="hljs-keyword">const</span> maybeTrue = <span class="hljs-title class_">Math</span>.<span class="hljs-title function_">random</span>() > <span class="hljs-number">0.5</span>; 1661 1662<span class="hljs-title function_">maybeSync</span>(maybeTrue, <span class="hljs-function">() =></span> { 1663 <span class="hljs-title function_">foo</span>(); 1664}); 1665 1666<span class="hljs-title function_">bar</span>();</code></pre> 1667<p>It is not clear whether <code>foo()</code> or <code>bar()</code> will be called first.</p> 1668<p>The following approach is much better:</p> 1669<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">definitelyAsync</span>(<span class="hljs-params">arg, cb</span>) { 1670 <span class="hljs-keyword">if</span> (arg) { 1671 process.<span class="hljs-title function_">nextTick</span>(cb); 1672 <span class="hljs-keyword">return</span>; 1673 } 1674 1675 fs.<span class="hljs-title function_">stat</span>(<span class="hljs-string">'file'</span>, cb); 1676}</code></pre> 1677<h4>When to use <code>queueMicrotask()</code> vs. <code>process.nextTick()</code><span><a class="mark" href="#process_when_to_use_queuemicrotask_vs_process_nexttick" id="process_when_to_use_queuemicrotask_vs_process_nexttick">#</a></span></h4> 1678<p>The <a href="globals.html#globals_queuemicrotask_callback"><code>queueMicrotask()</code></a> API is an alternative to <code>process.nextTick()</code> that 1679also defers execution of a function using the same microtask queue used to 1680execute the then, catch, and finally handlers of resolved promises. Within 1681Node.js, every time the "next tick queue" is drained, the microtask queue 1682is drained immediately after.</p> 1683<pre><code class="language-js"><span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">resolve</span>().<span class="hljs-title function_">then</span>(<span class="hljs-function">() =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-number">2</span>)); 1684<span class="hljs-title function_">queueMicrotask</span>(<span class="hljs-function">() =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-number">3</span>)); 1685process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-number">1</span>)); 1686<span class="hljs-comment">// Output:</span> 1687<span class="hljs-comment">// 1</span> 1688<span class="hljs-comment">// 2</span> 1689<span class="hljs-comment">// 3</span></code></pre> 1690<p>For <em>most</em> userland use cases, the <code>queueMicrotask()</code> API provides a portable 1691and reliable mechanism for deferring execution that works across multiple 1692JavaScript platform environments and should be favored over <code>process.nextTick()</code>. 1693In simple scenarios, <code>queueMicrotask()</code> can be a drop-in replacement for 1694<code>process.nextTick()</code>.</p> 1695<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'start'</span>); 1696<span class="hljs-title function_">queueMicrotask</span>(<span class="hljs-function">() =></span> { 1697 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'microtask callback'</span>); 1698}); 1699<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'scheduled'</span>); 1700<span class="hljs-comment">// Output:</span> 1701<span class="hljs-comment">// start</span> 1702<span class="hljs-comment">// scheduled</span> 1703<span class="hljs-comment">// microtask callback</span></code></pre> 1704<p>One note-worthy difference between the two APIs is that <code>process.nextTick()</code> 1705allows specifying additional values that will be passed as arguments to the 1706deferred function when it is called. Achieving the same result with 1707<code>queueMicrotask()</code> requires using either a closure or a bound function:</p> 1708<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">deferred</span>(<span class="hljs-params">a, b</span>) { 1709 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'microtask'</span>, a + b); 1710} 1711 1712<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'start'</span>); 1713<span class="hljs-title function_">queueMicrotask</span>(deferred.<span class="hljs-title function_">bind</span>(<span class="hljs-literal">undefined</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>)); 1714<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'scheduled'</span>); 1715<span class="hljs-comment">// Output:</span> 1716<span class="hljs-comment">// start</span> 1717<span class="hljs-comment">// scheduled</span> 1718<span class="hljs-comment">// microtask 3</span></code></pre> 1719<p>There are minor differences in the way errors raised from within the next tick 1720queue and microtask queue are handled. Errors thrown within a queued microtask 1721callback should be handled within the queued callback when possible. If they are 1722not, the <code>process.on('uncaughtException')</code> event handler can be used to capture 1723and handle the errors.</p> 1724<p>When in doubt, unless the specific capabilities of <code>process.nextTick()</code> are 1725needed, use <code>queueMicrotask()</code>.</p> 1726</section><section><h3><code>process.noDeprecation</code><span><a class="mark" href="#process_process_nodeprecation" id="process_process_nodeprecation">#</a></span></h3> 1727<div class="api_metadata"> 1728<span>Added in: v0.8.0</span> 1729</div> 1730<ul> 1731<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1732</ul> 1733<p>The <code>process.noDeprecation</code> property indicates whether the <code>--no-deprecation</code> 1734flag is set on the current Node.js process. See the documentation for 1735the <a href="#process_event_warning"><code>'warning'</code> event</a> and the 1736<a href="#process_process_emitwarning_warning_type_code_ctor"><code>emitWarning()</code> method</a> for more information about this 1737flag's behavior.</p> 1738</section><section><h3><code>process.pid</code><span><a class="mark" href="#process_process_pid" id="process_process_pid">#</a></span></h3> 1739<div class="api_metadata"> 1740<span>Added in: v0.1.15</span> 1741</div> 1742<ul> 1743<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1744</ul> 1745<p>The <code>process.pid</code> property returns the PID of the process.</p> 1746<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`This process is pid <span class="hljs-subst">${process.pid}</span>`</span>);</code></pre> 1747</section><section><h3><code>process.platform</code><span><a class="mark" href="#process_process_platform" id="process_process_platform">#</a></span></h3> 1748<div class="api_metadata"> 1749<span>Added in: v0.1.16</span> 1750</div> 1751<ul> 1752<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1753</ul> 1754<p>The <code>process.platform</code> property returns a string identifying the operating 1755system platform on which the Node.js process is running.</p> 1756<p>Currently possible values are:</p> 1757<ul> 1758<li><code>'aix'</code></li> 1759<li><code>'darwin'</code></li> 1760<li><code>'freebsd'</code></li> 1761<li><code>'linux'</code></li> 1762<li><code>'openbsd'</code></li> 1763<li><code>'sunos'</code></li> 1764<li><code>'win32'</code></li> 1765</ul> 1766<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`This platform is <span class="hljs-subst">${process.platform}</span>`</span>);</code></pre> 1767<p>The value <code>'android'</code> may also be returned if the Node.js is built on the 1768Android operating system. However, Android support in Node.js 1769<a href="https://github.com/nodejs/node/blob/HEAD/BUILDING.md#androidandroid-based-devices-eg-firefox-os">is experimental</a>.</p> 1770</section><section><h3><code>process.ppid</code><span><a class="mark" href="#process_process_ppid" id="process_process_ppid">#</a></span></h3> 1771<div class="api_metadata"> 1772<span>Added in: v9.2.0, v8.10.0, v6.13.0</span> 1773</div> 1774<ul> 1775<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1776</ul> 1777<p>The <code>process.ppid</code> property returns the PID of the parent of the 1778current process.</p> 1779<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`The parent process is pid <span class="hljs-subst">${process.ppid}</span>`</span>);</code></pre> 1780</section><section><h3><code>process.release</code><span><a class="mark" href="#process_process_release" id="process_process_release">#</a></span></h3> 1781<div class="api_metadata"> 1782<details class="changelog"><summary>History</summary> 1783<table> 1784<tbody><tr><th>Version</th><th>Changes</th></tr> 1785<tr><td>v4.2.0</td> 1786<td><p>The <code>lts</code> property is now supported.</p></td></tr> 1787<tr><td>v3.0.0</td> 1788<td><p><span>Added in: v3.0.0</span></p></td></tr> 1789</tbody></table> 1790</details> 1791</div> 1792<ul> 1793<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 1794</ul> 1795<p>The <code>process.release</code> property returns an <code>Object</code> containing metadata related 1796to the current release, including URLs for the source tarball and headers-only 1797tarball.</p> 1798<p><code>process.release</code> contains the following properties:</p> 1799<ul> 1800<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A value that will always be <code>'node'</code>.</li> 1801<li><code>sourceUrl</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> an absolute URL pointing to a <em><code>.tar.gz</code></em> file containing 1802the source code of the current release.</li> 1803<li><code>headersUrl</code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> an absolute URL pointing to a <em><code>.tar.gz</code></em> file containing 1804only the source header files for the current release. This file is 1805significantly smaller than the full source file and can be used for compiling 1806Node.js native add-ons.</li> 1807<li><code>libUrl</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> an absolute URL pointing to a <em><code>node.lib</code></em> file matching the 1808architecture and version of the current release. This file is used for 1809compiling Node.js native add-ons. <em>This property is only present on Windows 1810builds of Node.js and will be missing on all other platforms.</em></li> 1811<li><code>lts</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> a string label identifying the <a href="https://github.com/nodejs/Release">LTS</a> label for this release. 1812This property only exists for LTS releases and is <code>undefined</code> for all other 1813release types, including <em>Current</em> releases. 1814Valid values include the LTS Release code names (including those 1815that are no longer supported). 1816<ul> 1817<li><code>'Dubnium'</code> for the 10.x LTS line beginning with 10.13.0.</li> 1818<li><code>'Erbium'</code> for the 12.x LTS line beginning with 12.13.0.</li> 1819</ul> 1820For other LTS Release code names, see <a href="https://github.com/nodejs/node/blob/HEAD/doc/changelogs/CHANGELOG_ARCHIVE.md">Node.js Changelog Archive</a></li> 1821</ul> 1822<!-- eslint-skip --> 1823<pre><code class="language-js">{ 1824 <span class="hljs-attr">name</span>: <span class="hljs-string">'node'</span>, 1825 <span class="hljs-attr">lts</span>: <span class="hljs-string">'Erbium'</span>, 1826 <span class="hljs-attr">sourceUrl</span>: <span class="hljs-string">'https://nodejs.org/download/release/v12.18.1/node-v12.18.1.tar.gz'</span>, 1827 <span class="hljs-attr">headersUrl</span>: <span class="hljs-string">'https://nodejs.org/download/release/v12.18.1/node-v12.18.1-headers.tar.gz'</span>, 1828 <span class="hljs-attr">libUrl</span>: <span class="hljs-string">'https://nodejs.org/download/release/v12.18.1/win-x64/node.lib'</span> 1829}</code></pre> 1830<p>In custom builds from non-release versions of the source tree, only the 1831<code>name</code> property may be present. The additional properties should not be 1832relied upon to exist.</p> 1833</section><section><h3><code>process.report</code><span><a class="mark" href="#process_process_report" id="process_process_report">#</a></span></h3> 1834<div class="api_metadata"> 1835<details class="changelog"><summary>History</summary> 1836<table> 1837<tbody><tr><th>Version</th><th>Changes</th></tr> 1838<tr><td>v13.12.0</td> 1839<td><p>This API is no longer experimental.</p></td></tr> 1840<tr><td>v11.8.0</td> 1841<td><p><span>Added in: v11.8.0</span></p></td></tr> 1842</tbody></table> 1843</details> 1844</div> 1845<ul> 1846<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 1847</ul> 1848<p><code>process.report</code> is an object whose methods are used to generate diagnostic 1849reports for the current process. Additional documentation is available in the 1850<a href="report.html">report documentation</a>.</p> 1851<h4><code>process.report.compact</code><span><a class="mark" href="#process_process_report_compact" id="process_process_report_compact">#</a></span></h4> 1852<div class="api_metadata"> 1853<span>Added in: v13.12.0</span> 1854</div> 1855<ul> 1856<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1857</ul> 1858<p>Write reports in a compact format, single-line JSON, more easily consumable 1859by log processing systems than the default multi-line format designed for 1860human consumption.</p> 1861<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Reports are compact? <span class="hljs-subst">${process.report.compact}</span>`</span>);</code></pre> 1862<h4><code>process.report.directory</code><span><a class="mark" href="#process_process_report_directory" id="process_process_report_directory">#</a></span></h4> 1863<div class="api_metadata"> 1864<details class="changelog"><summary>History</summary> 1865<table> 1866<tbody><tr><th>Version</th><th>Changes</th></tr> 1867<tr><td>v13.12.0</td> 1868<td><p>This API is no longer experimental.</p></td></tr> 1869<tr><td>v11.12.0</td> 1870<td><p><span>Added in: v11.12.0</span></p></td></tr> 1871</tbody></table> 1872</details> 1873</div> 1874<ul> 1875<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1876</ul> 1877<p>Directory where the report is written. The default value is the empty string, 1878indicating that reports are written to the current working directory of the 1879Node.js process.</p> 1880<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Report directory is <span class="hljs-subst">${process.report.directory}</span>`</span>);</code></pre> 1881<h4><code>process.report.filename</code><span><a class="mark" href="#process_process_report_filename" id="process_process_report_filename">#</a></span></h4> 1882<div class="api_metadata"> 1883<details class="changelog"><summary>History</summary> 1884<table> 1885<tbody><tr><th>Version</th><th>Changes</th></tr> 1886<tr><td>v13.12.0</td> 1887<td><p>This API is no longer experimental.</p></td></tr> 1888<tr><td>v11.12.0</td> 1889<td><p><span>Added in: v11.12.0</span></p></td></tr> 1890</tbody></table> 1891</details> 1892</div> 1893<ul> 1894<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1895</ul> 1896<p>Filename where the report is written. If set to the empty string, the output 1897filename will be comprised of a timestamp, PID, and sequence number. The default 1898value is the empty string.</p> 1899<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Report filename is <span class="hljs-subst">${process.report.filename}</span>`</span>);</code></pre> 1900<h4><code>process.report.getReport([err])</code><span><a class="mark" href="#process_process_report_getreport_err" id="process_process_report_getreport_err">#</a></span></h4> 1901<div class="api_metadata"> 1902<details class="changelog"><summary>History</summary> 1903<table> 1904<tbody><tr><th>Version</th><th>Changes</th></tr> 1905<tr><td>v13.12.0</td> 1906<td><p>This API is no longer experimental.</p></td></tr> 1907<tr><td>v11.8.0</td> 1908<td><p><span>Added in: v11.8.0</span></p></td></tr> 1909</tbody></table> 1910</details> 1911</div> 1912<ul> 1913<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> A custom error used for reporting the JavaScript stack.</li> 1914<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 1915</ul> 1916<p>Returns a JavaScript Object representation of a diagnostic report for the 1917running process. The report's JavaScript stack trace is taken from <code>err</code>, if 1918present.</p> 1919<pre><code class="language-js"><span class="hljs-keyword">const</span> data = process.<span class="hljs-property">report</span>.<span class="hljs-title function_">getReport</span>(); 1920<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(data.<span class="hljs-property">header</span>.<span class="hljs-property">nodejsVersion</span>); 1921 1922<span class="hljs-comment">// Similar to process.report.writeReport()</span> 1923<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>); 1924fs.<span class="hljs-title function_">writeFileSync</span>(<span class="hljs-string">'my-report.log'</span>, util.<span class="hljs-title function_">inspect</span>(data), <span class="hljs-string">'utf8'</span>);</code></pre> 1925<p>Additional documentation is available in the <a href="report.html">report documentation</a>.</p> 1926<h4><code>process.report.reportOnFatalError</code><span><a class="mark" href="#process_process_report_reportonfatalerror" id="process_process_report_reportonfatalerror">#</a></span></h4> 1927<div class="api_metadata"> 1928<details class="changelog"><summary>History</summary> 1929<table> 1930<tbody><tr><th>Version</th><th>Changes</th></tr> 1931<tr><td>v14.17.0</td> 1932<td><p>This API is no longer experimental.</p></td></tr> 1933<tr><td>v11.12.0</td> 1934<td><p><span>Added in: v11.12.0</span></p></td></tr> 1935</tbody></table> 1936</details> 1937</div> 1938<ul> 1939<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1940</ul> 1941<p>If <code>true</code>, a diagnostic report is generated on fatal errors, such as out of 1942memory errors or failed C++ assertions.</p> 1943<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Report on fatal error: <span class="hljs-subst">${process.report.reportOnFatalError}</span>`</span>);</code></pre> 1944<h4><code>process.report.reportOnSignal</code><span><a class="mark" href="#process_process_report_reportonsignal" id="process_process_report_reportonsignal">#</a></span></h4> 1945<div class="api_metadata"> 1946<details class="changelog"><summary>History</summary> 1947<table> 1948<tbody><tr><th>Version</th><th>Changes</th></tr> 1949<tr><td>v13.12.0</td> 1950<td><p>This API is no longer experimental.</p></td></tr> 1951<tr><td>v11.12.0</td> 1952<td><p><span>Added in: v11.12.0</span></p></td></tr> 1953</tbody></table> 1954</details> 1955</div> 1956<ul> 1957<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1958</ul> 1959<p>If <code>true</code>, a diagnostic report is generated when the process receives the 1960signal specified by <code>process.report.signal</code>.</p> 1961<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Report on signal: <span class="hljs-subst">${process.report.reportOnSignal}</span>`</span>);</code></pre> 1962<h4><code>process.report.reportOnUncaughtException</code><span><a class="mark" href="#process_process_report_reportonuncaughtexception" id="process_process_report_reportonuncaughtexception">#</a></span></h4> 1963<div class="api_metadata"> 1964<details class="changelog"><summary>History</summary> 1965<table> 1966<tbody><tr><th>Version</th><th>Changes</th></tr> 1967<tr><td>v13.12.0</td> 1968<td><p>This API is no longer experimental.</p></td></tr> 1969<tr><td>v11.12.0</td> 1970<td><p><span>Added in: v11.12.0</span></p></td></tr> 1971</tbody></table> 1972</details> 1973</div> 1974<ul> 1975<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1976</ul> 1977<p>If <code>true</code>, a diagnostic report is generated on uncaught exception.</p> 1978<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Report on exception: <span class="hljs-subst">${process.report.reportOnUncaughtException}</span>`</span>);</code></pre> 1979<h4><code>process.report.signal</code><span><a class="mark" href="#process_process_report_signal" id="process_process_report_signal">#</a></span></h4> 1980<div class="api_metadata"> 1981<details class="changelog"><summary>History</summary> 1982<table> 1983<tbody><tr><th>Version</th><th>Changes</th></tr> 1984<tr><td>v13.12.0</td> 1985<td><p>This API is no longer experimental.</p></td></tr> 1986<tr><td>v11.12.0</td> 1987<td><p><span>Added in: v11.12.0</span></p></td></tr> 1988</tbody></table> 1989</details> 1990</div> 1991<ul> 1992<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1993</ul> 1994<p>The signal used to trigger the creation of a diagnostic report. Defaults to 1995<code>'SIGUSR2'</code>.</p> 1996<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Report signal: <span class="hljs-subst">${process.report.signal}</span>`</span>);</code></pre> 1997<h4><code>process.report.writeReport([filename][, err])</code><span><a class="mark" href="#process_process_report_writereport_filename_err" id="process_process_report_writereport_filename_err">#</a></span></h4> 1998<div class="api_metadata"> 1999<details class="changelog"><summary>History</summary> 2000<table> 2001<tbody><tr><th>Version</th><th>Changes</th></tr> 2002<tr><td>v13.12.0</td> 2003<td><p>This API is no longer experimental.</p></td></tr> 2004<tr><td>v11.8.0</td> 2005<td><p><span>Added in: v11.8.0</span></p></td></tr> 2006</tbody></table> 2007</details> 2008</div> 2009<ul> 2010<li> 2011<p><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Name of the file where the report is written. This 2012should be a relative path, that will be appended to the directory specified in 2013<code>process.report.directory</code>, or the current working directory of the Node.js 2014process, if unspecified.</p> 2015</li> 2016<li> 2017<p><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> A custom error used for reporting the JavaScript stack.</p> 2018</li> 2019<li> 2020<p>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Returns the filename of the generated report.</p> 2021</li> 2022</ul> 2023<p>Writes a diagnostic report to a file. If <code>filename</code> is not provided, the default 2024filename includes the date, time, PID, and a sequence number. The report's 2025JavaScript stack trace is taken from <code>err</code>, if present.</p> 2026<pre><code class="language-js">process.<span class="hljs-property">report</span>.<span class="hljs-title function_">writeReport</span>();</code></pre> 2027<p>Additional documentation is available in the <a href="report.html">report documentation</a>.</p> 2028</section><section><h3><code>process.resourceUsage()</code><span><a class="mark" href="#process_process_resourceusage" id="process_process_resourceusage">#</a></span></h3> 2029<div class="api_metadata"> 2030<span>Added in: v12.6.0</span> 2031</div> 2032<ul> 2033<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> the resource usage for the current process. All of these 2034values come from the <code>uv_getrusage</code> call which returns 2035a <a href="https://docs.libuv.org/en/v1.x/misc.html#c.uv_rusage_t"><code>uv_rusage_t</code> struct</a>. 2036<ul> 2037<li><code>userCPUTime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> maps to <code>ru_utime</code> computed in microseconds. 2038It is the same value as <a href="#process_process_cpuusage_previousvalue"><code>process.cpuUsage().user</code></a>.</li> 2039<li><code>systemCPUTime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> maps to <code>ru_stime</code> computed in microseconds. 2040It is the same value as <a href="#process_process_cpuusage_previousvalue"><code>process.cpuUsage().system</code></a>.</li> 2041<li><code>maxRSS</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> maps to <code>ru_maxrss</code> which is the maximum resident set 2042size used in kilobytes.</li> 2043<li><code>sharedMemorySize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> maps to <code>ru_ixrss</code> but is not supported by 2044any platform.</li> 2045<li><code>unsharedDataSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> maps to <code>ru_idrss</code> but is not supported by 2046any platform.</li> 2047<li><code>unsharedStackSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> maps to <code>ru_isrss</code> but is not supported by 2048any platform.</li> 2049<li><code>minorPageFault</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> maps to <code>ru_minflt</code> which is the number of 2050minor page faults for the process, see 2051<a href="https://en.wikipedia.org/wiki/Page_fault#Minor">this article for more details</a>.</li> 2052<li><code>majorPageFault</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> maps to <code>ru_majflt</code> which is the number of 2053major page faults for the process, see 2054<a href="https://en.wikipedia.org/wiki/Page_fault#Major">this article for more details</a>. This field is not 2055supported on Windows.</li> 2056<li><code>swappedOut</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> maps to <code>ru_nswap</code> but is not supported by any 2057platform.</li> 2058<li><code>fsRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> maps to <code>ru_inblock</code> which is the number of times the 2059file system had to perform input.</li> 2060<li><code>fsWrite</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> maps to <code>ru_oublock</code> which is the number of times the 2061file system had to perform output.</li> 2062<li><code>ipcSent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> maps to <code>ru_msgsnd</code> but is not supported by any 2063platform.</li> 2064<li><code>ipcReceived</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> maps to <code>ru_msgrcv</code> but is not supported by any 2065platform.</li> 2066<li><code>signalsCount</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> maps to <code>ru_nsignals</code> but is not supported by any 2067platform.</li> 2068<li><code>voluntaryContextSwitches</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> maps to <code>ru_nvcsw</code> which is the 2069number of times a CPU context switch resulted due to a process voluntarily 2070giving up the processor before its time slice was completed (usually to 2071await availability of a resource). This field is not supported on Windows.</li> 2072<li><code>involuntaryContextSwitches</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> maps to <code>ru_nivcsw</code> which is the 2073number of times a CPU context switch resulted due to a higher priority 2074process becoming runnable or because the current process exceeded its 2075time slice. This field is not supported on Windows.</li> 2076</ul> 2077</li> 2078</ul> 2079<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span class="hljs-title function_">resourceUsage</span>()); 2080<span class="hljs-comment">/* 2081 Will output: 2082 { 2083 userCPUTime: 82872, 2084 systemCPUTime: 4143, 2085 maxRSS: 33164, 2086 sharedMemorySize: 0, 2087 unsharedDataSize: 0, 2088 unsharedStackSize: 0, 2089 minorPageFault: 2469, 2090 majorPageFault: 0, 2091 swappedOut: 0, 2092 fsRead: 0, 2093 fsWrite: 8, 2094 ipcSent: 0, 2095 ipcReceived: 0, 2096 signalsCount: 0, 2097 voluntaryContextSwitches: 79, 2098 involuntaryContextSwitches: 1 2099 } 2100*/</span></code></pre> 2101</section><section><h3><code>process.send(message[, sendHandle[, options]][, callback])</code><span><a class="mark" href="#process_process_send_message_sendhandle_options_callback" id="process_process_send_message_sendhandle_options_callback">#</a></span></h3> 2102<div class="api_metadata"> 2103<span>Added in: v0.5.9</span> 2104</div> 2105<ul> 2106<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 2107<li><code>sendHandle</code> <a href="net.html#net_class_net_server" class="type"><net.Server></a> | <a href="net.html#net_class_net_socket" class="type"><net.Socket></a></li> 2108<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> used to parameterize the sending of certain types of 2109handles.<code>options</code> supports the following properties: 2110<ul> 2111<li><code>keepOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> A value that can be used when passing instances of 2112<code>net.Socket</code>. When <code>true</code>, the socket is kept open in the sending process. 2113<strong>Default:</strong> <code>false</code>.</li> 2114</ul> 2115</li> 2116<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 2117<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2118</ul> 2119<p>If Node.js is spawned with an IPC channel, the <code>process.send()</code> method can be 2120used to send messages to the parent process. Messages will be received as a 2121<a href="child_process.html#child_process_event_message"><code>'message'</code></a> event on the parent's <a href="child_process.html#child_process_class_childprocess"><code>ChildProcess</code></a> object.</p> 2122<p>If Node.js was not spawned with an IPC channel, <code>process.send</code> will be 2123<code>undefined</code>.</p> 2124<p>The message goes through serialization and parsing. The resulting message might 2125not be the same as what is originally sent.</p> 2126</section><section><h3><code>process.setegid(id)</code><span><a class="mark" href="#process_process_setegid_id" id="process_process_setegid_id">#</a></span></h3> 2127<div class="api_metadata"> 2128<span>Added in: v2.0.0</span> 2129</div> 2130<ul> 2131<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> A group name or ID</li> 2132</ul> 2133<p>The <code>process.setegid()</code> method sets the effective group identity of the process. 2134(See <a href="http://man7.org/linux/man-pages/man2/setegid.2.html"><code>setegid(2)</code></a>.) The <code>id</code> can be passed as either a numeric ID or a group 2135name string. If a group name is specified, this method blocks while resolving 2136the associated a numeric ID.</p> 2137<pre><code class="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-property">getegid</span> && process.<span class="hljs-property">setegid</span>) { 2138 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Current gid: <span class="hljs-subst">${process.getegid()}</span>`</span>); 2139 <span class="hljs-keyword">try</span> { 2140 process.<span class="hljs-title function_">setegid</span>(<span class="hljs-number">501</span>); 2141 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`New gid: <span class="hljs-subst">${process.getegid()}</span>`</span>); 2142 } <span class="hljs-keyword">catch</span> (err) { 2143 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Failed to set gid: <span class="hljs-subst">${err}</span>`</span>); 2144 } 2145}</code></pre> 2146<p>This function is only available on POSIX platforms (i.e. not Windows or 2147Android). 2148This feature is not available in <a href="worker_threads.html#worker_threads_class_worker"><code>Worker</code></a> threads.</p> 2149</section><section><h3><code>process.seteuid(id)</code><span><a class="mark" href="#process_process_seteuid_id" id="process_process_seteuid_id">#</a></span></h3> 2150<div class="api_metadata"> 2151<span>Added in: v2.0.0</span> 2152</div> 2153<ul> 2154<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> A user name or ID</li> 2155</ul> 2156<p>The <code>process.seteuid()</code> method sets the effective user identity of the process. 2157(See <a href="http://man7.org/linux/man-pages/man2/seteuid.2.html"><code>seteuid(2)</code></a>.) The <code>id</code> can be passed as either a numeric ID or a username 2158string. If a username is specified, the method blocks while resolving the 2159associated numeric ID.</p> 2160<pre><code class="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-property">geteuid</span> && process.<span class="hljs-property">seteuid</span>) { 2161 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Current uid: <span class="hljs-subst">${process.geteuid()}</span>`</span>); 2162 <span class="hljs-keyword">try</span> { 2163 process.<span class="hljs-title function_">seteuid</span>(<span class="hljs-number">501</span>); 2164 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`New uid: <span class="hljs-subst">${process.geteuid()}</span>`</span>); 2165 } <span class="hljs-keyword">catch</span> (err) { 2166 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Failed to set uid: <span class="hljs-subst">${err}</span>`</span>); 2167 } 2168}</code></pre> 2169<p>This function is only available on POSIX platforms (i.e. not Windows or 2170Android). 2171This feature is not available in <a href="worker_threads.html#worker_threads_class_worker"><code>Worker</code></a> threads.</p> 2172</section><section><h3><code>process.setgid(id)</code><span><a class="mark" href="#process_process_setgid_id" id="process_process_setgid_id">#</a></span></h3> 2173<div class="api_metadata"> 2174<span>Added in: v0.1.31</span> 2175</div> 2176<ul> 2177<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The group name or ID</li> 2178</ul> 2179<p>The <code>process.setgid()</code> method sets the group identity of the process. (See 2180<a href="http://man7.org/linux/man-pages/man2/setgid.2.html"><code>setgid(2)</code></a>.) The <code>id</code> can be passed as either a numeric ID or a group name 2181string. If a group name is specified, this method blocks while resolving the 2182associated numeric ID.</p> 2183<pre><code class="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-property">getgid</span> && process.<span class="hljs-property">setgid</span>) { 2184 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Current gid: <span class="hljs-subst">${process.getgid()}</span>`</span>); 2185 <span class="hljs-keyword">try</span> { 2186 process.<span class="hljs-title function_">setgid</span>(<span class="hljs-number">501</span>); 2187 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`New gid: <span class="hljs-subst">${process.getgid()}</span>`</span>); 2188 } <span class="hljs-keyword">catch</span> (err) { 2189 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Failed to set gid: <span class="hljs-subst">${err}</span>`</span>); 2190 } 2191}</code></pre> 2192<p>This function is only available on POSIX platforms (i.e. not Windows or 2193Android). 2194This feature is not available in <a href="worker_threads.html#worker_threads_class_worker"><code>Worker</code></a> threads.</p> 2195</section><section><h3><code>process.setgroups(groups)</code><span><a class="mark" href="#process_process_setgroups_groups" id="process_process_setgroups_groups">#</a></span></h3> 2196<div class="api_metadata"> 2197<span>Added in: v0.9.4</span> 2198</div> 2199<ul> 2200<li><code>groups</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer[]></a></li> 2201</ul> 2202<p>The <code>process.setgroups()</code> method sets the supplementary group IDs for the 2203Node.js process. This is a privileged operation that requires the Node.js 2204process to have <code>root</code> or the <code>CAP_SETGID</code> capability.</p> 2205<p>The <code>groups</code> array can contain numeric group IDs, group names, or both.</p> 2206<pre><code class="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-property">getgroups</span> && process.<span class="hljs-property">setgroups</span>) { 2207 <span class="hljs-keyword">try</span> { 2208 process.<span class="hljs-title function_">setgroups</span>([<span class="hljs-number">501</span>]); 2209 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span class="hljs-title function_">getgroups</span>()); <span class="hljs-comment">// new groups</span> 2210 } <span class="hljs-keyword">catch</span> (err) { 2211 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Failed to set groups: <span class="hljs-subst">${err}</span>`</span>); 2212 } 2213}</code></pre> 2214<p>This function is only available on POSIX platforms (i.e. not Windows or 2215Android). 2216This feature is not available in <a href="worker_threads.html#worker_threads_class_worker"><code>Worker</code></a> threads.</p> 2217</section><section><h3><code>process.setuid(id)</code><span><a class="mark" href="#process_process_setuid_id" id="process_process_setuid_id">#</a></span></h3> 2218<div class="api_metadata"> 2219<span>Added in: v0.1.28</span> 2220</div> 2221<ul> 2222<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2223</ul> 2224<p>The <code>process.setuid(id)</code> method sets the user identity of the process. (See 2225<a href="http://man7.org/linux/man-pages/man2/setuid.2.html"><code>setuid(2)</code></a>.) The <code>id</code> can be passed as either a numeric ID or a username string. 2226If a username is specified, the method blocks while resolving the associated 2227numeric ID.</p> 2228<pre><code class="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-property">getuid</span> && process.<span class="hljs-property">setuid</span>) { 2229 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Current uid: <span class="hljs-subst">${process.getuid()}</span>`</span>); 2230 <span class="hljs-keyword">try</span> { 2231 process.<span class="hljs-title function_">setuid</span>(<span class="hljs-number">501</span>); 2232 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`New uid: <span class="hljs-subst">${process.getuid()}</span>`</span>); 2233 } <span class="hljs-keyword">catch</span> (err) { 2234 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Failed to set uid: <span class="hljs-subst">${err}</span>`</span>); 2235 } 2236}</code></pre> 2237<p>This function is only available on POSIX platforms (i.e. not Windows or 2238Android). 2239This feature is not available in <a href="worker_threads.html#worker_threads_class_worker"><code>Worker</code></a> threads.</p> 2240</section><section><h3><code>process.setSourceMapsEnabled(val)</code><span><a class="mark" href="#process_process_setsourcemapsenabled_val" id="process_process_setsourcemapsenabled_val">#</a></span></h3> 2241<div class="api_metadata"> 2242<span>Added in: v14.18.0</span> 2243</div> 2244<p></p><div class="api_stability api_stability_1"><a href="documentation.html#documentation_stability_index">Stability: 1</a> - Experimental</div><p></p> 2245<ul> 2246<li><code>val</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2247</ul> 2248<p>This function enables or disables the <a href="https://sourcemaps.info/spec.html">Source Map v3</a> support for 2249stack traces.</p> 2250<p>It provides same features as launching Node.js process with commandline options 2251<code>--enable-source-maps</code>.</p> 2252<p>Only source maps in JavaScript files that are loaded after source maps has been 2253enabled will be parsed and loaded.</p> 2254</section><section><h3><code>process.setUncaughtExceptionCaptureCallback(fn)</code><span><a class="mark" href="#process_process_setuncaughtexceptioncapturecallback_fn" id="process_process_setuncaughtexceptioncapturecallback_fn">#</a></span></h3> 2255<div class="api_metadata"> 2256<span>Added in: v9.3.0</span> 2257</div> 2258<ul> 2259<li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a></li> 2260</ul> 2261<p>The <code>process.setUncaughtExceptionCaptureCallback()</code> function sets a function 2262that will be invoked when an uncaught exception occurs, which will receive the 2263exception value itself as its first argument.</p> 2264<p>If such a function is set, the <a href="#process_event_uncaughtexception"><code>'uncaughtException'</code></a> event will 2265not be emitted. If <code>--abort-on-uncaught-exception</code> was passed from the 2266command line or set through <a href="v8.html#v8_v8_setflagsfromstring_flags"><code>v8.setFlagsFromString()</code></a>, the process will 2267not abort. Actions configured to take place on exceptions such as report 2268generations will be affected too</p> 2269<p>To unset the capture function, 2270<code>process.setUncaughtExceptionCaptureCallback(null)</code> may be used. Calling this 2271method with a non-<code>null</code> argument while another capture function is set will 2272throw an error.</p> 2273<p>Using this function is mutually exclusive with using the deprecated 2274<a href="domain.html"><code>domain</code></a> built-in module.</p> 2275</section><section><h3><code>process.stderr</code><span><a class="mark" href="#process_process_stderr" id="process_process_stderr">#</a></span></h3> 2276<ul> 2277<li><a href="stream.html#stream_stream" class="type"><Stream></a></li> 2278</ul> 2279<p>The <code>process.stderr</code> property returns a stream connected to 2280<code>stderr</code> (fd <code>2</code>). It is a <a href="net.html#net_class_net_socket"><code>net.Socket</code></a> (which is a <a href="stream.html#stream_duplex_and_transform_streams">Duplex</a> 2281stream) unless fd <code>2</code> refers to a file, in which case it is 2282a <a href="stream.html#stream_writable_streams">Writable</a> stream.</p> 2283<p><code>process.stderr</code> differs from other Node.js streams in important ways. See 2284<a href="#process_a_note_on_process_i_o">note on process I/O</a> for more information.</p> 2285<h4><code>process.stderr.fd</code><span><a class="mark" href="#process_process_stderr_fd" id="process_process_stderr_fd">#</a></span></h4> 2286<ul> 2287<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 2288</ul> 2289<p>This property refers to the value of underlying file descriptor of 2290<code>process.stderr</code>. The value is fixed at <code>2</code>. In <a href="worker_threads.html#worker_threads_class_worker"><code>Worker</code></a> threads, 2291this field does not exist.</p> 2292</section><section><h3><code>process.stdin</code><span><a class="mark" href="#process_process_stdin" id="process_process_stdin">#</a></span></h3> 2293<ul> 2294<li><a href="stream.html#stream_stream" class="type"><Stream></a></li> 2295</ul> 2296<p>The <code>process.stdin</code> property returns a stream connected to 2297<code>stdin</code> (fd <code>0</code>). It is a <a href="net.html#net_class_net_socket"><code>net.Socket</code></a> (which is a <a href="stream.html#stream_duplex_and_transform_streams">Duplex</a> 2298stream) unless fd <code>0</code> refers to a file, in which case it is 2299a <a href="stream.html#stream_readable_streams">Readable</a> stream.</p> 2300<p>For details of how to read from <code>stdin</code> see <a href="stream.html#stream_readable_read_size"><code>readable.read()</code></a>.</p> 2301<p>As a <a href="stream.html#stream_duplex_and_transform_streams">Duplex</a> stream, <code>process.stdin</code> can also be used in "old" mode that 2302is compatible with scripts written for Node.js prior to v0.10. 2303For more information see <a href="stream.html#stream_compatibility_with_older_node_js_versions">Stream compatibility</a>.</p> 2304<p>In "old" streams mode the <code>stdin</code> stream is paused by default, so one 2305must call <code>process.stdin.resume()</code> to read from it. Note also that calling 2306<code>process.stdin.resume()</code> itself would switch stream to "old" mode.</p> 2307<h4><code>process.stdin.fd</code><span><a class="mark" href="#process_process_stdin_fd" id="process_process_stdin_fd">#</a></span></h4> 2308<ul> 2309<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 2310</ul> 2311<p>This property refers to the value of underlying file descriptor of 2312<code>process.stdin</code>. The value is fixed at <code>0</code>. In <a href="worker_threads.html#worker_threads_class_worker"><code>Worker</code></a> threads, 2313this field does not exist.</p> 2314</section><section><h3><code>process.stdout</code><span><a class="mark" href="#process_process_stdout" id="process_process_stdout">#</a></span></h3> 2315<ul> 2316<li><a href="stream.html#stream_stream" class="type"><Stream></a></li> 2317</ul> 2318<p>The <code>process.stdout</code> property returns a stream connected to 2319<code>stdout</code> (fd <code>1</code>). It is a <a href="net.html#net_class_net_socket"><code>net.Socket</code></a> (which is a <a href="stream.html#stream_duplex_and_transform_streams">Duplex</a> 2320stream) unless fd <code>1</code> refers to a file, in which case it is 2321a <a href="stream.html#stream_writable_streams">Writable</a> stream.</p> 2322<p>For example, to copy <code>process.stdin</code> to <code>process.stdout</code>:</p> 2323<pre><code class="language-js">process.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">pipe</span>(process.<span class="hljs-property">stdout</span>);</code></pre> 2324<p><code>process.stdout</code> differs from other Node.js streams in important ways. See 2325<a href="#process_a_note_on_process_i_o">note on process I/O</a> for more information.</p> 2326<h4><code>process.stdout.fd</code><span><a class="mark" href="#process_process_stdout_fd" id="process_process_stdout_fd">#</a></span></h4> 2327<ul> 2328<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 2329</ul> 2330<p>This property refers to the value of underlying file descriptor of 2331<code>process.stdout</code>. The value is fixed at <code>1</code>. In <a href="worker_threads.html#worker_threads_class_worker"><code>Worker</code></a> threads, 2332this field does not exist.</p> 2333<h4>A note on process I/O<span><a class="mark" href="#process_a_note_on_process_i_o" id="process_a_note_on_process_i_o">#</a></span></h4> 2334<p><code>process.stdout</code> and <code>process.stderr</code> differ from other Node.js streams in 2335important ways:</p> 2336<ol> 2337<li>They are used internally by <a href="console.html#console_console_log_data_args"><code>console.log()</code></a> and <a href="console.html#console_console_error_data_args"><code>console.error()</code></a>, 2338respectively.</li> 2339<li>Writes may be synchronous depending on what the stream is connected to 2340and whether the system is Windows or POSIX: 2341<ul> 2342<li>Files: <em>synchronous</em> on Windows and POSIX</li> 2343<li>TTYs (Terminals): <em>asynchronous</em> on Windows, <em>synchronous</em> on POSIX</li> 2344<li>Pipes (and sockets): <em>synchronous</em> on Windows, <em>asynchronous</em> on POSIX</li> 2345</ul> 2346</li> 2347</ol> 2348<p>These behaviors are partly for historical reasons, as changing them would 2349create backward incompatibility, but they are also expected by some users.</p> 2350<p>Synchronous writes avoid problems such as output written with <code>console.log()</code> or 2351<code>console.error()</code> being unexpectedly interleaved, or not written at all if 2352<code>process.exit()</code> is called before an asynchronous write completes. See 2353<a href="#process_process_exit_code"><code>process.exit()</code></a> for more information.</p> 2354<p><em><strong>Warning</strong></em>: Synchronous writes block the event loop until the write has 2355completed. This can be near instantaneous in the case of output to a file, but 2356under high system load, pipes that are not being read at the receiving end, or 2357with slow terminals or file systems, its possible for the event loop to be 2358blocked often enough and long enough to have severe negative performance 2359impacts. This may not be a problem when writing to an interactive terminal 2360session, but consider this particularly careful when doing production logging to 2361the process output streams.</p> 2362<p>To check if a stream is connected to a <a href="tty.html#tty_tty">TTY</a> context, check the <code>isTTY</code> 2363property.</p> 2364<p>For instance:</p> 2365<pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">node -p <span class="hljs-string">"Boolean(process.stdin.isTTY)"</span></span> 2366true 2367<span class="hljs-meta">$ </span><span class="language-bash"><span class="hljs-built_in">echo</span> <span class="hljs-string">"foo"</span> | node -p <span class="hljs-string">"Boolean(process.stdin.isTTY)"</span></span> 2368false 2369<span class="hljs-meta">$ </span><span class="language-bash">node -p <span class="hljs-string">"Boolean(process.stdout.isTTY)"</span></span> 2370true 2371<span class="hljs-meta">$ </span><span class="language-bash">node -p <span class="hljs-string">"Boolean(process.stdout.isTTY)"</span> | cat</span> 2372false</code></pre> 2373<p>See the <a href="tty.html#tty_tty">TTY</a> documentation for more information.</p> 2374</section><section><h3><code>process.throwDeprecation</code><span><a class="mark" href="#process_process_throwdeprecation" id="process_process_throwdeprecation">#</a></span></h3> 2375<div class="api_metadata"> 2376<span>Added in: v0.9.12</span> 2377</div> 2378<ul> 2379<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2380</ul> 2381<p>The initial value of <code>process.throwDeprecation</code> indicates whether the 2382<code>--throw-deprecation</code> flag is set on the current Node.js process. 2383<code>process.throwDeprecation</code> is mutable, so whether or not deprecation 2384warnings result in errors may be altered at runtime. See the 2385documentation for the <a href="#process_event_warning"><code>'warning'</code> event</a> and the 2386<a href="#process_process_emitwarning_warning_type_code_ctor"><code>emitWarning()</code> method</a> for more information.</p> 2387<pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">node --throw-deprecation -p <span class="hljs-string">"process.throwDeprecation"</span></span> 2388true 2389<span class="hljs-meta">$ </span><span class="language-bash">node -p <span class="hljs-string">"process.throwDeprecation"</span></span> 2390undefined 2391<span class="hljs-meta">$ </span><span class="language-bash">node</span> 2392<span class="hljs-meta">> </span><span class="language-bash">process.emitWarning(<span class="hljs-string">'test'</span>, <span class="hljs-string">'DeprecationWarning'</span>);</span> 2393undefined 2394<span class="hljs-meta">> </span><span class="language-bash">(node:26598) DeprecationWarning: <span class="hljs-built_in">test</span></span> 2395<span class="hljs-meta">> </span><span class="language-bash">process.throwDeprecation = <span class="hljs-literal">true</span>;</span> 2396true 2397<span class="hljs-meta">> </span><span class="language-bash">process.emitWarning(<span class="hljs-string">'test'</span>, <span class="hljs-string">'DeprecationWarning'</span>);</span> 2398Thrown: 2399[DeprecationWarning: test] { name: 'DeprecationWarning' }</code></pre> 2400</section><section><h3><code>process.title</code><span><a class="mark" href="#process_process_title" id="process_process_title">#</a></span></h3> 2401<div class="api_metadata"> 2402<span>Added in: v0.1.104</span> 2403</div> 2404<ul> 2405<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2406</ul> 2407<p>The <code>process.title</code> property returns the current process title (i.e. returns 2408the current value of <code>ps</code>). Assigning a new value to <code>process.title</code> modifies 2409the current value of <code>ps</code>.</p> 2410<p>When a new value is assigned, different platforms will impose different maximum 2411length restrictions on the title. Usually such restrictions are quite limited. 2412For instance, on Linux and macOS, <code>process.title</code> is limited to the size of the 2413binary name plus the length of the command-line arguments because setting the 2414<code>process.title</code> overwrites the <code>argv</code> memory of the process. Node.js v0.8 2415allowed for longer process title strings by also overwriting the <code>environ</code> 2416memory but that was potentially insecure and confusing in some (rather obscure) 2417cases.</p> 2418<p>Assigning a value to <code>process.title</code> might not result in an accurate label 2419within process manager applications such as macOS Activity Monitor or Windows 2420Services Manager.</p> 2421</section><section><h3><code>process.traceDeprecation</code><span><a class="mark" href="#process_process_tracedeprecation" id="process_process_tracedeprecation">#</a></span></h3> 2422<div class="api_metadata"> 2423<span>Added in: v0.8.0</span> 2424</div> 2425<ul> 2426<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2427</ul> 2428<p>The <code>process.traceDeprecation</code> property indicates whether the 2429<code>--trace-deprecation</code> flag is set on the current Node.js process. See the 2430documentation for the <a href="#process_event_warning"><code>'warning'</code> event</a> and the 2431<a href="#process_process_emitwarning_warning_type_code_ctor"><code>emitWarning()</code> method</a> for more information about this 2432flag's behavior.</p> 2433</section><section><h3><code>process.umask()</code><span><a class="mark" href="#process_process_umask" id="process_process_umask">#</a></span></h3> 2434<div class="api_metadata"> 2435<details class="changelog"><summary>History</summary> 2436<table> 2437<tbody><tr><th>Version</th><th>Changes</th></tr> 2438<tr><td>v14.0.0, v12.19.0</td> 2439<td><p>Calling <code>process.umask()</code> with no arguments is deprecated.</p></td></tr> 2440<tr><td>v0.1.19</td> 2441<td><p><span>Added in: v0.1.19</span></p></td></tr> 2442</tbody></table> 2443</details> 2444</div> 2445<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated. Calling <code>process.umask()</code> with no argument causes 2446the process-wide umask to be written twice. This introduces a race condition 2447between threads, and is a potential security vulnerability. There is no safe, 2448cross-platform alternative API.</div><p></p> 2449<p><code>process.umask()</code> returns the Node.js process's file mode creation mask. Child 2450processes inherit the mask from the parent process.</p> 2451</section><section><h3><code>process.umask(mask)</code><span><a class="mark" href="#process_process_umask_mask" id="process_process_umask_mask">#</a></span></h3> 2452<div class="api_metadata"> 2453<span>Added in: v0.1.19</span> 2454</div> 2455<ul> 2456<li><code>mask</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 2457</ul> 2458<p><code>process.umask(mask)</code> sets the Node.js process's file mode creation mask. Child 2459processes inherit the mask from the parent process. Returns the previous mask.</p> 2460<pre><code class="language-js"><span class="hljs-keyword">const</span> newmask = <span class="hljs-number">0o022</span>; 2461<span class="hljs-keyword">const</span> oldmask = process.<span class="hljs-title function_">umask</span>(newmask); 2462<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>( 2463 <span class="hljs-string">`Changed umask from <span class="hljs-subst">${oldmask.toString(<span class="hljs-number">8</span>)}</span> to <span class="hljs-subst">${newmask.toString(<span class="hljs-number">8</span>)}</span>`</span> 2464);</code></pre> 2465<p>In <a href="worker_threads.html#worker_threads_class_worker"><code>Worker</code></a> threads, <code>process.umask(mask)</code> will throw an exception.</p> 2466</section><section><h3><code>process.uptime()</code><span><a class="mark" href="#process_process_uptime" id="process_process_uptime">#</a></span></h3> 2467<div class="api_metadata"> 2468<span>Added in: v0.5.0</span> 2469</div> 2470<ul> 2471<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 2472</ul> 2473<p>The <code>process.uptime()</code> method returns the number of seconds the current Node.js 2474process has been running.</p> 2475<p>The return value includes fractions of a second. Use <code>Math.floor()</code> to get whole 2476seconds.</p> 2477</section><section><h3><code>process.version</code><span><a class="mark" href="#process_process_version" id="process_process_version">#</a></span></h3> 2478<div class="api_metadata"> 2479<span>Added in: v0.1.3</span> 2480</div> 2481<ul> 2482<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2483</ul> 2484<p>The <code>process.version</code> property contains the Node.js version string.</p> 2485<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Version: <span class="hljs-subst">${process.version}</span>`</span>); 2486<span class="hljs-comment">// Version: v14.8.0</span></code></pre> 2487<p>To get the version string without the prepended <em>v</em>, use 2488<code>process.versions.node</code>.</p> 2489</section><section><h3><code>process.versions</code><span><a class="mark" href="#process_process_versions" id="process_process_versions">#</a></span></h3> 2490<div class="api_metadata"> 2491<details class="changelog"><summary>History</summary> 2492<table> 2493<tbody><tr><th>Version</th><th>Changes</th></tr> 2494<tr><td>v9.0.0</td> 2495<td><p>The <code>v8</code> property now includes a Node.js specific suffix.</p></td></tr> 2496<tr><td>v4.2.0</td> 2497<td><p>The <code>icu</code> property is now supported.</p></td></tr> 2498<tr><td>v0.2.0</td> 2499<td><p><span>Added in: v0.2.0</span></p></td></tr> 2500</tbody></table> 2501</details> 2502</div> 2503<ul> 2504<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 2505</ul> 2506<p>The <code>process.versions</code> property returns an object listing the version strings of 2507Node.js and its dependencies. <code>process.versions.modules</code> indicates the current 2508ABI version, which is increased whenever a C++ API changes. Node.js will refuse 2509to load modules that were compiled against a different module ABI version.</p> 2510<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span class="hljs-property">versions</span>);</code></pre> 2511<p>Will generate an object similar to:</p> 2512<pre><code class="language-console">{ node: '11.13.0', 2513 v8: '7.0.276.38-node.18', 2514 uv: '1.27.0', 2515 zlib: '1.2.11', 2516 brotli: '1.0.7', 2517 ares: '1.15.0', 2518 modules: '67', 2519 nghttp2: '1.34.0', 2520 napi: '4', 2521 llhttp: '1.1.1', 2522 openssl: '1.1.1b', 2523 cldr: '34.0', 2524 icu: '63.1', 2525 tz: '2018e', 2526 unicode: '11.0' }</code></pre> 2527</section><section><h3>Exit codes<span><a class="mark" href="#process_exit_codes" id="process_exit_codes">#</a></span></h3> 2528<p>Node.js will normally exit with a <code>0</code> status code when no more async 2529operations are pending. The following status codes are used in other 2530cases:</p> 2531<ul> 2532<li><code>1</code> <strong>Uncaught Fatal Exception</strong>: There was an uncaught exception, 2533and it was not handled by a domain or an <a href="#process_event_uncaughtexception"><code>'uncaughtException'</code></a> event 2534handler.</li> 2535<li><code>2</code>: Unused (reserved by Bash for builtin misuse)</li> 2536<li><code>3</code> <strong>Internal JavaScript Parse Error</strong>: The JavaScript source code 2537internal in the Node.js bootstrapping process caused a parse error. This 2538is extremely rare, and generally can only happen during development 2539of Node.js itself.</li> 2540<li><code>4</code> <strong>Internal JavaScript Evaluation Failure</strong>: The JavaScript 2541source code internal in the Node.js bootstrapping process failed to 2542return a function value when evaluated. This is extremely rare, and 2543generally can only happen during development of Node.js itself.</li> 2544<li><code>5</code> <strong>Fatal Error</strong>: There was a fatal unrecoverable error in V8. 2545Typically a message will be printed to stderr with the prefix <code>FATAL ERROR</code>.</li> 2546<li><code>6</code> <strong>Non-function Internal Exception Handler</strong>: There was an 2547uncaught exception, but the internal fatal exception handler 2548function was somehow set to a non-function, and could not be called.</li> 2549<li><code>7</code> <strong>Internal Exception Handler Run-Time Failure</strong>: There was an 2550uncaught exception, and the internal fatal exception handler 2551function itself threw an error while attempting to handle it. This 2552can happen, for example, if an <a href="#process_event_uncaughtexception"><code>'uncaughtException'</code></a> or 2553<code>domain.on('error')</code> handler throws an error.</li> 2554<li><code>8</code>: Unused. In previous versions of Node.js, exit code 8 sometimes 2555indicated an uncaught exception.</li> 2556<li><code>9</code> <strong>Invalid Argument</strong>: Either an unknown option was specified, 2557or an option requiring a value was provided without a value.</li> 2558<li><code>10</code> <strong>Internal JavaScript Run-Time Failure</strong>: The JavaScript 2559source code internal in the Node.js bootstrapping process threw an error 2560when the bootstrapping function was called. This is extremely rare, 2561and generally can only happen during development of Node.js itself.</li> 2562<li><code>12</code> <strong>Invalid Debug Argument</strong>: The <code>--inspect</code> and/or <code>--inspect-brk</code> 2563options were set, but the port number chosen was invalid or unavailable.</li> 2564<li><code>13</code> <strong>Unfinished Top-Level Await</strong>: <code>await</code> was used outside of a function 2565in the top-level code, but the passed <code>Promise</code> never resolved.</li> 2566<li><code>>128</code> <strong>Signal Exits</strong>: If Node.js receives a fatal signal such as 2567<code>SIGKILL</code> or <code>SIGHUP</code>, then its exit code will be <code>128</code> plus the 2568value of the signal code. This is a standard POSIX practice, since 2569exit codes are defined to be 7-bit integers, and signal exits set 2570the high-order bit, and then contain the value of the signal code. 2571For example, signal <code>SIGABRT</code> has value <code>6</code>, so the expected exit 2572code will be <code>128</code> + <code>6</code>, or <code>134</code>.</li> 2573</ul></section> 2574 <!-- API END --> 2575 </div> 2576 </div> 2577 </div> 2578 <script> 2579 'use strict'; 2580 { 2581 const kCustomPreference = 'customDarkTheme'; 2582 const userSettings = sessionStorage.getItem(kCustomPreference); 2583 const themeToggleButton = document.getElementById('theme-toggle-btn'); 2584 if (userSettings === null && window.matchMedia) { 2585 const mq = window.matchMedia('(prefers-color-scheme: dark)'); 2586 if ('onchange' in mq) { 2587 function mqChangeListener(e) { 2588 document.body.classList.toggle('dark-mode', e.matches); 2589 } 2590 mq.addEventListener('change', mqChangeListener); 2591 if (themeToggleButton) { 2592 themeToggleButton.addEventListener('click', function() { 2593 mq.removeEventListener('change', mqChangeListener); 2594 }, { once: true }); 2595 } 2596 } 2597 if (mq.matches) { 2598 document.body.classList.add('dark-mode'); 2599 } 2600 } else if (userSettings === 'true') { 2601 document.body.classList.add('dark-mode'); 2602 } 2603 if (themeToggleButton) { 2604 themeToggleButton.hidden = false; 2605 themeToggleButton.addEventListener('click', function() { 2606 sessionStorage.setItem( 2607 kCustomPreference, 2608 document.body.classList.toggle('dark-mode') 2609 ); 2610 }); 2611 } 2612 } 2613 </script> 2614</body> 2615</html> 2616