11cb0ef41Sopenharmony_ci# Process 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci<!-- introduced_in=v0.10.0 --> 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci<!-- type=global --> 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci<!-- source_link=lib/process.js --> 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciThe `process` object provides information about, and control over, the current 101cb0ef41Sopenharmony_ciNode.js process. 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ci```mjs 131cb0ef41Sopenharmony_ciimport process from 'node:process'; 141cb0ef41Sopenharmony_ci``` 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci```cjs 171cb0ef41Sopenharmony_ciconst process = require('node:process'); 181cb0ef41Sopenharmony_ci``` 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci## Process events 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ciThe `process` object is an instance of [`EventEmitter`][]. 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci### Event: `'beforeExit'` 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci<!-- YAML 271cb0ef41Sopenharmony_ciadded: v0.11.12 281cb0ef41Sopenharmony_ci--> 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ciThe `'beforeExit'` event is emitted when Node.js empties its event loop and has 311cb0ef41Sopenharmony_cino additional work to schedule. Normally, the Node.js process will exit when 321cb0ef41Sopenharmony_cithere is no work scheduled, but a listener registered on the `'beforeExit'` 331cb0ef41Sopenharmony_cievent can make asynchronous calls, and thereby cause the Node.js process to 341cb0ef41Sopenharmony_cicontinue. 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ciThe listener callback function is invoked with the value of 371cb0ef41Sopenharmony_ci[`process.exitCode`][] passed as the only argument. 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ciThe `'beforeExit'` event is _not_ emitted for conditions causing explicit 401cb0ef41Sopenharmony_citermination, such as calling [`process.exit()`][] or uncaught exceptions. 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ciThe `'beforeExit'` should _not_ be used as an alternative to the `'exit'` event 431cb0ef41Sopenharmony_ciunless the intention is to schedule additional work. 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci```mjs 461cb0ef41Sopenharmony_ciimport process from 'node:process'; 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ciprocess.on('beforeExit', (code) => { 491cb0ef41Sopenharmony_ci console.log('Process beforeExit event with code: ', code); 501cb0ef41Sopenharmony_ci}); 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ciprocess.on('exit', (code) => { 531cb0ef41Sopenharmony_ci console.log('Process exit event with code: ', code); 541cb0ef41Sopenharmony_ci}); 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ciconsole.log('This message is displayed first.'); 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci// Prints: 591cb0ef41Sopenharmony_ci// This message is displayed first. 601cb0ef41Sopenharmony_ci// Process beforeExit event with code: 0 611cb0ef41Sopenharmony_ci// Process exit event with code: 0 621cb0ef41Sopenharmony_ci``` 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci```cjs 651cb0ef41Sopenharmony_ciconst process = require('node:process'); 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ciprocess.on('beforeExit', (code) => { 681cb0ef41Sopenharmony_ci console.log('Process beforeExit event with code: ', code); 691cb0ef41Sopenharmony_ci}); 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ciprocess.on('exit', (code) => { 721cb0ef41Sopenharmony_ci console.log('Process exit event with code: ', code); 731cb0ef41Sopenharmony_ci}); 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ciconsole.log('This message is displayed first.'); 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci// Prints: 781cb0ef41Sopenharmony_ci// This message is displayed first. 791cb0ef41Sopenharmony_ci// Process beforeExit event with code: 0 801cb0ef41Sopenharmony_ci// Process exit event with code: 0 811cb0ef41Sopenharmony_ci``` 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci### Event: `'disconnect'` 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_ci<!-- YAML 861cb0ef41Sopenharmony_ciadded: v0.7.7 871cb0ef41Sopenharmony_ci--> 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ciIf the Node.js process is spawned with an IPC channel (see the [Child Process][] 901cb0ef41Sopenharmony_ciand [Cluster][] documentation), the `'disconnect'` event will be emitted when 911cb0ef41Sopenharmony_cithe IPC channel is closed. 921cb0ef41Sopenharmony_ci 931cb0ef41Sopenharmony_ci### Event: `'exit'` 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_ci<!-- YAML 961cb0ef41Sopenharmony_ciadded: v0.1.7 971cb0ef41Sopenharmony_ci--> 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ci* `code` {integer} 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_ciThe `'exit'` event is emitted when the Node.js process is about to exit as a 1021cb0ef41Sopenharmony_ciresult of either: 1031cb0ef41Sopenharmony_ci 1041cb0ef41Sopenharmony_ci* The `process.exit()` method being called explicitly; 1051cb0ef41Sopenharmony_ci* The Node.js event loop no longer having any additional work to perform. 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ciThere is no way to prevent the exiting of the event loop at this point, and once 1081cb0ef41Sopenharmony_ciall `'exit'` listeners have finished running the Node.js process will terminate. 1091cb0ef41Sopenharmony_ci 1101cb0ef41Sopenharmony_ciThe listener callback function is invoked with the exit code specified either 1111cb0ef41Sopenharmony_ciby the [`process.exitCode`][] property, or the `exitCode` argument passed to the 1121cb0ef41Sopenharmony_ci[`process.exit()`][] method. 1131cb0ef41Sopenharmony_ci 1141cb0ef41Sopenharmony_ci```mjs 1151cb0ef41Sopenharmony_ciimport process from 'node:process'; 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_ciprocess.on('exit', (code) => { 1181cb0ef41Sopenharmony_ci console.log(`About to exit with code: ${code}`); 1191cb0ef41Sopenharmony_ci}); 1201cb0ef41Sopenharmony_ci``` 1211cb0ef41Sopenharmony_ci 1221cb0ef41Sopenharmony_ci```cjs 1231cb0ef41Sopenharmony_ciconst process = require('node:process'); 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ciprocess.on('exit', (code) => { 1261cb0ef41Sopenharmony_ci console.log(`About to exit with code: ${code}`); 1271cb0ef41Sopenharmony_ci}); 1281cb0ef41Sopenharmony_ci``` 1291cb0ef41Sopenharmony_ci 1301cb0ef41Sopenharmony_ciListener functions **must** only perform **synchronous** operations. The Node.js 1311cb0ef41Sopenharmony_ciprocess will exit immediately after calling the `'exit'` event listeners 1321cb0ef41Sopenharmony_cicausing any additional work still queued in the event loop to be abandoned. 1331cb0ef41Sopenharmony_ciIn the following example, for instance, the timeout will never occur: 1341cb0ef41Sopenharmony_ci 1351cb0ef41Sopenharmony_ci```mjs 1361cb0ef41Sopenharmony_ciimport process from 'node:process'; 1371cb0ef41Sopenharmony_ci 1381cb0ef41Sopenharmony_ciprocess.on('exit', (code) => { 1391cb0ef41Sopenharmony_ci setTimeout(() => { 1401cb0ef41Sopenharmony_ci console.log('This will not run'); 1411cb0ef41Sopenharmony_ci }, 0); 1421cb0ef41Sopenharmony_ci}); 1431cb0ef41Sopenharmony_ci``` 1441cb0ef41Sopenharmony_ci 1451cb0ef41Sopenharmony_ci```cjs 1461cb0ef41Sopenharmony_ciconst process = require('node:process'); 1471cb0ef41Sopenharmony_ci 1481cb0ef41Sopenharmony_ciprocess.on('exit', (code) => { 1491cb0ef41Sopenharmony_ci setTimeout(() => { 1501cb0ef41Sopenharmony_ci console.log('This will not run'); 1511cb0ef41Sopenharmony_ci }, 0); 1521cb0ef41Sopenharmony_ci}); 1531cb0ef41Sopenharmony_ci``` 1541cb0ef41Sopenharmony_ci 1551cb0ef41Sopenharmony_ci### Event: `'message'` 1561cb0ef41Sopenharmony_ci 1571cb0ef41Sopenharmony_ci<!-- YAML 1581cb0ef41Sopenharmony_ciadded: v0.5.10 1591cb0ef41Sopenharmony_ci--> 1601cb0ef41Sopenharmony_ci 1611cb0ef41Sopenharmony_ci* `message` { Object | boolean | number | string | null } a parsed JSON object 1621cb0ef41Sopenharmony_ci or a serializable primitive value. 1631cb0ef41Sopenharmony_ci* `sendHandle` {net.Server|net.Socket} a [`net.Server`][] or [`net.Socket`][] 1641cb0ef41Sopenharmony_ci object, or undefined. 1651cb0ef41Sopenharmony_ci 1661cb0ef41Sopenharmony_ciIf the Node.js process is spawned with an IPC channel (see the [Child Process][] 1671cb0ef41Sopenharmony_ciand [Cluster][] documentation), the `'message'` event is emitted whenever a 1681cb0ef41Sopenharmony_cimessage sent by a parent process using [`childprocess.send()`][] is received by 1691cb0ef41Sopenharmony_cithe child process. 1701cb0ef41Sopenharmony_ci 1711cb0ef41Sopenharmony_ciThe message goes through serialization and parsing. The resulting message might 1721cb0ef41Sopenharmony_cinot be the same as what is originally sent. 1731cb0ef41Sopenharmony_ci 1741cb0ef41Sopenharmony_ciIf the `serialization` option was set to `advanced` used when spawning the 1751cb0ef41Sopenharmony_ciprocess, the `message` argument can contain data that JSON is not able 1761cb0ef41Sopenharmony_cito represent. 1771cb0ef41Sopenharmony_ciSee [Advanced serialization for `child_process`][] for more details. 1781cb0ef41Sopenharmony_ci 1791cb0ef41Sopenharmony_ci### Event: `'multipleResolves'` 1801cb0ef41Sopenharmony_ci 1811cb0ef41Sopenharmony_ci<!-- YAML 1821cb0ef41Sopenharmony_ciadded: v10.12.0 1831cb0ef41Sopenharmony_cideprecated: v17.6.0 1841cb0ef41Sopenharmony_ci--> 1851cb0ef41Sopenharmony_ci 1861cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated 1871cb0ef41Sopenharmony_ci 1881cb0ef41Sopenharmony_ci* `type` {string} The resolution type. One of `'resolve'` or `'reject'`. 1891cb0ef41Sopenharmony_ci* `promise` {Promise} The promise that resolved or rejected more than once. 1901cb0ef41Sopenharmony_ci* `value` {any} The value with which the promise was either resolved or 1911cb0ef41Sopenharmony_ci rejected after the original resolve. 1921cb0ef41Sopenharmony_ci 1931cb0ef41Sopenharmony_ciThe `'multipleResolves'` event is emitted whenever a `Promise` has been either: 1941cb0ef41Sopenharmony_ci 1951cb0ef41Sopenharmony_ci* Resolved more than once. 1961cb0ef41Sopenharmony_ci* Rejected more than once. 1971cb0ef41Sopenharmony_ci* Rejected after resolve. 1981cb0ef41Sopenharmony_ci* Resolved after reject. 1991cb0ef41Sopenharmony_ci 2001cb0ef41Sopenharmony_ciThis is useful for tracking potential errors in an application while using the 2011cb0ef41Sopenharmony_ci`Promise` constructor, as multiple resolutions are silently swallowed. However, 2021cb0ef41Sopenharmony_cithe occurrence of this event does not necessarily indicate an error. For 2031cb0ef41Sopenharmony_ciexample, [`Promise.race()`][] can trigger a `'multipleResolves'` event. 2041cb0ef41Sopenharmony_ci 2051cb0ef41Sopenharmony_ciBecause of the unreliability of the event in cases like the 2061cb0ef41Sopenharmony_ci[`Promise.race()`][] example above it has been deprecated. 2071cb0ef41Sopenharmony_ci 2081cb0ef41Sopenharmony_ci```mjs 2091cb0ef41Sopenharmony_ciimport process from 'node:process'; 2101cb0ef41Sopenharmony_ci 2111cb0ef41Sopenharmony_ciprocess.on('multipleResolves', (type, promise, reason) => { 2121cb0ef41Sopenharmony_ci console.error(type, promise, reason); 2131cb0ef41Sopenharmony_ci setImmediate(() => process.exit(1)); 2141cb0ef41Sopenharmony_ci}); 2151cb0ef41Sopenharmony_ci 2161cb0ef41Sopenharmony_ciasync function main() { 2171cb0ef41Sopenharmony_ci try { 2181cb0ef41Sopenharmony_ci return await new Promise((resolve, reject) => { 2191cb0ef41Sopenharmony_ci resolve('First call'); 2201cb0ef41Sopenharmony_ci resolve('Swallowed resolve'); 2211cb0ef41Sopenharmony_ci reject(new Error('Swallowed reject')); 2221cb0ef41Sopenharmony_ci }); 2231cb0ef41Sopenharmony_ci } catch { 2241cb0ef41Sopenharmony_ci throw new Error('Failed'); 2251cb0ef41Sopenharmony_ci } 2261cb0ef41Sopenharmony_ci} 2271cb0ef41Sopenharmony_ci 2281cb0ef41Sopenharmony_cimain().then(console.log); 2291cb0ef41Sopenharmony_ci// resolve: Promise { 'First call' } 'Swallowed resolve' 2301cb0ef41Sopenharmony_ci// reject: Promise { 'First call' } Error: Swallowed reject 2311cb0ef41Sopenharmony_ci// at Promise (*) 2321cb0ef41Sopenharmony_ci// at new Promise (<anonymous>) 2331cb0ef41Sopenharmony_ci// at main (*) 2341cb0ef41Sopenharmony_ci// First call 2351cb0ef41Sopenharmony_ci``` 2361cb0ef41Sopenharmony_ci 2371cb0ef41Sopenharmony_ci```cjs 2381cb0ef41Sopenharmony_ciconst process = require('node:process'); 2391cb0ef41Sopenharmony_ci 2401cb0ef41Sopenharmony_ciprocess.on('multipleResolves', (type, promise, reason) => { 2411cb0ef41Sopenharmony_ci console.error(type, promise, reason); 2421cb0ef41Sopenharmony_ci setImmediate(() => process.exit(1)); 2431cb0ef41Sopenharmony_ci}); 2441cb0ef41Sopenharmony_ci 2451cb0ef41Sopenharmony_ciasync function main() { 2461cb0ef41Sopenharmony_ci try { 2471cb0ef41Sopenharmony_ci return await new Promise((resolve, reject) => { 2481cb0ef41Sopenharmony_ci resolve('First call'); 2491cb0ef41Sopenharmony_ci resolve('Swallowed resolve'); 2501cb0ef41Sopenharmony_ci reject(new Error('Swallowed reject')); 2511cb0ef41Sopenharmony_ci }); 2521cb0ef41Sopenharmony_ci } catch { 2531cb0ef41Sopenharmony_ci throw new Error('Failed'); 2541cb0ef41Sopenharmony_ci } 2551cb0ef41Sopenharmony_ci} 2561cb0ef41Sopenharmony_ci 2571cb0ef41Sopenharmony_cimain().then(console.log); 2581cb0ef41Sopenharmony_ci// resolve: Promise { 'First call' } 'Swallowed resolve' 2591cb0ef41Sopenharmony_ci// reject: Promise { 'First call' } Error: Swallowed reject 2601cb0ef41Sopenharmony_ci// at Promise (*) 2611cb0ef41Sopenharmony_ci// at new Promise (<anonymous>) 2621cb0ef41Sopenharmony_ci// at main (*) 2631cb0ef41Sopenharmony_ci// First call 2641cb0ef41Sopenharmony_ci``` 2651cb0ef41Sopenharmony_ci 2661cb0ef41Sopenharmony_ci### Event: `'rejectionHandled'` 2671cb0ef41Sopenharmony_ci 2681cb0ef41Sopenharmony_ci<!-- YAML 2691cb0ef41Sopenharmony_ciadded: v1.4.1 2701cb0ef41Sopenharmony_ci--> 2711cb0ef41Sopenharmony_ci 2721cb0ef41Sopenharmony_ci* `promise` {Promise} The late handled promise. 2731cb0ef41Sopenharmony_ci 2741cb0ef41Sopenharmony_ciThe `'rejectionHandled'` event is emitted whenever a `Promise` has been rejected 2751cb0ef41Sopenharmony_ciand an error handler was attached to it (using [`promise.catch()`][], for 2761cb0ef41Sopenharmony_ciexample) later than one turn of the Node.js event loop. 2771cb0ef41Sopenharmony_ci 2781cb0ef41Sopenharmony_ciThe `Promise` object would have previously been emitted in an 2791cb0ef41Sopenharmony_ci`'unhandledRejection'` event, but during the course of processing gained a 2801cb0ef41Sopenharmony_cirejection handler. 2811cb0ef41Sopenharmony_ci 2821cb0ef41Sopenharmony_ciThere is no notion of a top level for a `Promise` chain at which rejections can 2831cb0ef41Sopenharmony_cialways be handled. Being inherently asynchronous in nature, a `Promise` 2841cb0ef41Sopenharmony_cirejection can be handled at a future point in time, possibly much later than 2851cb0ef41Sopenharmony_cithe event loop turn it takes for the `'unhandledRejection'` event to be emitted. 2861cb0ef41Sopenharmony_ci 2871cb0ef41Sopenharmony_ciAnother way of stating this is that, unlike in synchronous code where there is 2881cb0ef41Sopenharmony_cian ever-growing list of unhandled exceptions, with Promises there can be a 2891cb0ef41Sopenharmony_cigrowing-and-shrinking list of unhandled rejections. 2901cb0ef41Sopenharmony_ci 2911cb0ef41Sopenharmony_ciIn synchronous code, the `'uncaughtException'` event is emitted when the list of 2921cb0ef41Sopenharmony_ciunhandled exceptions grows. 2931cb0ef41Sopenharmony_ci 2941cb0ef41Sopenharmony_ciIn asynchronous code, the `'unhandledRejection'` event is emitted when the list 2951cb0ef41Sopenharmony_ciof unhandled rejections grows, and the `'rejectionHandled'` event is emitted 2961cb0ef41Sopenharmony_ciwhen the list of unhandled rejections shrinks. 2971cb0ef41Sopenharmony_ci 2981cb0ef41Sopenharmony_ci```mjs 2991cb0ef41Sopenharmony_ciimport process from 'node:process'; 3001cb0ef41Sopenharmony_ci 3011cb0ef41Sopenharmony_ciconst unhandledRejections = new Map(); 3021cb0ef41Sopenharmony_ciprocess.on('unhandledRejection', (reason, promise) => { 3031cb0ef41Sopenharmony_ci unhandledRejections.set(promise, reason); 3041cb0ef41Sopenharmony_ci}); 3051cb0ef41Sopenharmony_ciprocess.on('rejectionHandled', (promise) => { 3061cb0ef41Sopenharmony_ci unhandledRejections.delete(promise); 3071cb0ef41Sopenharmony_ci}); 3081cb0ef41Sopenharmony_ci``` 3091cb0ef41Sopenharmony_ci 3101cb0ef41Sopenharmony_ci```cjs 3111cb0ef41Sopenharmony_ciconst process = require('node:process'); 3121cb0ef41Sopenharmony_ci 3131cb0ef41Sopenharmony_ciconst unhandledRejections = new Map(); 3141cb0ef41Sopenharmony_ciprocess.on('unhandledRejection', (reason, promise) => { 3151cb0ef41Sopenharmony_ci unhandledRejections.set(promise, reason); 3161cb0ef41Sopenharmony_ci}); 3171cb0ef41Sopenharmony_ciprocess.on('rejectionHandled', (promise) => { 3181cb0ef41Sopenharmony_ci unhandledRejections.delete(promise); 3191cb0ef41Sopenharmony_ci}); 3201cb0ef41Sopenharmony_ci``` 3211cb0ef41Sopenharmony_ci 3221cb0ef41Sopenharmony_ciIn this example, the `unhandledRejections` `Map` will grow and shrink over time, 3231cb0ef41Sopenharmony_cireflecting rejections that start unhandled and then become handled. It is 3241cb0ef41Sopenharmony_cipossible to record such errors in an error log, either periodically (which is 3251cb0ef41Sopenharmony_cilikely best for long-running application) or upon process exit (which is likely 3261cb0ef41Sopenharmony_cimost convenient for scripts). 3271cb0ef41Sopenharmony_ci 3281cb0ef41Sopenharmony_ci### Event: `'uncaughtException'` 3291cb0ef41Sopenharmony_ci 3301cb0ef41Sopenharmony_ci<!-- YAML 3311cb0ef41Sopenharmony_ciadded: v0.1.18 3321cb0ef41Sopenharmony_cichanges: 3331cb0ef41Sopenharmony_ci - version: 3341cb0ef41Sopenharmony_ci - v12.0.0 3351cb0ef41Sopenharmony_ci - v10.17.0 3361cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/26599 3371cb0ef41Sopenharmony_ci description: Added the `origin` argument. 3381cb0ef41Sopenharmony_ci--> 3391cb0ef41Sopenharmony_ci 3401cb0ef41Sopenharmony_ci* `err` {Error} The uncaught exception. 3411cb0ef41Sopenharmony_ci* `origin` {string} Indicates if the exception originates from an unhandled 3421cb0ef41Sopenharmony_ci rejection or from a synchronous error. Can either be `'uncaughtException'` or 3431cb0ef41Sopenharmony_ci `'unhandledRejection'`. The latter is used when an exception happens in a 3441cb0ef41Sopenharmony_ci `Promise` based async context (or if a `Promise` is rejected) and 3451cb0ef41Sopenharmony_ci [`--unhandled-rejections`][] flag set to `strict` or `throw` (which is the 3461cb0ef41Sopenharmony_ci default) and the rejection is not handled, or when a rejection happens during 3471cb0ef41Sopenharmony_ci the command line entry point's ES module static loading phase. 3481cb0ef41Sopenharmony_ci 3491cb0ef41Sopenharmony_ciThe `'uncaughtException'` event is emitted when an uncaught JavaScript 3501cb0ef41Sopenharmony_ciexception bubbles all the way back to the event loop. By default, Node.js 3511cb0ef41Sopenharmony_cihandles such exceptions by printing the stack trace to `stderr` and exiting 3521cb0ef41Sopenharmony_ciwith code 1, overriding any previously set [`process.exitCode`][]. 3531cb0ef41Sopenharmony_ciAdding a handler for the `'uncaughtException'` event overrides this default 3541cb0ef41Sopenharmony_cibehavior. Alternatively, change the [`process.exitCode`][] in the 3551cb0ef41Sopenharmony_ci`'uncaughtException'` handler which will result in the process exiting with the 3561cb0ef41Sopenharmony_ciprovided exit code. Otherwise, in the presence of such handler the process will 3571cb0ef41Sopenharmony_ciexit with 0. 3581cb0ef41Sopenharmony_ci 3591cb0ef41Sopenharmony_ci```mjs 3601cb0ef41Sopenharmony_ciimport process from 'node:process'; 3611cb0ef41Sopenharmony_ci 3621cb0ef41Sopenharmony_ciprocess.on('uncaughtException', (err, origin) => { 3631cb0ef41Sopenharmony_ci fs.writeSync( 3641cb0ef41Sopenharmony_ci process.stderr.fd, 3651cb0ef41Sopenharmony_ci `Caught exception: ${err}\n` + 3661cb0ef41Sopenharmony_ci `Exception origin: ${origin}`, 3671cb0ef41Sopenharmony_ci ); 3681cb0ef41Sopenharmony_ci}); 3691cb0ef41Sopenharmony_ci 3701cb0ef41Sopenharmony_cisetTimeout(() => { 3711cb0ef41Sopenharmony_ci console.log('This will still run.'); 3721cb0ef41Sopenharmony_ci}, 500); 3731cb0ef41Sopenharmony_ci 3741cb0ef41Sopenharmony_ci// Intentionally cause an exception, but don't catch it. 3751cb0ef41Sopenharmony_cinonexistentFunc(); 3761cb0ef41Sopenharmony_ciconsole.log('This will not run.'); 3771cb0ef41Sopenharmony_ci``` 3781cb0ef41Sopenharmony_ci 3791cb0ef41Sopenharmony_ci```cjs 3801cb0ef41Sopenharmony_ciconst process = require('node:process'); 3811cb0ef41Sopenharmony_ci 3821cb0ef41Sopenharmony_ciprocess.on('uncaughtException', (err, origin) => { 3831cb0ef41Sopenharmony_ci fs.writeSync( 3841cb0ef41Sopenharmony_ci process.stderr.fd, 3851cb0ef41Sopenharmony_ci `Caught exception: ${err}\n` + 3861cb0ef41Sopenharmony_ci `Exception origin: ${origin}`, 3871cb0ef41Sopenharmony_ci ); 3881cb0ef41Sopenharmony_ci}); 3891cb0ef41Sopenharmony_ci 3901cb0ef41Sopenharmony_cisetTimeout(() => { 3911cb0ef41Sopenharmony_ci console.log('This will still run.'); 3921cb0ef41Sopenharmony_ci}, 500); 3931cb0ef41Sopenharmony_ci 3941cb0ef41Sopenharmony_ci// Intentionally cause an exception, but don't catch it. 3951cb0ef41Sopenharmony_cinonexistentFunc(); 3961cb0ef41Sopenharmony_ciconsole.log('This will not run.'); 3971cb0ef41Sopenharmony_ci``` 3981cb0ef41Sopenharmony_ci 3991cb0ef41Sopenharmony_ciIt is possible to monitor `'uncaughtException'` events without overriding the 4001cb0ef41Sopenharmony_cidefault behavior to exit the process by installing a 4011cb0ef41Sopenharmony_ci`'uncaughtExceptionMonitor'` listener. 4021cb0ef41Sopenharmony_ci 4031cb0ef41Sopenharmony_ci#### Warning: Using `'uncaughtException'` correctly 4041cb0ef41Sopenharmony_ci 4051cb0ef41Sopenharmony_ci`'uncaughtException'` is a crude mechanism for exception handling 4061cb0ef41Sopenharmony_ciintended to be used only as a last resort. The event _should not_ be used as 4071cb0ef41Sopenharmony_cian equivalent to `On Error Resume Next`. Unhandled exceptions inherently mean 4081cb0ef41Sopenharmony_cithat an application is in an undefined state. Attempting to resume application 4091cb0ef41Sopenharmony_cicode without properly recovering from the exception can cause additional 4101cb0ef41Sopenharmony_ciunforeseen and unpredictable issues. 4111cb0ef41Sopenharmony_ci 4121cb0ef41Sopenharmony_ciExceptions thrown from within the event handler will not be caught. Instead the 4131cb0ef41Sopenharmony_ciprocess will exit with a non-zero exit code and the stack trace will be printed. 4141cb0ef41Sopenharmony_ciThis is to avoid infinite recursion. 4151cb0ef41Sopenharmony_ci 4161cb0ef41Sopenharmony_ciAttempting to resume normally after an uncaught exception can be similar to 4171cb0ef41Sopenharmony_cipulling out the power cord when upgrading a computer. Nine out of ten 4181cb0ef41Sopenharmony_citimes, nothing happens. But the tenth time, the system becomes corrupted. 4191cb0ef41Sopenharmony_ci 4201cb0ef41Sopenharmony_ciThe correct use of `'uncaughtException'` is to perform synchronous cleanup 4211cb0ef41Sopenharmony_ciof allocated resources (e.g. file descriptors, handles, etc) before shutting 4221cb0ef41Sopenharmony_cidown the process. **It is not safe to resume normal operation after 4231cb0ef41Sopenharmony_ci`'uncaughtException'`.** 4241cb0ef41Sopenharmony_ci 4251cb0ef41Sopenharmony_ciTo restart a crashed application in a more reliable way, whether 4261cb0ef41Sopenharmony_ci`'uncaughtException'` is emitted or not, an external monitor should be employed 4271cb0ef41Sopenharmony_ciin a separate process to detect application failures and recover or restart as 4281cb0ef41Sopenharmony_cineeded. 4291cb0ef41Sopenharmony_ci 4301cb0ef41Sopenharmony_ci### Event: `'uncaughtExceptionMonitor'` 4311cb0ef41Sopenharmony_ci 4321cb0ef41Sopenharmony_ci<!-- YAML 4331cb0ef41Sopenharmony_ciadded: 4341cb0ef41Sopenharmony_ci - v13.7.0 4351cb0ef41Sopenharmony_ci - v12.17.0 4361cb0ef41Sopenharmony_ci--> 4371cb0ef41Sopenharmony_ci 4381cb0ef41Sopenharmony_ci* `err` {Error} The uncaught exception. 4391cb0ef41Sopenharmony_ci* `origin` {string} Indicates if the exception originates from an unhandled 4401cb0ef41Sopenharmony_ci rejection or from synchronous errors. Can either be `'uncaughtException'` or 4411cb0ef41Sopenharmony_ci `'unhandledRejection'`. The latter is used when an exception happens in a 4421cb0ef41Sopenharmony_ci `Promise` based async context (or if a `Promise` is rejected) and 4431cb0ef41Sopenharmony_ci [`--unhandled-rejections`][] flag set to `strict` or `throw` (which is the 4441cb0ef41Sopenharmony_ci default) and the rejection is not handled, or when a rejection happens during 4451cb0ef41Sopenharmony_ci the command line entry point's ES module static loading phase. 4461cb0ef41Sopenharmony_ci 4471cb0ef41Sopenharmony_ciThe `'uncaughtExceptionMonitor'` event is emitted before an 4481cb0ef41Sopenharmony_ci`'uncaughtException'` event is emitted or a hook installed via 4491cb0ef41Sopenharmony_ci[`process.setUncaughtExceptionCaptureCallback()`][] is called. 4501cb0ef41Sopenharmony_ci 4511cb0ef41Sopenharmony_ciInstalling an `'uncaughtExceptionMonitor'` listener does not change the behavior 4521cb0ef41Sopenharmony_cionce an `'uncaughtException'` event is emitted. The process will 4531cb0ef41Sopenharmony_cistill crash if no `'uncaughtException'` listener is installed. 4541cb0ef41Sopenharmony_ci 4551cb0ef41Sopenharmony_ci```mjs 4561cb0ef41Sopenharmony_ciimport process from 'node:process'; 4571cb0ef41Sopenharmony_ci 4581cb0ef41Sopenharmony_ciprocess.on('uncaughtExceptionMonitor', (err, origin) => { 4591cb0ef41Sopenharmony_ci MyMonitoringTool.logSync(err, origin); 4601cb0ef41Sopenharmony_ci}); 4611cb0ef41Sopenharmony_ci 4621cb0ef41Sopenharmony_ci// Intentionally cause an exception, but don't catch it. 4631cb0ef41Sopenharmony_cinonexistentFunc(); 4641cb0ef41Sopenharmony_ci// Still crashes Node.js 4651cb0ef41Sopenharmony_ci``` 4661cb0ef41Sopenharmony_ci 4671cb0ef41Sopenharmony_ci```cjs 4681cb0ef41Sopenharmony_ciconst process = require('node:process'); 4691cb0ef41Sopenharmony_ci 4701cb0ef41Sopenharmony_ciprocess.on('uncaughtExceptionMonitor', (err, origin) => { 4711cb0ef41Sopenharmony_ci MyMonitoringTool.logSync(err, origin); 4721cb0ef41Sopenharmony_ci}); 4731cb0ef41Sopenharmony_ci 4741cb0ef41Sopenharmony_ci// Intentionally cause an exception, but don't catch it. 4751cb0ef41Sopenharmony_cinonexistentFunc(); 4761cb0ef41Sopenharmony_ci// Still crashes Node.js 4771cb0ef41Sopenharmony_ci``` 4781cb0ef41Sopenharmony_ci 4791cb0ef41Sopenharmony_ci### Event: `'unhandledRejection'` 4801cb0ef41Sopenharmony_ci 4811cb0ef41Sopenharmony_ci<!-- YAML 4821cb0ef41Sopenharmony_ciadded: v1.4.1 4831cb0ef41Sopenharmony_cichanges: 4841cb0ef41Sopenharmony_ci - version: v7.0.0 4851cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/8217 4861cb0ef41Sopenharmony_ci description: Not handling `Promise` rejections is deprecated. 4871cb0ef41Sopenharmony_ci - version: v6.6.0 4881cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/8223 4891cb0ef41Sopenharmony_ci description: Unhandled `Promise` rejections will now emit 4901cb0ef41Sopenharmony_ci a process warning. 4911cb0ef41Sopenharmony_ci--> 4921cb0ef41Sopenharmony_ci 4931cb0ef41Sopenharmony_ci* `reason` {Error|any} The object with which the promise was rejected 4941cb0ef41Sopenharmony_ci (typically an [`Error`][] object). 4951cb0ef41Sopenharmony_ci* `promise` {Promise} The rejected promise. 4961cb0ef41Sopenharmony_ci 4971cb0ef41Sopenharmony_ciThe `'unhandledRejection'` event is emitted whenever a `Promise` is rejected and 4981cb0ef41Sopenharmony_cino error handler is attached to the promise within a turn of the event loop. 4991cb0ef41Sopenharmony_ciWhen programming with Promises, exceptions are encapsulated as "rejected 5001cb0ef41Sopenharmony_cipromises". Rejections can be caught and handled using [`promise.catch()`][] and 5011cb0ef41Sopenharmony_ciare propagated through a `Promise` chain. The `'unhandledRejection'` event is 5021cb0ef41Sopenharmony_ciuseful for detecting and keeping track of promises that were rejected whose 5031cb0ef41Sopenharmony_cirejections have not yet been handled. 5041cb0ef41Sopenharmony_ci 5051cb0ef41Sopenharmony_ci```mjs 5061cb0ef41Sopenharmony_ciimport process from 'node:process'; 5071cb0ef41Sopenharmony_ci 5081cb0ef41Sopenharmony_ciprocess.on('unhandledRejection', (reason, promise) => { 5091cb0ef41Sopenharmony_ci console.log('Unhandled Rejection at:', promise, 'reason:', reason); 5101cb0ef41Sopenharmony_ci // Application specific logging, throwing an error, or other logic here 5111cb0ef41Sopenharmony_ci}); 5121cb0ef41Sopenharmony_ci 5131cb0ef41Sopenharmony_cisomePromise.then((res) => { 5141cb0ef41Sopenharmony_ci return reportToUser(JSON.pasre(res)); // Note the typo (`pasre`) 5151cb0ef41Sopenharmony_ci}); // No `.catch()` or `.then()` 5161cb0ef41Sopenharmony_ci``` 5171cb0ef41Sopenharmony_ci 5181cb0ef41Sopenharmony_ci```cjs 5191cb0ef41Sopenharmony_ciconst process = require('node:process'); 5201cb0ef41Sopenharmony_ci 5211cb0ef41Sopenharmony_ciprocess.on('unhandledRejection', (reason, promise) => { 5221cb0ef41Sopenharmony_ci console.log('Unhandled Rejection at:', promise, 'reason:', reason); 5231cb0ef41Sopenharmony_ci // Application specific logging, throwing an error, or other logic here 5241cb0ef41Sopenharmony_ci}); 5251cb0ef41Sopenharmony_ci 5261cb0ef41Sopenharmony_cisomePromise.then((res) => { 5271cb0ef41Sopenharmony_ci return reportToUser(JSON.pasre(res)); // Note the typo (`pasre`) 5281cb0ef41Sopenharmony_ci}); // No `.catch()` or `.then()` 5291cb0ef41Sopenharmony_ci``` 5301cb0ef41Sopenharmony_ci 5311cb0ef41Sopenharmony_ciThe following will also trigger the `'unhandledRejection'` event to be 5321cb0ef41Sopenharmony_ciemitted: 5331cb0ef41Sopenharmony_ci 5341cb0ef41Sopenharmony_ci```mjs 5351cb0ef41Sopenharmony_ciimport process from 'node:process'; 5361cb0ef41Sopenharmony_ci 5371cb0ef41Sopenharmony_cifunction SomeResource() { 5381cb0ef41Sopenharmony_ci // Initially set the loaded status to a rejected promise 5391cb0ef41Sopenharmony_ci this.loaded = Promise.reject(new Error('Resource not yet loaded!')); 5401cb0ef41Sopenharmony_ci} 5411cb0ef41Sopenharmony_ci 5421cb0ef41Sopenharmony_ciconst resource = new SomeResource(); 5431cb0ef41Sopenharmony_ci// no .catch or .then on resource.loaded for at least a turn 5441cb0ef41Sopenharmony_ci``` 5451cb0ef41Sopenharmony_ci 5461cb0ef41Sopenharmony_ci```cjs 5471cb0ef41Sopenharmony_ciconst process = require('node:process'); 5481cb0ef41Sopenharmony_ci 5491cb0ef41Sopenharmony_cifunction SomeResource() { 5501cb0ef41Sopenharmony_ci // Initially set the loaded status to a rejected promise 5511cb0ef41Sopenharmony_ci this.loaded = Promise.reject(new Error('Resource not yet loaded!')); 5521cb0ef41Sopenharmony_ci} 5531cb0ef41Sopenharmony_ci 5541cb0ef41Sopenharmony_ciconst resource = new SomeResource(); 5551cb0ef41Sopenharmony_ci// no .catch or .then on resource.loaded for at least a turn 5561cb0ef41Sopenharmony_ci``` 5571cb0ef41Sopenharmony_ci 5581cb0ef41Sopenharmony_ciIn this example case, it is possible to track the rejection as a developer error 5591cb0ef41Sopenharmony_cias would typically be the case for other `'unhandledRejection'` events. To 5601cb0ef41Sopenharmony_ciaddress such failures, a non-operational 5611cb0ef41Sopenharmony_ci[`.catch(() => { })`][`promise.catch()`] handler may be attached to 5621cb0ef41Sopenharmony_ci`resource.loaded`, which would prevent the `'unhandledRejection'` event from 5631cb0ef41Sopenharmony_cibeing emitted. 5641cb0ef41Sopenharmony_ci 5651cb0ef41Sopenharmony_ci### Event: `'warning'` 5661cb0ef41Sopenharmony_ci 5671cb0ef41Sopenharmony_ci<!-- YAML 5681cb0ef41Sopenharmony_ciadded: v6.0.0 5691cb0ef41Sopenharmony_ci--> 5701cb0ef41Sopenharmony_ci 5711cb0ef41Sopenharmony_ci* `warning` {Error} Key properties of the warning are: 5721cb0ef41Sopenharmony_ci * `name` {string} The name of the warning. **Default:** `'Warning'`. 5731cb0ef41Sopenharmony_ci * `message` {string} A system-provided description of the warning. 5741cb0ef41Sopenharmony_ci * `stack` {string} A stack trace to the location in the code where the warning 5751cb0ef41Sopenharmony_ci was issued. 5761cb0ef41Sopenharmony_ci 5771cb0ef41Sopenharmony_ciThe `'warning'` event is emitted whenever Node.js emits a process warning. 5781cb0ef41Sopenharmony_ci 5791cb0ef41Sopenharmony_ciA process warning is similar to an error in that it describes exceptional 5801cb0ef41Sopenharmony_ciconditions that are being brought to the user's attention. However, warnings 5811cb0ef41Sopenharmony_ciare not part of the normal Node.js and JavaScript error handling flow. 5821cb0ef41Sopenharmony_ciNode.js can emit warnings whenever it detects bad coding practices that could 5831cb0ef41Sopenharmony_cilead to sub-optimal application performance, bugs, or security vulnerabilities. 5841cb0ef41Sopenharmony_ci 5851cb0ef41Sopenharmony_ci```mjs 5861cb0ef41Sopenharmony_ciimport process from 'node:process'; 5871cb0ef41Sopenharmony_ci 5881cb0ef41Sopenharmony_ciprocess.on('warning', (warning) => { 5891cb0ef41Sopenharmony_ci console.warn(warning.name); // Print the warning name 5901cb0ef41Sopenharmony_ci console.warn(warning.message); // Print the warning message 5911cb0ef41Sopenharmony_ci console.warn(warning.stack); // Print the stack trace 5921cb0ef41Sopenharmony_ci}); 5931cb0ef41Sopenharmony_ci``` 5941cb0ef41Sopenharmony_ci 5951cb0ef41Sopenharmony_ci```cjs 5961cb0ef41Sopenharmony_ciconst process = require('node:process'); 5971cb0ef41Sopenharmony_ci 5981cb0ef41Sopenharmony_ciprocess.on('warning', (warning) => { 5991cb0ef41Sopenharmony_ci console.warn(warning.name); // Print the warning name 6001cb0ef41Sopenharmony_ci console.warn(warning.message); // Print the warning message 6011cb0ef41Sopenharmony_ci console.warn(warning.stack); // Print the stack trace 6021cb0ef41Sopenharmony_ci}); 6031cb0ef41Sopenharmony_ci``` 6041cb0ef41Sopenharmony_ci 6051cb0ef41Sopenharmony_ciBy default, Node.js will print process warnings to `stderr`. The `--no-warnings` 6061cb0ef41Sopenharmony_cicommand-line option can be used to suppress the default console output but the 6071cb0ef41Sopenharmony_ci`'warning'` event will still be emitted by the `process` object. 6081cb0ef41Sopenharmony_ci 6091cb0ef41Sopenharmony_ciThe following example illustrates the warning that is printed to `stderr` when 6101cb0ef41Sopenharmony_citoo many listeners have been added to an event: 6111cb0ef41Sopenharmony_ci 6121cb0ef41Sopenharmony_ci```console 6131cb0ef41Sopenharmony_ci$ node 6141cb0ef41Sopenharmony_ci> events.defaultMaxListeners = 1; 6151cb0ef41Sopenharmony_ci> process.on('foo', () => {}); 6161cb0ef41Sopenharmony_ci> process.on('foo', () => {}); 6171cb0ef41Sopenharmony_ci> (node:38638) MaxListenersExceededWarning: Possible EventEmitter memory leak 6181cb0ef41Sopenharmony_cidetected. 2 foo listeners added. Use emitter.setMaxListeners() to increase limit 6191cb0ef41Sopenharmony_ci``` 6201cb0ef41Sopenharmony_ci 6211cb0ef41Sopenharmony_ciIn contrast, the following example turns off the default warning output and 6221cb0ef41Sopenharmony_ciadds a custom handler to the `'warning'` event: 6231cb0ef41Sopenharmony_ci 6241cb0ef41Sopenharmony_ci```console 6251cb0ef41Sopenharmony_ci$ node --no-warnings 6261cb0ef41Sopenharmony_ci> const p = process.on('warning', (warning) => console.warn('Do not do that!')); 6271cb0ef41Sopenharmony_ci> events.defaultMaxListeners = 1; 6281cb0ef41Sopenharmony_ci> process.on('foo', () => {}); 6291cb0ef41Sopenharmony_ci> process.on('foo', () => {}); 6301cb0ef41Sopenharmony_ci> Do not do that! 6311cb0ef41Sopenharmony_ci``` 6321cb0ef41Sopenharmony_ci 6331cb0ef41Sopenharmony_ciThe `--trace-warnings` command-line option can be used to have the default 6341cb0ef41Sopenharmony_ciconsole output for warnings include the full stack trace of the warning. 6351cb0ef41Sopenharmony_ci 6361cb0ef41Sopenharmony_ciLaunching Node.js using the `--throw-deprecation` command-line flag will 6371cb0ef41Sopenharmony_cicause custom deprecation warnings to be thrown as exceptions. 6381cb0ef41Sopenharmony_ci 6391cb0ef41Sopenharmony_ciUsing the `--trace-deprecation` command-line flag will cause the custom 6401cb0ef41Sopenharmony_cideprecation to be printed to `stderr` along with the stack trace. 6411cb0ef41Sopenharmony_ci 6421cb0ef41Sopenharmony_ciUsing the `--no-deprecation` command-line flag will suppress all reporting 6431cb0ef41Sopenharmony_ciof the custom deprecation. 6441cb0ef41Sopenharmony_ci 6451cb0ef41Sopenharmony_ciThe `*-deprecation` command-line flags only affect warnings that use the name 6461cb0ef41Sopenharmony_ci`'DeprecationWarning'`. 6471cb0ef41Sopenharmony_ci 6481cb0ef41Sopenharmony_ci### Event: `'worker'` 6491cb0ef41Sopenharmony_ci 6501cb0ef41Sopenharmony_ci<!-- YAML 6511cb0ef41Sopenharmony_ciadded: 6521cb0ef41Sopenharmony_ci - v16.2.0 6531cb0ef41Sopenharmony_ci - v14.18.0 6541cb0ef41Sopenharmony_ci--> 6551cb0ef41Sopenharmony_ci 6561cb0ef41Sopenharmony_ci* `worker` {Worker} The {Worker} that was created. 6571cb0ef41Sopenharmony_ci 6581cb0ef41Sopenharmony_ciThe `'worker'` event is emitted after a new {Worker} thread has been created. 6591cb0ef41Sopenharmony_ci 6601cb0ef41Sopenharmony_ci#### Emitting custom warnings 6611cb0ef41Sopenharmony_ci 6621cb0ef41Sopenharmony_ciSee the [`process.emitWarning()`][process_emit_warning] method for issuing 6631cb0ef41Sopenharmony_cicustom or application-specific warnings. 6641cb0ef41Sopenharmony_ci 6651cb0ef41Sopenharmony_ci#### Node.js warning names 6661cb0ef41Sopenharmony_ci 6671cb0ef41Sopenharmony_ciThere are no strict guidelines for warning types (as identified by the `name` 6681cb0ef41Sopenharmony_ciproperty) emitted by Node.js. New types of warnings can be added at any time. 6691cb0ef41Sopenharmony_ciA few of the warning types that are most common include: 6701cb0ef41Sopenharmony_ci 6711cb0ef41Sopenharmony_ci* `'DeprecationWarning'` - Indicates use of a deprecated Node.js API or feature. 6721cb0ef41Sopenharmony_ci Such warnings must include a `'code'` property identifying the 6731cb0ef41Sopenharmony_ci [deprecation code][]. 6741cb0ef41Sopenharmony_ci* `'ExperimentalWarning'` - Indicates use of an experimental Node.js API or 6751cb0ef41Sopenharmony_ci feature. Such features must be used with caution as they may change at any 6761cb0ef41Sopenharmony_ci time and are not subject to the same strict semantic-versioning and long-term 6771cb0ef41Sopenharmony_ci support policies as supported features. 6781cb0ef41Sopenharmony_ci* `'MaxListenersExceededWarning'` - Indicates that too many listeners for a 6791cb0ef41Sopenharmony_ci given event have been registered on either an `EventEmitter` or `EventTarget`. 6801cb0ef41Sopenharmony_ci This is often an indication of a memory leak. 6811cb0ef41Sopenharmony_ci* `'TimeoutOverflowWarning'` - Indicates that a numeric value that cannot fit 6821cb0ef41Sopenharmony_ci within a 32-bit signed integer has been provided to either the `setTimeout()` 6831cb0ef41Sopenharmony_ci or `setInterval()` functions. 6841cb0ef41Sopenharmony_ci* `'UnsupportedWarning'` - Indicates use of an unsupported option or feature 6851cb0ef41Sopenharmony_ci that will be ignored rather than treated as an error. One example is use of 6861cb0ef41Sopenharmony_ci the HTTP response status message when using the HTTP/2 compatibility API. 6871cb0ef41Sopenharmony_ci 6881cb0ef41Sopenharmony_ci### Signal events 6891cb0ef41Sopenharmony_ci 6901cb0ef41Sopenharmony_ci<!--type=event--> 6911cb0ef41Sopenharmony_ci 6921cb0ef41Sopenharmony_ci<!--name=SIGINT, SIGHUP, etc.--> 6931cb0ef41Sopenharmony_ci 6941cb0ef41Sopenharmony_ciSignal events will be emitted when the Node.js process receives a signal. Please 6951cb0ef41Sopenharmony_cirefer to signal(7) for a listing of standard POSIX signal names such as 6961cb0ef41Sopenharmony_ci`'SIGINT'`, `'SIGHUP'`, etc. 6971cb0ef41Sopenharmony_ci 6981cb0ef41Sopenharmony_ciSignals are not available on [`Worker`][] threads. 6991cb0ef41Sopenharmony_ci 7001cb0ef41Sopenharmony_ciThe signal handler will receive the signal's name (`'SIGINT'`, 7011cb0ef41Sopenharmony_ci`'SIGTERM'`, etc.) as the first argument. 7021cb0ef41Sopenharmony_ci 7031cb0ef41Sopenharmony_ciThe name of each event will be the uppercase common name for the signal (e.g. 7041cb0ef41Sopenharmony_ci`'SIGINT'` for `SIGINT` signals). 7051cb0ef41Sopenharmony_ci 7061cb0ef41Sopenharmony_ci```mjs 7071cb0ef41Sopenharmony_ciimport process from 'node:process'; 7081cb0ef41Sopenharmony_ci 7091cb0ef41Sopenharmony_ci// Begin reading from stdin so the process does not exit. 7101cb0ef41Sopenharmony_ciprocess.stdin.resume(); 7111cb0ef41Sopenharmony_ci 7121cb0ef41Sopenharmony_ciprocess.on('SIGINT', () => { 7131cb0ef41Sopenharmony_ci console.log('Received SIGINT. Press Control-D to exit.'); 7141cb0ef41Sopenharmony_ci}); 7151cb0ef41Sopenharmony_ci 7161cb0ef41Sopenharmony_ci// Using a single function to handle multiple signals 7171cb0ef41Sopenharmony_cifunction handle(signal) { 7181cb0ef41Sopenharmony_ci console.log(`Received ${signal}`); 7191cb0ef41Sopenharmony_ci} 7201cb0ef41Sopenharmony_ci 7211cb0ef41Sopenharmony_ciprocess.on('SIGINT', handle); 7221cb0ef41Sopenharmony_ciprocess.on('SIGTERM', handle); 7231cb0ef41Sopenharmony_ci``` 7241cb0ef41Sopenharmony_ci 7251cb0ef41Sopenharmony_ci```cjs 7261cb0ef41Sopenharmony_ciconst process = require('node:process'); 7271cb0ef41Sopenharmony_ci 7281cb0ef41Sopenharmony_ci// Begin reading from stdin so the process does not exit. 7291cb0ef41Sopenharmony_ciprocess.stdin.resume(); 7301cb0ef41Sopenharmony_ci 7311cb0ef41Sopenharmony_ciprocess.on('SIGINT', () => { 7321cb0ef41Sopenharmony_ci console.log('Received SIGINT. Press Control-D to exit.'); 7331cb0ef41Sopenharmony_ci}); 7341cb0ef41Sopenharmony_ci 7351cb0ef41Sopenharmony_ci// Using a single function to handle multiple signals 7361cb0ef41Sopenharmony_cifunction handle(signal) { 7371cb0ef41Sopenharmony_ci console.log(`Received ${signal}`); 7381cb0ef41Sopenharmony_ci} 7391cb0ef41Sopenharmony_ci 7401cb0ef41Sopenharmony_ciprocess.on('SIGINT', handle); 7411cb0ef41Sopenharmony_ciprocess.on('SIGTERM', handle); 7421cb0ef41Sopenharmony_ci``` 7431cb0ef41Sopenharmony_ci 7441cb0ef41Sopenharmony_ci* `'SIGUSR1'` is reserved by Node.js to start the [debugger][]. It's possible to 7451cb0ef41Sopenharmony_ci install a listener but doing so might interfere with the debugger. 7461cb0ef41Sopenharmony_ci* `'SIGTERM'` and `'SIGINT'` have default handlers on non-Windows platforms that 7471cb0ef41Sopenharmony_ci reset the terminal mode before exiting with code `128 + signal number`. If one 7481cb0ef41Sopenharmony_ci of these signals has a listener installed, its default behavior will be 7491cb0ef41Sopenharmony_ci removed (Node.js will no longer exit). 7501cb0ef41Sopenharmony_ci* `'SIGPIPE'` is ignored by default. It can have a listener installed. 7511cb0ef41Sopenharmony_ci* `'SIGHUP'` is generated on Windows when the console window is closed, and on 7521cb0ef41Sopenharmony_ci other platforms under various similar conditions. See signal(7). It can have a 7531cb0ef41Sopenharmony_ci listener installed, however Node.js will be unconditionally terminated by 7541cb0ef41Sopenharmony_ci Windows about 10 seconds later. On non-Windows platforms, the default 7551cb0ef41Sopenharmony_ci behavior of `SIGHUP` is to terminate Node.js, but once a listener has been 7561cb0ef41Sopenharmony_ci installed its default behavior will be removed. 7571cb0ef41Sopenharmony_ci* `'SIGTERM'` is not supported on Windows, it can be listened on. 7581cb0ef41Sopenharmony_ci* `'SIGINT'` from the terminal is supported on all platforms, and can usually be 7591cb0ef41Sopenharmony_ci generated with <kbd>Ctrl</kbd>+<kbd>C</kbd> (though this may be configurable). 7601cb0ef41Sopenharmony_ci It is not generated when [terminal raw mode][] is enabled 7611cb0ef41Sopenharmony_ci and <kbd>Ctrl</kbd>+<kbd>C</kbd> is used. 7621cb0ef41Sopenharmony_ci* `'SIGBREAK'` is delivered on Windows when <kbd>Ctrl</kbd>+<kbd>Break</kbd> is 7631cb0ef41Sopenharmony_ci pressed. On non-Windows platforms, it can be listened on, but there is no way 7641cb0ef41Sopenharmony_ci to send or generate it. 7651cb0ef41Sopenharmony_ci* `'SIGWINCH'` is delivered when the console has been resized. On Windows, this 7661cb0ef41Sopenharmony_ci will only happen on write to the console when the cursor is being moved, or 7671cb0ef41Sopenharmony_ci when a readable tty is used in raw mode. 7681cb0ef41Sopenharmony_ci* `'SIGKILL'` cannot have a listener installed, it will unconditionally 7691cb0ef41Sopenharmony_ci terminate Node.js on all platforms. 7701cb0ef41Sopenharmony_ci* `'SIGSTOP'` cannot have a listener installed. 7711cb0ef41Sopenharmony_ci* `'SIGBUS'`, `'SIGFPE'`, `'SIGSEGV'`, and `'SIGILL'`, when not raised 7721cb0ef41Sopenharmony_ci artificially using kill(2), inherently leave the process in a state from 7731cb0ef41Sopenharmony_ci which it is not safe to call JS listeners. Doing so might cause the process 7741cb0ef41Sopenharmony_ci to stop responding. 7751cb0ef41Sopenharmony_ci* `0` can be sent to test for the existence of a process, it has no effect if 7761cb0ef41Sopenharmony_ci the process exists, but will throw an error if the process does not exist. 7771cb0ef41Sopenharmony_ci 7781cb0ef41Sopenharmony_ciWindows does not support signals so has no equivalent to termination by signal, 7791cb0ef41Sopenharmony_cibut Node.js offers some emulation with [`process.kill()`][], and 7801cb0ef41Sopenharmony_ci[`subprocess.kill()`][]: 7811cb0ef41Sopenharmony_ci 7821cb0ef41Sopenharmony_ci* Sending `SIGINT`, `SIGTERM`, and `SIGKILL` will cause the unconditional 7831cb0ef41Sopenharmony_ci termination of the target process, and afterwards, subprocess will report that 7841cb0ef41Sopenharmony_ci the process was terminated by signal. 7851cb0ef41Sopenharmony_ci* Sending signal `0` can be used as a platform independent way to test for the 7861cb0ef41Sopenharmony_ci existence of a process. 7871cb0ef41Sopenharmony_ci 7881cb0ef41Sopenharmony_ci## `process.abort()` 7891cb0ef41Sopenharmony_ci 7901cb0ef41Sopenharmony_ci<!-- YAML 7911cb0ef41Sopenharmony_ciadded: v0.7.0 7921cb0ef41Sopenharmony_ci--> 7931cb0ef41Sopenharmony_ci 7941cb0ef41Sopenharmony_ciThe `process.abort()` method causes the Node.js process to exit immediately and 7951cb0ef41Sopenharmony_cigenerate a core file. 7961cb0ef41Sopenharmony_ci 7971cb0ef41Sopenharmony_ciThis feature is not available in [`Worker`][] threads. 7981cb0ef41Sopenharmony_ci 7991cb0ef41Sopenharmony_ci## `process.allowedNodeEnvironmentFlags` 8001cb0ef41Sopenharmony_ci 8011cb0ef41Sopenharmony_ci<!-- YAML 8021cb0ef41Sopenharmony_ciadded: v10.10.0 8031cb0ef41Sopenharmony_ci--> 8041cb0ef41Sopenharmony_ci 8051cb0ef41Sopenharmony_ci* {Set} 8061cb0ef41Sopenharmony_ci 8071cb0ef41Sopenharmony_ciThe `process.allowedNodeEnvironmentFlags` property is a special, 8081cb0ef41Sopenharmony_ciread-only `Set` of flags allowable within the [`NODE_OPTIONS`][] 8091cb0ef41Sopenharmony_cienvironment variable. 8101cb0ef41Sopenharmony_ci 8111cb0ef41Sopenharmony_ci`process.allowedNodeEnvironmentFlags` extends `Set`, but overrides 8121cb0ef41Sopenharmony_ci`Set.prototype.has` to recognize several different possible flag 8131cb0ef41Sopenharmony_cirepresentations. `process.allowedNodeEnvironmentFlags.has()` will 8141cb0ef41Sopenharmony_cireturn `true` in the following cases: 8151cb0ef41Sopenharmony_ci 8161cb0ef41Sopenharmony_ci* Flags may omit leading single (`-`) or double (`--`) dashes; e.g., 8171cb0ef41Sopenharmony_ci `inspect-brk` for `--inspect-brk`, or `r` for `-r`. 8181cb0ef41Sopenharmony_ci* Flags passed through to V8 (as listed in `--v8-options`) may replace 8191cb0ef41Sopenharmony_ci one or more _non-leading_ dashes for an underscore, or vice-versa; 8201cb0ef41Sopenharmony_ci e.g., `--perf_basic_prof`, `--perf-basic-prof`, `--perf_basic-prof`, 8211cb0ef41Sopenharmony_ci etc. 8221cb0ef41Sopenharmony_ci* Flags may contain one or more equals (`=`) characters; all 8231cb0ef41Sopenharmony_ci characters after and including the first equals will be ignored; 8241cb0ef41Sopenharmony_ci e.g., `--stack-trace-limit=100`. 8251cb0ef41Sopenharmony_ci* Flags _must_ be allowable within [`NODE_OPTIONS`][]. 8261cb0ef41Sopenharmony_ci 8271cb0ef41Sopenharmony_ciWhen iterating over `process.allowedNodeEnvironmentFlags`, flags will 8281cb0ef41Sopenharmony_ciappear only _once_; each will begin with one or more dashes. Flags 8291cb0ef41Sopenharmony_cipassed through to V8 will contain underscores instead of non-leading 8301cb0ef41Sopenharmony_cidashes: 8311cb0ef41Sopenharmony_ci 8321cb0ef41Sopenharmony_ci```mjs 8331cb0ef41Sopenharmony_ciimport { allowedNodeEnvironmentFlags } from 'node:process'; 8341cb0ef41Sopenharmony_ci 8351cb0ef41Sopenharmony_ciallowedNodeEnvironmentFlags.forEach((flag) => { 8361cb0ef41Sopenharmony_ci // -r 8371cb0ef41Sopenharmony_ci // --inspect-brk 8381cb0ef41Sopenharmony_ci // --abort_on_uncaught_exception 8391cb0ef41Sopenharmony_ci // ... 8401cb0ef41Sopenharmony_ci}); 8411cb0ef41Sopenharmony_ci``` 8421cb0ef41Sopenharmony_ci 8431cb0ef41Sopenharmony_ci```cjs 8441cb0ef41Sopenharmony_ciconst { allowedNodeEnvironmentFlags } = require('node:process'); 8451cb0ef41Sopenharmony_ci 8461cb0ef41Sopenharmony_ciallowedNodeEnvironmentFlags.forEach((flag) => { 8471cb0ef41Sopenharmony_ci // -r 8481cb0ef41Sopenharmony_ci // --inspect-brk 8491cb0ef41Sopenharmony_ci // --abort_on_uncaught_exception 8501cb0ef41Sopenharmony_ci // ... 8511cb0ef41Sopenharmony_ci}); 8521cb0ef41Sopenharmony_ci``` 8531cb0ef41Sopenharmony_ci 8541cb0ef41Sopenharmony_ciThe methods `add()`, `clear()`, and `delete()` of 8551cb0ef41Sopenharmony_ci`process.allowedNodeEnvironmentFlags` do nothing, and will fail 8561cb0ef41Sopenharmony_cisilently. 8571cb0ef41Sopenharmony_ci 8581cb0ef41Sopenharmony_ciIf Node.js was compiled _without_ [`NODE_OPTIONS`][] support (shown in 8591cb0ef41Sopenharmony_ci[`process.config`][]), `process.allowedNodeEnvironmentFlags` will 8601cb0ef41Sopenharmony_cicontain what _would have_ been allowable. 8611cb0ef41Sopenharmony_ci 8621cb0ef41Sopenharmony_ci## `process.arch` 8631cb0ef41Sopenharmony_ci 8641cb0ef41Sopenharmony_ci<!-- YAML 8651cb0ef41Sopenharmony_ciadded: v0.5.0 8661cb0ef41Sopenharmony_ci--> 8671cb0ef41Sopenharmony_ci 8681cb0ef41Sopenharmony_ci* {string} 8691cb0ef41Sopenharmony_ci 8701cb0ef41Sopenharmony_ciThe operating system CPU architecture for which the Node.js binary was compiled. 8711cb0ef41Sopenharmony_ciPossible values are: `'arm'`, `'arm64'`, `'ia32'`, `'mips'`,`'mipsel'`, `'ppc'`, 8721cb0ef41Sopenharmony_ci`'ppc64'`, `'s390'`, `'s390x'`, and `'x64'`. 8731cb0ef41Sopenharmony_ci 8741cb0ef41Sopenharmony_ci```mjs 8751cb0ef41Sopenharmony_ciimport { arch } from 'node:process'; 8761cb0ef41Sopenharmony_ci 8771cb0ef41Sopenharmony_ciconsole.log(`This processor architecture is ${arch}`); 8781cb0ef41Sopenharmony_ci``` 8791cb0ef41Sopenharmony_ci 8801cb0ef41Sopenharmony_ci```cjs 8811cb0ef41Sopenharmony_ciconst { arch } = require('node:process'); 8821cb0ef41Sopenharmony_ci 8831cb0ef41Sopenharmony_ciconsole.log(`This processor architecture is ${arch}`); 8841cb0ef41Sopenharmony_ci``` 8851cb0ef41Sopenharmony_ci 8861cb0ef41Sopenharmony_ci## `process.argv` 8871cb0ef41Sopenharmony_ci 8881cb0ef41Sopenharmony_ci<!-- YAML 8891cb0ef41Sopenharmony_ciadded: v0.1.27 8901cb0ef41Sopenharmony_ci--> 8911cb0ef41Sopenharmony_ci 8921cb0ef41Sopenharmony_ci* {string\[]} 8931cb0ef41Sopenharmony_ci 8941cb0ef41Sopenharmony_ciThe `process.argv` property returns an array containing the command-line 8951cb0ef41Sopenharmony_ciarguments passed when the Node.js process was launched. The first element will 8961cb0ef41Sopenharmony_cibe [`process.execPath`][]. See `process.argv0` if access to the original value 8971cb0ef41Sopenharmony_ciof `argv[0]` is needed. The second element will be the path to the JavaScript 8981cb0ef41Sopenharmony_cifile being executed. The remaining elements will be any additional command-line 8991cb0ef41Sopenharmony_ciarguments. 9001cb0ef41Sopenharmony_ci 9011cb0ef41Sopenharmony_ciFor example, assuming the following script for `process-args.js`: 9021cb0ef41Sopenharmony_ci 9031cb0ef41Sopenharmony_ci```mjs 9041cb0ef41Sopenharmony_ciimport { argv } from 'node:process'; 9051cb0ef41Sopenharmony_ci 9061cb0ef41Sopenharmony_ci// print process.argv 9071cb0ef41Sopenharmony_ciargv.forEach((val, index) => { 9081cb0ef41Sopenharmony_ci console.log(`${index}: ${val}`); 9091cb0ef41Sopenharmony_ci}); 9101cb0ef41Sopenharmony_ci``` 9111cb0ef41Sopenharmony_ci 9121cb0ef41Sopenharmony_ci```cjs 9131cb0ef41Sopenharmony_ciconst { argv } = require('node:process'); 9141cb0ef41Sopenharmony_ci 9151cb0ef41Sopenharmony_ci// print process.argv 9161cb0ef41Sopenharmony_ciargv.forEach((val, index) => { 9171cb0ef41Sopenharmony_ci console.log(`${index}: ${val}`); 9181cb0ef41Sopenharmony_ci}); 9191cb0ef41Sopenharmony_ci``` 9201cb0ef41Sopenharmony_ci 9211cb0ef41Sopenharmony_ciLaunching the Node.js process as: 9221cb0ef41Sopenharmony_ci 9231cb0ef41Sopenharmony_ci```console 9241cb0ef41Sopenharmony_ci$ node process-args.js one two=three four 9251cb0ef41Sopenharmony_ci``` 9261cb0ef41Sopenharmony_ci 9271cb0ef41Sopenharmony_ciWould generate the output: 9281cb0ef41Sopenharmony_ci 9291cb0ef41Sopenharmony_ci```text 9301cb0ef41Sopenharmony_ci0: /usr/local/bin/node 9311cb0ef41Sopenharmony_ci1: /Users/mjr/work/node/process-args.js 9321cb0ef41Sopenharmony_ci2: one 9331cb0ef41Sopenharmony_ci3: two=three 9341cb0ef41Sopenharmony_ci4: four 9351cb0ef41Sopenharmony_ci``` 9361cb0ef41Sopenharmony_ci 9371cb0ef41Sopenharmony_ci## `process.argv0` 9381cb0ef41Sopenharmony_ci 9391cb0ef41Sopenharmony_ci<!-- YAML 9401cb0ef41Sopenharmony_ciadded: v6.4.0 9411cb0ef41Sopenharmony_ci--> 9421cb0ef41Sopenharmony_ci 9431cb0ef41Sopenharmony_ci* {string} 9441cb0ef41Sopenharmony_ci 9451cb0ef41Sopenharmony_ciThe `process.argv0` property stores a read-only copy of the original value of 9461cb0ef41Sopenharmony_ci`argv[0]` passed when Node.js starts. 9471cb0ef41Sopenharmony_ci 9481cb0ef41Sopenharmony_ci```console 9491cb0ef41Sopenharmony_ci$ bash -c 'exec -a customArgv0 ./node' 9501cb0ef41Sopenharmony_ci> process.argv[0] 9511cb0ef41Sopenharmony_ci'/Volumes/code/external/node/out/Release/node' 9521cb0ef41Sopenharmony_ci> process.argv0 9531cb0ef41Sopenharmony_ci'customArgv0' 9541cb0ef41Sopenharmony_ci``` 9551cb0ef41Sopenharmony_ci 9561cb0ef41Sopenharmony_ci## `process.channel` 9571cb0ef41Sopenharmony_ci 9581cb0ef41Sopenharmony_ci<!-- YAML 9591cb0ef41Sopenharmony_ciadded: v7.1.0 9601cb0ef41Sopenharmony_cichanges: 9611cb0ef41Sopenharmony_ci - version: v14.0.0 9621cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/30165 9631cb0ef41Sopenharmony_ci description: The object no longer accidentally exposes native C++ bindings. 9641cb0ef41Sopenharmony_ci--> 9651cb0ef41Sopenharmony_ci 9661cb0ef41Sopenharmony_ci* {Object} 9671cb0ef41Sopenharmony_ci 9681cb0ef41Sopenharmony_ciIf the Node.js process was spawned with an IPC channel (see the 9691cb0ef41Sopenharmony_ci[Child Process][] documentation), the `process.channel` 9701cb0ef41Sopenharmony_ciproperty is a reference to the IPC channel. If no IPC channel exists, this 9711cb0ef41Sopenharmony_ciproperty is `undefined`. 9721cb0ef41Sopenharmony_ci 9731cb0ef41Sopenharmony_ci### `process.channel.ref()` 9741cb0ef41Sopenharmony_ci 9751cb0ef41Sopenharmony_ci<!-- YAML 9761cb0ef41Sopenharmony_ciadded: v7.1.0 9771cb0ef41Sopenharmony_ci--> 9781cb0ef41Sopenharmony_ci 9791cb0ef41Sopenharmony_ciThis method makes the IPC channel keep the event loop of the process 9801cb0ef41Sopenharmony_cirunning if `.unref()` has been called before. 9811cb0ef41Sopenharmony_ci 9821cb0ef41Sopenharmony_ciTypically, this is managed through the number of `'disconnect'` and `'message'` 9831cb0ef41Sopenharmony_cilisteners on the `process` object. However, this method can be used to 9841cb0ef41Sopenharmony_ciexplicitly request a specific behavior. 9851cb0ef41Sopenharmony_ci 9861cb0ef41Sopenharmony_ci### `process.channel.unref()` 9871cb0ef41Sopenharmony_ci 9881cb0ef41Sopenharmony_ci<!-- YAML 9891cb0ef41Sopenharmony_ciadded: v7.1.0 9901cb0ef41Sopenharmony_ci--> 9911cb0ef41Sopenharmony_ci 9921cb0ef41Sopenharmony_ciThis method makes the IPC channel not keep the event loop of the process 9931cb0ef41Sopenharmony_cirunning, and lets it finish even while the channel is open. 9941cb0ef41Sopenharmony_ci 9951cb0ef41Sopenharmony_ciTypically, this is managed through the number of `'disconnect'` and `'message'` 9961cb0ef41Sopenharmony_cilisteners on the `process` object. However, this method can be used to 9971cb0ef41Sopenharmony_ciexplicitly request a specific behavior. 9981cb0ef41Sopenharmony_ci 9991cb0ef41Sopenharmony_ci## `process.chdir(directory)` 10001cb0ef41Sopenharmony_ci 10011cb0ef41Sopenharmony_ci<!-- YAML 10021cb0ef41Sopenharmony_ciadded: v0.1.17 10031cb0ef41Sopenharmony_ci--> 10041cb0ef41Sopenharmony_ci 10051cb0ef41Sopenharmony_ci* `directory` {string} 10061cb0ef41Sopenharmony_ci 10071cb0ef41Sopenharmony_ciThe `process.chdir()` method changes the current working directory of the 10081cb0ef41Sopenharmony_ciNode.js process or throws an exception if doing so fails (for instance, if 10091cb0ef41Sopenharmony_cithe specified `directory` does not exist). 10101cb0ef41Sopenharmony_ci 10111cb0ef41Sopenharmony_ci```mjs 10121cb0ef41Sopenharmony_ciimport { chdir, cwd } from 'node:process'; 10131cb0ef41Sopenharmony_ci 10141cb0ef41Sopenharmony_ciconsole.log(`Starting directory: ${cwd()}`); 10151cb0ef41Sopenharmony_citry { 10161cb0ef41Sopenharmony_ci chdir('/tmp'); 10171cb0ef41Sopenharmony_ci console.log(`New directory: ${cwd()}`); 10181cb0ef41Sopenharmony_ci} catch (err) { 10191cb0ef41Sopenharmony_ci console.error(`chdir: ${err}`); 10201cb0ef41Sopenharmony_ci} 10211cb0ef41Sopenharmony_ci``` 10221cb0ef41Sopenharmony_ci 10231cb0ef41Sopenharmony_ci```cjs 10241cb0ef41Sopenharmony_ciconst { chdir, cwd } = require('node:process'); 10251cb0ef41Sopenharmony_ci 10261cb0ef41Sopenharmony_ciconsole.log(`Starting directory: ${cwd()}`); 10271cb0ef41Sopenharmony_citry { 10281cb0ef41Sopenharmony_ci chdir('/tmp'); 10291cb0ef41Sopenharmony_ci console.log(`New directory: ${cwd()}`); 10301cb0ef41Sopenharmony_ci} catch (err) { 10311cb0ef41Sopenharmony_ci console.error(`chdir: ${err}`); 10321cb0ef41Sopenharmony_ci} 10331cb0ef41Sopenharmony_ci``` 10341cb0ef41Sopenharmony_ci 10351cb0ef41Sopenharmony_ciThis feature is not available in [`Worker`][] threads. 10361cb0ef41Sopenharmony_ci 10371cb0ef41Sopenharmony_ci## `process.config` 10381cb0ef41Sopenharmony_ci 10391cb0ef41Sopenharmony_ci<!-- YAML 10401cb0ef41Sopenharmony_ciadded: v0.7.7 10411cb0ef41Sopenharmony_cichanges: 10421cb0ef41Sopenharmony_ci - version: v16.0.0 10431cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/36902 10441cb0ef41Sopenharmony_ci description: Modifying process.config has been deprecated. 10451cb0ef41Sopenharmony_ci--> 10461cb0ef41Sopenharmony_ci 10471cb0ef41Sopenharmony_ci* {Object} 10481cb0ef41Sopenharmony_ci 10491cb0ef41Sopenharmony_ciThe `process.config` property returns an `Object` containing the JavaScript 10501cb0ef41Sopenharmony_cirepresentation of the configure options used to compile the current Node.js 10511cb0ef41Sopenharmony_ciexecutable. This is the same as the `config.gypi` file that was produced when 10521cb0ef41Sopenharmony_cirunning the `./configure` script. 10531cb0ef41Sopenharmony_ci 10541cb0ef41Sopenharmony_ciAn example of the possible output looks like: 10551cb0ef41Sopenharmony_ci 10561cb0ef41Sopenharmony_ci<!-- eslint-skip --> 10571cb0ef41Sopenharmony_ci 10581cb0ef41Sopenharmony_ci```js 10591cb0ef41Sopenharmony_ci{ 10601cb0ef41Sopenharmony_ci target_defaults: 10611cb0ef41Sopenharmony_ci { cflags: [], 10621cb0ef41Sopenharmony_ci default_configuration: 'Release', 10631cb0ef41Sopenharmony_ci defines: [], 10641cb0ef41Sopenharmony_ci include_dirs: [], 10651cb0ef41Sopenharmony_ci libraries: [] }, 10661cb0ef41Sopenharmony_ci variables: 10671cb0ef41Sopenharmony_ci { 10681cb0ef41Sopenharmony_ci host_arch: 'x64', 10691cb0ef41Sopenharmony_ci napi_build_version: 5, 10701cb0ef41Sopenharmony_ci node_install_npm: 'true', 10711cb0ef41Sopenharmony_ci node_prefix: '', 10721cb0ef41Sopenharmony_ci node_shared_cares: 'false', 10731cb0ef41Sopenharmony_ci node_shared_http_parser: 'false', 10741cb0ef41Sopenharmony_ci node_shared_libuv: 'false', 10751cb0ef41Sopenharmony_ci node_shared_zlib: 'false', 10761cb0ef41Sopenharmony_ci node_use_dtrace: 'false', 10771cb0ef41Sopenharmony_ci node_use_openssl: 'true', 10781cb0ef41Sopenharmony_ci node_shared_openssl: 'false', 10791cb0ef41Sopenharmony_ci strict_aliasing: 'true', 10801cb0ef41Sopenharmony_ci target_arch: 'x64', 10811cb0ef41Sopenharmony_ci v8_use_snapshot: 1 10821cb0ef41Sopenharmony_ci } 10831cb0ef41Sopenharmony_ci} 10841cb0ef41Sopenharmony_ci``` 10851cb0ef41Sopenharmony_ci 10861cb0ef41Sopenharmony_ciThe `process.config` property is **not** read-only and there are existing 10871cb0ef41Sopenharmony_cimodules in the ecosystem that are known to extend, modify, or entirely replace 10881cb0ef41Sopenharmony_cithe value of `process.config`. 10891cb0ef41Sopenharmony_ci 10901cb0ef41Sopenharmony_ciModifying the `process.config` property, or any child-property of the 10911cb0ef41Sopenharmony_ci`process.config` object has been deprecated. The `process.config` will be made 10921cb0ef41Sopenharmony_ciread-only in a future release. 10931cb0ef41Sopenharmony_ci 10941cb0ef41Sopenharmony_ci## `process.connected` 10951cb0ef41Sopenharmony_ci 10961cb0ef41Sopenharmony_ci<!-- YAML 10971cb0ef41Sopenharmony_ciadded: v0.7.2 10981cb0ef41Sopenharmony_ci--> 10991cb0ef41Sopenharmony_ci 11001cb0ef41Sopenharmony_ci* {boolean} 11011cb0ef41Sopenharmony_ci 11021cb0ef41Sopenharmony_ciIf the Node.js process is spawned with an IPC channel (see the [Child Process][] 11031cb0ef41Sopenharmony_ciand [Cluster][] documentation), the `process.connected` property will return 11041cb0ef41Sopenharmony_ci`true` so long as the IPC channel is connected and will return `false` after 11051cb0ef41Sopenharmony_ci`process.disconnect()` is called. 11061cb0ef41Sopenharmony_ci 11071cb0ef41Sopenharmony_ciOnce `process.connected` is `false`, it is no longer possible to send messages 11081cb0ef41Sopenharmony_ciover the IPC channel using `process.send()`. 11091cb0ef41Sopenharmony_ci 11101cb0ef41Sopenharmony_ci## `process.constrainedMemory()` 11111cb0ef41Sopenharmony_ci 11121cb0ef41Sopenharmony_ci<!-- YAML 11131cb0ef41Sopenharmony_ciadded: v18.15.0 11141cb0ef41Sopenharmony_ci--> 11151cb0ef41Sopenharmony_ci 11161cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 11171cb0ef41Sopenharmony_ci 11181cb0ef41Sopenharmony_ci* {number|undefined} 11191cb0ef41Sopenharmony_ci 11201cb0ef41Sopenharmony_ciGets the amount of memory available to the process (in bytes) based on 11211cb0ef41Sopenharmony_cilimits imposed by the OS. If there is no such constraint, or the constraint 11221cb0ef41Sopenharmony_ciis unknown, `undefined` is returned. 11231cb0ef41Sopenharmony_ci 11241cb0ef41Sopenharmony_ciSee [`uv_get_constrained_memory`][uv_get_constrained_memory] for more 11251cb0ef41Sopenharmony_ciinformation. 11261cb0ef41Sopenharmony_ci 11271cb0ef41Sopenharmony_ci## `process.cpuUsage([previousValue])` 11281cb0ef41Sopenharmony_ci 11291cb0ef41Sopenharmony_ci<!-- YAML 11301cb0ef41Sopenharmony_ciadded: v6.1.0 11311cb0ef41Sopenharmony_ci--> 11321cb0ef41Sopenharmony_ci 11331cb0ef41Sopenharmony_ci* `previousValue` {Object} A previous return value from calling 11341cb0ef41Sopenharmony_ci `process.cpuUsage()` 11351cb0ef41Sopenharmony_ci* Returns: {Object} 11361cb0ef41Sopenharmony_ci * `user` {integer} 11371cb0ef41Sopenharmony_ci * `system` {integer} 11381cb0ef41Sopenharmony_ci 11391cb0ef41Sopenharmony_ciThe `process.cpuUsage()` method returns the user and system CPU time usage of 11401cb0ef41Sopenharmony_cithe current process, in an object with properties `user` and `system`, whose 11411cb0ef41Sopenharmony_civalues are microsecond values (millionth of a second). These values measure time 11421cb0ef41Sopenharmony_cispent in user and system code respectively, and may end up being greater than 11431cb0ef41Sopenharmony_ciactual elapsed time if multiple CPU cores are performing work for this process. 11441cb0ef41Sopenharmony_ci 11451cb0ef41Sopenharmony_ciThe result of a previous call to `process.cpuUsage()` can be passed as the 11461cb0ef41Sopenharmony_ciargument to the function, to get a diff reading. 11471cb0ef41Sopenharmony_ci 11481cb0ef41Sopenharmony_ci```mjs 11491cb0ef41Sopenharmony_ciimport { cpuUsage } from 'node:process'; 11501cb0ef41Sopenharmony_ci 11511cb0ef41Sopenharmony_ciconst startUsage = cpuUsage(); 11521cb0ef41Sopenharmony_ci// { user: 38579, system: 6986 } 11531cb0ef41Sopenharmony_ci 11541cb0ef41Sopenharmony_ci// spin the CPU for 500 milliseconds 11551cb0ef41Sopenharmony_ciconst now = Date.now(); 11561cb0ef41Sopenharmony_ciwhile (Date.now() - now < 500); 11571cb0ef41Sopenharmony_ci 11581cb0ef41Sopenharmony_ciconsole.log(cpuUsage(startUsage)); 11591cb0ef41Sopenharmony_ci// { user: 514883, system: 11226 } 11601cb0ef41Sopenharmony_ci``` 11611cb0ef41Sopenharmony_ci 11621cb0ef41Sopenharmony_ci```cjs 11631cb0ef41Sopenharmony_ciconst { cpuUsage } = require('node:process'); 11641cb0ef41Sopenharmony_ci 11651cb0ef41Sopenharmony_ciconst startUsage = cpuUsage(); 11661cb0ef41Sopenharmony_ci// { user: 38579, system: 6986 } 11671cb0ef41Sopenharmony_ci 11681cb0ef41Sopenharmony_ci// spin the CPU for 500 milliseconds 11691cb0ef41Sopenharmony_ciconst now = Date.now(); 11701cb0ef41Sopenharmony_ciwhile (Date.now() - now < 500); 11711cb0ef41Sopenharmony_ci 11721cb0ef41Sopenharmony_ciconsole.log(cpuUsage(startUsage)); 11731cb0ef41Sopenharmony_ci// { user: 514883, system: 11226 } 11741cb0ef41Sopenharmony_ci``` 11751cb0ef41Sopenharmony_ci 11761cb0ef41Sopenharmony_ci## `process.cwd()` 11771cb0ef41Sopenharmony_ci 11781cb0ef41Sopenharmony_ci<!-- YAML 11791cb0ef41Sopenharmony_ciadded: v0.1.8 11801cb0ef41Sopenharmony_ci--> 11811cb0ef41Sopenharmony_ci 11821cb0ef41Sopenharmony_ci* Returns: {string} 11831cb0ef41Sopenharmony_ci 11841cb0ef41Sopenharmony_ciThe `process.cwd()` method returns the current working directory of the Node.js 11851cb0ef41Sopenharmony_ciprocess. 11861cb0ef41Sopenharmony_ci 11871cb0ef41Sopenharmony_ci```mjs 11881cb0ef41Sopenharmony_ciimport { cwd } from 'node:process'; 11891cb0ef41Sopenharmony_ci 11901cb0ef41Sopenharmony_ciconsole.log(`Current directory: ${cwd()}`); 11911cb0ef41Sopenharmony_ci``` 11921cb0ef41Sopenharmony_ci 11931cb0ef41Sopenharmony_ci```cjs 11941cb0ef41Sopenharmony_ciconst { cwd } = require('node:process'); 11951cb0ef41Sopenharmony_ci 11961cb0ef41Sopenharmony_ciconsole.log(`Current directory: ${cwd()}`); 11971cb0ef41Sopenharmony_ci``` 11981cb0ef41Sopenharmony_ci 11991cb0ef41Sopenharmony_ci## `process.debugPort` 12001cb0ef41Sopenharmony_ci 12011cb0ef41Sopenharmony_ci<!-- YAML 12021cb0ef41Sopenharmony_ciadded: v0.7.2 12031cb0ef41Sopenharmony_ci--> 12041cb0ef41Sopenharmony_ci 12051cb0ef41Sopenharmony_ci* {number} 12061cb0ef41Sopenharmony_ci 12071cb0ef41Sopenharmony_ciThe port used by the Node.js debugger when enabled. 12081cb0ef41Sopenharmony_ci 12091cb0ef41Sopenharmony_ci```mjs 12101cb0ef41Sopenharmony_ciimport process from 'node:process'; 12111cb0ef41Sopenharmony_ci 12121cb0ef41Sopenharmony_ciprocess.debugPort = 5858; 12131cb0ef41Sopenharmony_ci``` 12141cb0ef41Sopenharmony_ci 12151cb0ef41Sopenharmony_ci```cjs 12161cb0ef41Sopenharmony_ciconst process = require('node:process'); 12171cb0ef41Sopenharmony_ci 12181cb0ef41Sopenharmony_ciprocess.debugPort = 5858; 12191cb0ef41Sopenharmony_ci``` 12201cb0ef41Sopenharmony_ci 12211cb0ef41Sopenharmony_ci## `process.disconnect()` 12221cb0ef41Sopenharmony_ci 12231cb0ef41Sopenharmony_ci<!-- YAML 12241cb0ef41Sopenharmony_ciadded: v0.7.2 12251cb0ef41Sopenharmony_ci--> 12261cb0ef41Sopenharmony_ci 12271cb0ef41Sopenharmony_ciIf the Node.js process is spawned with an IPC channel (see the [Child Process][] 12281cb0ef41Sopenharmony_ciand [Cluster][] documentation), the `process.disconnect()` method will close the 12291cb0ef41Sopenharmony_ciIPC channel to the parent process, allowing the child process to exit gracefully 12301cb0ef41Sopenharmony_cionce there are no other connections keeping it alive. 12311cb0ef41Sopenharmony_ci 12321cb0ef41Sopenharmony_ciThe effect of calling `process.disconnect()` is the same as calling 12331cb0ef41Sopenharmony_ci[`ChildProcess.disconnect()`][] from the parent process. 12341cb0ef41Sopenharmony_ci 12351cb0ef41Sopenharmony_ciIf the Node.js process was not spawned with an IPC channel, 12361cb0ef41Sopenharmony_ci`process.disconnect()` will be `undefined`. 12371cb0ef41Sopenharmony_ci 12381cb0ef41Sopenharmony_ci## `process.dlopen(module, filename[, flags])` 12391cb0ef41Sopenharmony_ci 12401cb0ef41Sopenharmony_ci<!-- YAML 12411cb0ef41Sopenharmony_ciadded: v0.1.16 12421cb0ef41Sopenharmony_cichanges: 12431cb0ef41Sopenharmony_ci - version: v9.0.0 12441cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/12794 12451cb0ef41Sopenharmony_ci description: Added support for the `flags` argument. 12461cb0ef41Sopenharmony_ci--> 12471cb0ef41Sopenharmony_ci 12481cb0ef41Sopenharmony_ci* `module` {Object} 12491cb0ef41Sopenharmony_ci* `filename` {string} 12501cb0ef41Sopenharmony_ci* `flags` {os.constants.dlopen} **Default:** `os.constants.dlopen.RTLD_LAZY` 12511cb0ef41Sopenharmony_ci 12521cb0ef41Sopenharmony_ciThe `process.dlopen()` method allows dynamically loading shared objects. It is 12531cb0ef41Sopenharmony_ciprimarily used by `require()` to load C++ Addons, and should not be used 12541cb0ef41Sopenharmony_cidirectly, except in special cases. In other words, [`require()`][] should be 12551cb0ef41Sopenharmony_cipreferred over `process.dlopen()` unless there are specific reasons such as 12561cb0ef41Sopenharmony_cicustom dlopen flags or loading from ES modules. 12571cb0ef41Sopenharmony_ci 12581cb0ef41Sopenharmony_ciThe `flags` argument is an integer that allows to specify dlopen 12591cb0ef41Sopenharmony_cibehavior. See the [`os.constants.dlopen`][] documentation for details. 12601cb0ef41Sopenharmony_ci 12611cb0ef41Sopenharmony_ciAn important requirement when calling `process.dlopen()` is that the `module` 12621cb0ef41Sopenharmony_ciinstance must be passed. Functions exported by the C++ Addon are then 12631cb0ef41Sopenharmony_ciaccessible via `module.exports`. 12641cb0ef41Sopenharmony_ci 12651cb0ef41Sopenharmony_ciThe example below shows how to load a C++ Addon, named `local.node`, 12661cb0ef41Sopenharmony_cithat exports a `foo` function. All the symbols are loaded before 12671cb0ef41Sopenharmony_cithe call returns, by passing the `RTLD_NOW` constant. In this example 12681cb0ef41Sopenharmony_cithe constant is assumed to be available. 12691cb0ef41Sopenharmony_ci 12701cb0ef41Sopenharmony_ci```mjs 12711cb0ef41Sopenharmony_ciimport { dlopen } from 'node:process'; 12721cb0ef41Sopenharmony_ciimport { constants } from 'node:os'; 12731cb0ef41Sopenharmony_ciimport { fileURLToPath } from 'node:url'; 12741cb0ef41Sopenharmony_ci 12751cb0ef41Sopenharmony_ciconst module = { exports: {} }; 12761cb0ef41Sopenharmony_cidlopen(module, fileURLToPath(new URL('local.node', import.meta.url)), 12771cb0ef41Sopenharmony_ci constants.dlopen.RTLD_NOW); 12781cb0ef41Sopenharmony_cimodule.exports.foo(); 12791cb0ef41Sopenharmony_ci``` 12801cb0ef41Sopenharmony_ci 12811cb0ef41Sopenharmony_ci```cjs 12821cb0ef41Sopenharmony_ciconst { dlopen } = require('node:process'); 12831cb0ef41Sopenharmony_ciconst { constants } = require('node:os'); 12841cb0ef41Sopenharmony_ciconst { join } = require('node:path'); 12851cb0ef41Sopenharmony_ci 12861cb0ef41Sopenharmony_ciconst module = { exports: {} }; 12871cb0ef41Sopenharmony_cidlopen(module, join(__dirname, 'local.node'), constants.dlopen.RTLD_NOW); 12881cb0ef41Sopenharmony_cimodule.exports.foo(); 12891cb0ef41Sopenharmony_ci``` 12901cb0ef41Sopenharmony_ci 12911cb0ef41Sopenharmony_ci## `process.emitWarning(warning[, options])` 12921cb0ef41Sopenharmony_ci 12931cb0ef41Sopenharmony_ci<!-- YAML 12941cb0ef41Sopenharmony_ciadded: v8.0.0 12951cb0ef41Sopenharmony_ci--> 12961cb0ef41Sopenharmony_ci 12971cb0ef41Sopenharmony_ci* `warning` {string|Error} The warning to emit. 12981cb0ef41Sopenharmony_ci* `options` {Object} 12991cb0ef41Sopenharmony_ci * `type` {string} When `warning` is a `String`, `type` is the name to use 13001cb0ef41Sopenharmony_ci for the _type_ of warning being emitted. **Default:** `'Warning'`. 13011cb0ef41Sopenharmony_ci * `code` {string} A unique identifier for the warning instance being emitted. 13021cb0ef41Sopenharmony_ci * `ctor` {Function} When `warning` is a `String`, `ctor` is an optional 13031cb0ef41Sopenharmony_ci function used to limit the generated stack trace. **Default:** 13041cb0ef41Sopenharmony_ci `process.emitWarning`. 13051cb0ef41Sopenharmony_ci * `detail` {string} Additional text to include with the error. 13061cb0ef41Sopenharmony_ci 13071cb0ef41Sopenharmony_ciThe `process.emitWarning()` method can be used to emit custom or application 13081cb0ef41Sopenharmony_cispecific process warnings. These can be listened for by adding a handler to the 13091cb0ef41Sopenharmony_ci[`'warning'`][process_warning] event. 13101cb0ef41Sopenharmony_ci 13111cb0ef41Sopenharmony_ci```mjs 13121cb0ef41Sopenharmony_ciimport { emitWarning } from 'node:process'; 13131cb0ef41Sopenharmony_ci 13141cb0ef41Sopenharmony_ci// Emit a warning with a code and additional detail. 13151cb0ef41Sopenharmony_ciemitWarning('Something happened!', { 13161cb0ef41Sopenharmony_ci code: 'MY_WARNING', 13171cb0ef41Sopenharmony_ci detail: 'This is some additional information', 13181cb0ef41Sopenharmony_ci}); 13191cb0ef41Sopenharmony_ci// Emits: 13201cb0ef41Sopenharmony_ci// (node:56338) [MY_WARNING] Warning: Something happened! 13211cb0ef41Sopenharmony_ci// This is some additional information 13221cb0ef41Sopenharmony_ci``` 13231cb0ef41Sopenharmony_ci 13241cb0ef41Sopenharmony_ci```cjs 13251cb0ef41Sopenharmony_ciconst { emitWarning } = require('node:process'); 13261cb0ef41Sopenharmony_ci 13271cb0ef41Sopenharmony_ci// Emit a warning with a code and additional detail. 13281cb0ef41Sopenharmony_ciemitWarning('Something happened!', { 13291cb0ef41Sopenharmony_ci code: 'MY_WARNING', 13301cb0ef41Sopenharmony_ci detail: 'This is some additional information', 13311cb0ef41Sopenharmony_ci}); 13321cb0ef41Sopenharmony_ci// Emits: 13331cb0ef41Sopenharmony_ci// (node:56338) [MY_WARNING] Warning: Something happened! 13341cb0ef41Sopenharmony_ci// This is some additional information 13351cb0ef41Sopenharmony_ci``` 13361cb0ef41Sopenharmony_ci 13371cb0ef41Sopenharmony_ciIn this example, an `Error` object is generated internally by 13381cb0ef41Sopenharmony_ci`process.emitWarning()` and passed through to the 13391cb0ef41Sopenharmony_ci[`'warning'`][process_warning] handler. 13401cb0ef41Sopenharmony_ci 13411cb0ef41Sopenharmony_ci```mjs 13421cb0ef41Sopenharmony_ciimport process from 'node:process'; 13431cb0ef41Sopenharmony_ci 13441cb0ef41Sopenharmony_ciprocess.on('warning', (warning) => { 13451cb0ef41Sopenharmony_ci console.warn(warning.name); // 'Warning' 13461cb0ef41Sopenharmony_ci console.warn(warning.message); // 'Something happened!' 13471cb0ef41Sopenharmony_ci console.warn(warning.code); // 'MY_WARNING' 13481cb0ef41Sopenharmony_ci console.warn(warning.stack); // Stack trace 13491cb0ef41Sopenharmony_ci console.warn(warning.detail); // 'This is some additional information' 13501cb0ef41Sopenharmony_ci}); 13511cb0ef41Sopenharmony_ci``` 13521cb0ef41Sopenharmony_ci 13531cb0ef41Sopenharmony_ci```cjs 13541cb0ef41Sopenharmony_ciconst process = require('node:process'); 13551cb0ef41Sopenharmony_ci 13561cb0ef41Sopenharmony_ciprocess.on('warning', (warning) => { 13571cb0ef41Sopenharmony_ci console.warn(warning.name); // 'Warning' 13581cb0ef41Sopenharmony_ci console.warn(warning.message); // 'Something happened!' 13591cb0ef41Sopenharmony_ci console.warn(warning.code); // 'MY_WARNING' 13601cb0ef41Sopenharmony_ci console.warn(warning.stack); // Stack trace 13611cb0ef41Sopenharmony_ci console.warn(warning.detail); // 'This is some additional information' 13621cb0ef41Sopenharmony_ci}); 13631cb0ef41Sopenharmony_ci``` 13641cb0ef41Sopenharmony_ci 13651cb0ef41Sopenharmony_ciIf `warning` is passed as an `Error` object, the `options` argument is ignored. 13661cb0ef41Sopenharmony_ci 13671cb0ef41Sopenharmony_ci## `process.emitWarning(warning[, type[, code]][, ctor])` 13681cb0ef41Sopenharmony_ci 13691cb0ef41Sopenharmony_ci<!-- YAML 13701cb0ef41Sopenharmony_ciadded: v6.0.0 13711cb0ef41Sopenharmony_ci--> 13721cb0ef41Sopenharmony_ci 13731cb0ef41Sopenharmony_ci* `warning` {string|Error} The warning to emit. 13741cb0ef41Sopenharmony_ci* `type` {string} When `warning` is a `String`, `type` is the name to use 13751cb0ef41Sopenharmony_ci for the _type_ of warning being emitted. **Default:** `'Warning'`. 13761cb0ef41Sopenharmony_ci* `code` {string} A unique identifier for the warning instance being emitted. 13771cb0ef41Sopenharmony_ci* `ctor` {Function} When `warning` is a `String`, `ctor` is an optional 13781cb0ef41Sopenharmony_ci function used to limit the generated stack trace. **Default:** 13791cb0ef41Sopenharmony_ci `process.emitWarning`. 13801cb0ef41Sopenharmony_ci 13811cb0ef41Sopenharmony_ciThe `process.emitWarning()` method can be used to emit custom or application 13821cb0ef41Sopenharmony_cispecific process warnings. These can be listened for by adding a handler to the 13831cb0ef41Sopenharmony_ci[`'warning'`][process_warning] event. 13841cb0ef41Sopenharmony_ci 13851cb0ef41Sopenharmony_ci```mjs 13861cb0ef41Sopenharmony_ciimport { emitWarning } from 'node:process'; 13871cb0ef41Sopenharmony_ci 13881cb0ef41Sopenharmony_ci// Emit a warning using a string. 13891cb0ef41Sopenharmony_ciemitWarning('Something happened!'); 13901cb0ef41Sopenharmony_ci// Emits: (node: 56338) Warning: Something happened! 13911cb0ef41Sopenharmony_ci``` 13921cb0ef41Sopenharmony_ci 13931cb0ef41Sopenharmony_ci```cjs 13941cb0ef41Sopenharmony_ciconst { emitWarning } = require('node:process'); 13951cb0ef41Sopenharmony_ci 13961cb0ef41Sopenharmony_ci// Emit a warning using a string. 13971cb0ef41Sopenharmony_ciemitWarning('Something happened!'); 13981cb0ef41Sopenharmony_ci// Emits: (node: 56338) Warning: Something happened! 13991cb0ef41Sopenharmony_ci``` 14001cb0ef41Sopenharmony_ci 14011cb0ef41Sopenharmony_ci```mjs 14021cb0ef41Sopenharmony_ciimport { emitWarning } from 'node:process'; 14031cb0ef41Sopenharmony_ci 14041cb0ef41Sopenharmony_ci// Emit a warning using a string and a type. 14051cb0ef41Sopenharmony_ciemitWarning('Something Happened!', 'CustomWarning'); 14061cb0ef41Sopenharmony_ci// Emits: (node:56338) CustomWarning: Something Happened! 14071cb0ef41Sopenharmony_ci``` 14081cb0ef41Sopenharmony_ci 14091cb0ef41Sopenharmony_ci```cjs 14101cb0ef41Sopenharmony_ciconst { emitWarning } = require('node:process'); 14111cb0ef41Sopenharmony_ci 14121cb0ef41Sopenharmony_ci// Emit a warning using a string and a type. 14131cb0ef41Sopenharmony_ciemitWarning('Something Happened!', 'CustomWarning'); 14141cb0ef41Sopenharmony_ci// Emits: (node:56338) CustomWarning: Something Happened! 14151cb0ef41Sopenharmony_ci``` 14161cb0ef41Sopenharmony_ci 14171cb0ef41Sopenharmony_ci```mjs 14181cb0ef41Sopenharmony_ciimport { emitWarning } from 'node:process'; 14191cb0ef41Sopenharmony_ci 14201cb0ef41Sopenharmony_ciemitWarning('Something happened!', 'CustomWarning', 'WARN001'); 14211cb0ef41Sopenharmony_ci// Emits: (node:56338) [WARN001] CustomWarning: Something happened! 14221cb0ef41Sopenharmony_ci``` 14231cb0ef41Sopenharmony_ci 14241cb0ef41Sopenharmony_ci```cjs 14251cb0ef41Sopenharmony_ciconst { emitWarning } = require('node:process'); 14261cb0ef41Sopenharmony_ci 14271cb0ef41Sopenharmony_ciprocess.emitWarning('Something happened!', 'CustomWarning', 'WARN001'); 14281cb0ef41Sopenharmony_ci// Emits: (node:56338) [WARN001] CustomWarning: Something happened! 14291cb0ef41Sopenharmony_ci``` 14301cb0ef41Sopenharmony_ci 14311cb0ef41Sopenharmony_ciIn each of the previous examples, an `Error` object is generated internally by 14321cb0ef41Sopenharmony_ci`process.emitWarning()` and passed through to the [`'warning'`][process_warning] 14331cb0ef41Sopenharmony_cihandler. 14341cb0ef41Sopenharmony_ci 14351cb0ef41Sopenharmony_ci```mjs 14361cb0ef41Sopenharmony_ciimport process from 'node:process'; 14371cb0ef41Sopenharmony_ci 14381cb0ef41Sopenharmony_ciprocess.on('warning', (warning) => { 14391cb0ef41Sopenharmony_ci console.warn(warning.name); 14401cb0ef41Sopenharmony_ci console.warn(warning.message); 14411cb0ef41Sopenharmony_ci console.warn(warning.code); 14421cb0ef41Sopenharmony_ci console.warn(warning.stack); 14431cb0ef41Sopenharmony_ci}); 14441cb0ef41Sopenharmony_ci``` 14451cb0ef41Sopenharmony_ci 14461cb0ef41Sopenharmony_ci```cjs 14471cb0ef41Sopenharmony_ciconst process = require('node:process'); 14481cb0ef41Sopenharmony_ci 14491cb0ef41Sopenharmony_ciprocess.on('warning', (warning) => { 14501cb0ef41Sopenharmony_ci console.warn(warning.name); 14511cb0ef41Sopenharmony_ci console.warn(warning.message); 14521cb0ef41Sopenharmony_ci console.warn(warning.code); 14531cb0ef41Sopenharmony_ci console.warn(warning.stack); 14541cb0ef41Sopenharmony_ci}); 14551cb0ef41Sopenharmony_ci``` 14561cb0ef41Sopenharmony_ci 14571cb0ef41Sopenharmony_ciIf `warning` is passed as an `Error` object, it will be passed through to the 14581cb0ef41Sopenharmony_ci`'warning'` event handler unmodified (and the optional `type`, 14591cb0ef41Sopenharmony_ci`code` and `ctor` arguments will be ignored): 14601cb0ef41Sopenharmony_ci 14611cb0ef41Sopenharmony_ci```mjs 14621cb0ef41Sopenharmony_ciimport { emitWarning } from 'node:process'; 14631cb0ef41Sopenharmony_ci 14641cb0ef41Sopenharmony_ci// Emit a warning using an Error object. 14651cb0ef41Sopenharmony_ciconst myWarning = new Error('Something happened!'); 14661cb0ef41Sopenharmony_ci// Use the Error name property to specify the type name 14671cb0ef41Sopenharmony_cimyWarning.name = 'CustomWarning'; 14681cb0ef41Sopenharmony_cimyWarning.code = 'WARN001'; 14691cb0ef41Sopenharmony_ci 14701cb0ef41Sopenharmony_ciemitWarning(myWarning); 14711cb0ef41Sopenharmony_ci// Emits: (node:56338) [WARN001] CustomWarning: Something happened! 14721cb0ef41Sopenharmony_ci``` 14731cb0ef41Sopenharmony_ci 14741cb0ef41Sopenharmony_ci```cjs 14751cb0ef41Sopenharmony_ciconst { emitWarning } = require('node:process'); 14761cb0ef41Sopenharmony_ci 14771cb0ef41Sopenharmony_ci// Emit a warning using an Error object. 14781cb0ef41Sopenharmony_ciconst myWarning = new Error('Something happened!'); 14791cb0ef41Sopenharmony_ci// Use the Error name property to specify the type name 14801cb0ef41Sopenharmony_cimyWarning.name = 'CustomWarning'; 14811cb0ef41Sopenharmony_cimyWarning.code = 'WARN001'; 14821cb0ef41Sopenharmony_ci 14831cb0ef41Sopenharmony_ciemitWarning(myWarning); 14841cb0ef41Sopenharmony_ci// Emits: (node:56338) [WARN001] CustomWarning: Something happened! 14851cb0ef41Sopenharmony_ci``` 14861cb0ef41Sopenharmony_ci 14871cb0ef41Sopenharmony_ciA `TypeError` is thrown if `warning` is anything other than a string or `Error` 14881cb0ef41Sopenharmony_ciobject. 14891cb0ef41Sopenharmony_ci 14901cb0ef41Sopenharmony_ciWhile process warnings use `Error` objects, the process warning 14911cb0ef41Sopenharmony_cimechanism is **not** a replacement for normal error handling mechanisms. 14921cb0ef41Sopenharmony_ci 14931cb0ef41Sopenharmony_ciThe following additional handling is implemented if the warning `type` is 14941cb0ef41Sopenharmony_ci`'DeprecationWarning'`: 14951cb0ef41Sopenharmony_ci 14961cb0ef41Sopenharmony_ci* If the `--throw-deprecation` command-line flag is used, the deprecation 14971cb0ef41Sopenharmony_ci warning is thrown as an exception rather than being emitted as an event. 14981cb0ef41Sopenharmony_ci* If the `--no-deprecation` command-line flag is used, the deprecation 14991cb0ef41Sopenharmony_ci warning is suppressed. 15001cb0ef41Sopenharmony_ci* If the `--trace-deprecation` command-line flag is used, the deprecation 15011cb0ef41Sopenharmony_ci warning is printed to `stderr` along with the full stack trace. 15021cb0ef41Sopenharmony_ci 15031cb0ef41Sopenharmony_ci### Avoiding duplicate warnings 15041cb0ef41Sopenharmony_ci 15051cb0ef41Sopenharmony_ciAs a best practice, warnings should be emitted only once per process. To do 15061cb0ef41Sopenharmony_ciso, place the `emitWarning()` behind a boolean. 15071cb0ef41Sopenharmony_ci 15081cb0ef41Sopenharmony_ci```mjs 15091cb0ef41Sopenharmony_ciimport { emitWarning } from 'node:process'; 15101cb0ef41Sopenharmony_ci 15111cb0ef41Sopenharmony_cifunction emitMyWarning() { 15121cb0ef41Sopenharmony_ci if (!emitMyWarning.warned) { 15131cb0ef41Sopenharmony_ci emitMyWarning.warned = true; 15141cb0ef41Sopenharmony_ci emitWarning('Only warn once!'); 15151cb0ef41Sopenharmony_ci } 15161cb0ef41Sopenharmony_ci} 15171cb0ef41Sopenharmony_ciemitMyWarning(); 15181cb0ef41Sopenharmony_ci// Emits: (node: 56339) Warning: Only warn once! 15191cb0ef41Sopenharmony_ciemitMyWarning(); 15201cb0ef41Sopenharmony_ci// Emits nothing 15211cb0ef41Sopenharmony_ci``` 15221cb0ef41Sopenharmony_ci 15231cb0ef41Sopenharmony_ci```cjs 15241cb0ef41Sopenharmony_ciconst { emitWarning } = require('node:process'); 15251cb0ef41Sopenharmony_ci 15261cb0ef41Sopenharmony_cifunction emitMyWarning() { 15271cb0ef41Sopenharmony_ci if (!emitMyWarning.warned) { 15281cb0ef41Sopenharmony_ci emitMyWarning.warned = true; 15291cb0ef41Sopenharmony_ci emitWarning('Only warn once!'); 15301cb0ef41Sopenharmony_ci } 15311cb0ef41Sopenharmony_ci} 15321cb0ef41Sopenharmony_ciemitMyWarning(); 15331cb0ef41Sopenharmony_ci// Emits: (node: 56339) Warning: Only warn once! 15341cb0ef41Sopenharmony_ciemitMyWarning(); 15351cb0ef41Sopenharmony_ci// Emits nothing 15361cb0ef41Sopenharmony_ci``` 15371cb0ef41Sopenharmony_ci 15381cb0ef41Sopenharmony_ci## `process.env` 15391cb0ef41Sopenharmony_ci 15401cb0ef41Sopenharmony_ci<!-- YAML 15411cb0ef41Sopenharmony_ciadded: v0.1.27 15421cb0ef41Sopenharmony_cichanges: 15431cb0ef41Sopenharmony_ci - version: v11.14.0 15441cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/26544 15451cb0ef41Sopenharmony_ci description: Worker threads will now use a copy of the parent thread's 15461cb0ef41Sopenharmony_ci `process.env` by default, configurable through the `env` 15471cb0ef41Sopenharmony_ci option of the `Worker` constructor. 15481cb0ef41Sopenharmony_ci - version: v10.0.0 15491cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/18990 15501cb0ef41Sopenharmony_ci description: Implicit conversion of variable value to string is deprecated. 15511cb0ef41Sopenharmony_ci--> 15521cb0ef41Sopenharmony_ci 15531cb0ef41Sopenharmony_ci* {Object} 15541cb0ef41Sopenharmony_ci 15551cb0ef41Sopenharmony_ciThe `process.env` property returns an object containing the user environment. 15561cb0ef41Sopenharmony_ciSee environ(7). 15571cb0ef41Sopenharmony_ci 15581cb0ef41Sopenharmony_ciAn example of this object looks like: 15591cb0ef41Sopenharmony_ci 15601cb0ef41Sopenharmony_ci<!-- eslint-skip --> 15611cb0ef41Sopenharmony_ci 15621cb0ef41Sopenharmony_ci```js 15631cb0ef41Sopenharmony_ci{ 15641cb0ef41Sopenharmony_ci TERM: 'xterm-256color', 15651cb0ef41Sopenharmony_ci SHELL: '/usr/local/bin/bash', 15661cb0ef41Sopenharmony_ci USER: 'maciej', 15671cb0ef41Sopenharmony_ci PATH: '~/.bin/:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin', 15681cb0ef41Sopenharmony_ci PWD: '/Users/maciej', 15691cb0ef41Sopenharmony_ci EDITOR: 'vim', 15701cb0ef41Sopenharmony_ci SHLVL: '1', 15711cb0ef41Sopenharmony_ci HOME: '/Users/maciej', 15721cb0ef41Sopenharmony_ci LOGNAME: 'maciej', 15731cb0ef41Sopenharmony_ci _: '/usr/local/bin/node' 15741cb0ef41Sopenharmony_ci} 15751cb0ef41Sopenharmony_ci``` 15761cb0ef41Sopenharmony_ci 15771cb0ef41Sopenharmony_ciIt is possible to modify this object, but such modifications will not be 15781cb0ef41Sopenharmony_cireflected outside the Node.js process, or (unless explicitly requested) 15791cb0ef41Sopenharmony_cito other [`Worker`][] threads. 15801cb0ef41Sopenharmony_ciIn other words, the following example would not work: 15811cb0ef41Sopenharmony_ci 15821cb0ef41Sopenharmony_ci```console 15831cb0ef41Sopenharmony_ci$ node -e 'process.env.foo = "bar"' && echo $foo 15841cb0ef41Sopenharmony_ci``` 15851cb0ef41Sopenharmony_ci 15861cb0ef41Sopenharmony_ciWhile the following will: 15871cb0ef41Sopenharmony_ci 15881cb0ef41Sopenharmony_ci```mjs 15891cb0ef41Sopenharmony_ciimport { env } from 'node:process'; 15901cb0ef41Sopenharmony_ci 15911cb0ef41Sopenharmony_cienv.foo = 'bar'; 15921cb0ef41Sopenharmony_ciconsole.log(env.foo); 15931cb0ef41Sopenharmony_ci``` 15941cb0ef41Sopenharmony_ci 15951cb0ef41Sopenharmony_ci```cjs 15961cb0ef41Sopenharmony_ciconst { env } = require('node:process'); 15971cb0ef41Sopenharmony_ci 15981cb0ef41Sopenharmony_cienv.foo = 'bar'; 15991cb0ef41Sopenharmony_ciconsole.log(env.foo); 16001cb0ef41Sopenharmony_ci``` 16011cb0ef41Sopenharmony_ci 16021cb0ef41Sopenharmony_ciAssigning a property on `process.env` will implicitly convert the value 16031cb0ef41Sopenharmony_cito a string. **This behavior is deprecated.** Future versions of Node.js may 16041cb0ef41Sopenharmony_cithrow an error when the value is not a string, number, or boolean. 16051cb0ef41Sopenharmony_ci 16061cb0ef41Sopenharmony_ci```mjs 16071cb0ef41Sopenharmony_ciimport { env } from 'node:process'; 16081cb0ef41Sopenharmony_ci 16091cb0ef41Sopenharmony_cienv.test = null; 16101cb0ef41Sopenharmony_ciconsole.log(env.test); 16111cb0ef41Sopenharmony_ci// => 'null' 16121cb0ef41Sopenharmony_cienv.test = undefined; 16131cb0ef41Sopenharmony_ciconsole.log(env.test); 16141cb0ef41Sopenharmony_ci// => 'undefined' 16151cb0ef41Sopenharmony_ci``` 16161cb0ef41Sopenharmony_ci 16171cb0ef41Sopenharmony_ci```cjs 16181cb0ef41Sopenharmony_ciconst { env } = require('node:process'); 16191cb0ef41Sopenharmony_ci 16201cb0ef41Sopenharmony_cienv.test = null; 16211cb0ef41Sopenharmony_ciconsole.log(env.test); 16221cb0ef41Sopenharmony_ci// => 'null' 16231cb0ef41Sopenharmony_cienv.test = undefined; 16241cb0ef41Sopenharmony_ciconsole.log(env.test); 16251cb0ef41Sopenharmony_ci// => 'undefined' 16261cb0ef41Sopenharmony_ci``` 16271cb0ef41Sopenharmony_ci 16281cb0ef41Sopenharmony_ciUse `delete` to delete a property from `process.env`. 16291cb0ef41Sopenharmony_ci 16301cb0ef41Sopenharmony_ci```mjs 16311cb0ef41Sopenharmony_ciimport { env } from 'node:process'; 16321cb0ef41Sopenharmony_ci 16331cb0ef41Sopenharmony_cienv.TEST = 1; 16341cb0ef41Sopenharmony_cidelete env.TEST; 16351cb0ef41Sopenharmony_ciconsole.log(env.TEST); 16361cb0ef41Sopenharmony_ci// => undefined 16371cb0ef41Sopenharmony_ci``` 16381cb0ef41Sopenharmony_ci 16391cb0ef41Sopenharmony_ci```cjs 16401cb0ef41Sopenharmony_ciconst { env } = require('node:process'); 16411cb0ef41Sopenharmony_ci 16421cb0ef41Sopenharmony_cienv.TEST = 1; 16431cb0ef41Sopenharmony_cidelete env.TEST; 16441cb0ef41Sopenharmony_ciconsole.log(env.TEST); 16451cb0ef41Sopenharmony_ci// => undefined 16461cb0ef41Sopenharmony_ci``` 16471cb0ef41Sopenharmony_ci 16481cb0ef41Sopenharmony_ciOn Windows operating systems, environment variables are case-insensitive. 16491cb0ef41Sopenharmony_ci 16501cb0ef41Sopenharmony_ci```mjs 16511cb0ef41Sopenharmony_ciimport { env } from 'node:process'; 16521cb0ef41Sopenharmony_ci 16531cb0ef41Sopenharmony_cienv.TEST = 1; 16541cb0ef41Sopenharmony_ciconsole.log(env.test); 16551cb0ef41Sopenharmony_ci// => 1 16561cb0ef41Sopenharmony_ci``` 16571cb0ef41Sopenharmony_ci 16581cb0ef41Sopenharmony_ci```cjs 16591cb0ef41Sopenharmony_ciconst { env } = require('node:process'); 16601cb0ef41Sopenharmony_ci 16611cb0ef41Sopenharmony_cienv.TEST = 1; 16621cb0ef41Sopenharmony_ciconsole.log(env.test); 16631cb0ef41Sopenharmony_ci// => 1 16641cb0ef41Sopenharmony_ci``` 16651cb0ef41Sopenharmony_ci 16661cb0ef41Sopenharmony_ciUnless explicitly specified when creating a [`Worker`][] instance, 16671cb0ef41Sopenharmony_cieach [`Worker`][] thread has its own copy of `process.env`, based on its 16681cb0ef41Sopenharmony_ciparent thread's `process.env`, or whatever was specified as the `env` option 16691cb0ef41Sopenharmony_cito the [`Worker`][] constructor. Changes to `process.env` will not be visible 16701cb0ef41Sopenharmony_ciacross [`Worker`][] threads, and only the main thread can make changes that 16711cb0ef41Sopenharmony_ciare visible to the operating system or to native add-ons. On Windows, a copy of 16721cb0ef41Sopenharmony_ci`process.env` on a [`Worker`][] instance operates in a case-sensitive manner 16731cb0ef41Sopenharmony_ciunlike the main thread. 16741cb0ef41Sopenharmony_ci 16751cb0ef41Sopenharmony_ci## `process.execArgv` 16761cb0ef41Sopenharmony_ci 16771cb0ef41Sopenharmony_ci<!-- YAML 16781cb0ef41Sopenharmony_ciadded: v0.7.7 16791cb0ef41Sopenharmony_ci--> 16801cb0ef41Sopenharmony_ci 16811cb0ef41Sopenharmony_ci* {string\[]} 16821cb0ef41Sopenharmony_ci 16831cb0ef41Sopenharmony_ciThe `process.execArgv` property returns the set of Node.js-specific command-line 16841cb0ef41Sopenharmony_cioptions passed when the Node.js process was launched. These options do not 16851cb0ef41Sopenharmony_ciappear in the array returned by the [`process.argv`][] property, and do not 16861cb0ef41Sopenharmony_ciinclude the Node.js executable, the name of the script, or any options following 16871cb0ef41Sopenharmony_cithe script name. These options are useful in order to spawn child processes with 16881cb0ef41Sopenharmony_cithe same execution environment as the parent. 16891cb0ef41Sopenharmony_ci 16901cb0ef41Sopenharmony_ci```console 16911cb0ef41Sopenharmony_ci$ node --harmony script.js --version 16921cb0ef41Sopenharmony_ci``` 16931cb0ef41Sopenharmony_ci 16941cb0ef41Sopenharmony_ciResults in `process.execArgv`: 16951cb0ef41Sopenharmony_ci 16961cb0ef41Sopenharmony_ci<!-- eslint-disable semi --> 16971cb0ef41Sopenharmony_ci 16981cb0ef41Sopenharmony_ci```js 16991cb0ef41Sopenharmony_ci['--harmony'] 17001cb0ef41Sopenharmony_ci``` 17011cb0ef41Sopenharmony_ci 17021cb0ef41Sopenharmony_ciAnd `process.argv`: 17031cb0ef41Sopenharmony_ci 17041cb0ef41Sopenharmony_ci<!-- eslint-disable semi --> 17051cb0ef41Sopenharmony_ci 17061cb0ef41Sopenharmony_ci```js 17071cb0ef41Sopenharmony_ci['/usr/local/bin/node', 'script.js', '--version'] 17081cb0ef41Sopenharmony_ci``` 17091cb0ef41Sopenharmony_ci 17101cb0ef41Sopenharmony_ciRefer to [`Worker` constructor][] for the detailed behavior of worker 17111cb0ef41Sopenharmony_cithreads with this property. 17121cb0ef41Sopenharmony_ci 17131cb0ef41Sopenharmony_ci## `process.execPath` 17141cb0ef41Sopenharmony_ci 17151cb0ef41Sopenharmony_ci<!-- YAML 17161cb0ef41Sopenharmony_ciadded: v0.1.100 17171cb0ef41Sopenharmony_ci--> 17181cb0ef41Sopenharmony_ci 17191cb0ef41Sopenharmony_ci* {string} 17201cb0ef41Sopenharmony_ci 17211cb0ef41Sopenharmony_ciThe `process.execPath` property returns the absolute pathname of the executable 17221cb0ef41Sopenharmony_cithat started the Node.js process. Symbolic links, if any, are resolved. 17231cb0ef41Sopenharmony_ci 17241cb0ef41Sopenharmony_ci<!-- eslint-disable semi --> 17251cb0ef41Sopenharmony_ci 17261cb0ef41Sopenharmony_ci```js 17271cb0ef41Sopenharmony_ci'/usr/local/bin/node' 17281cb0ef41Sopenharmony_ci``` 17291cb0ef41Sopenharmony_ci 17301cb0ef41Sopenharmony_ci## `process.exit([code])` 17311cb0ef41Sopenharmony_ci 17321cb0ef41Sopenharmony_ci<!-- YAML 17331cb0ef41Sopenharmony_ciadded: v0.1.13 17341cb0ef41Sopenharmony_ci--> 17351cb0ef41Sopenharmony_ci 17361cb0ef41Sopenharmony_ci* `code` {integer} The exit code. **Default:** `0`. 17371cb0ef41Sopenharmony_ci 17381cb0ef41Sopenharmony_ciThe `process.exit()` method instructs Node.js to terminate the process 17391cb0ef41Sopenharmony_cisynchronously with an exit status of `code`. If `code` is omitted, exit uses 17401cb0ef41Sopenharmony_cieither the 'success' code `0` or the value of `process.exitCode` if it has been 17411cb0ef41Sopenharmony_ciset. Node.js will not terminate until all the [`'exit'`][] event listeners are 17421cb0ef41Sopenharmony_cicalled. 17431cb0ef41Sopenharmony_ci 17441cb0ef41Sopenharmony_ciTo exit with a 'failure' code: 17451cb0ef41Sopenharmony_ci 17461cb0ef41Sopenharmony_ci```mjs 17471cb0ef41Sopenharmony_ciimport { exit } from 'node:process'; 17481cb0ef41Sopenharmony_ci 17491cb0ef41Sopenharmony_ciexit(1); 17501cb0ef41Sopenharmony_ci``` 17511cb0ef41Sopenharmony_ci 17521cb0ef41Sopenharmony_ci```cjs 17531cb0ef41Sopenharmony_ciconst { exit } = require('node:process'); 17541cb0ef41Sopenharmony_ci 17551cb0ef41Sopenharmony_ciexit(1); 17561cb0ef41Sopenharmony_ci``` 17571cb0ef41Sopenharmony_ci 17581cb0ef41Sopenharmony_ciThe shell that executed Node.js should see the exit code as `1`. 17591cb0ef41Sopenharmony_ci 17601cb0ef41Sopenharmony_ciCalling `process.exit()` will force the process to exit as quickly as possible 17611cb0ef41Sopenharmony_cieven if there are still asynchronous operations pending that have not yet 17621cb0ef41Sopenharmony_cicompleted fully, including I/O operations to `process.stdout` and 17631cb0ef41Sopenharmony_ci`process.stderr`. 17641cb0ef41Sopenharmony_ci 17651cb0ef41Sopenharmony_ciIn most situations, it is not actually necessary to call `process.exit()` 17661cb0ef41Sopenharmony_ciexplicitly. The Node.js process will exit on its own _if there is no additional 17671cb0ef41Sopenharmony_ciwork pending_ in the event loop. The `process.exitCode` property can be set to 17681cb0ef41Sopenharmony_citell the process which exit code to use when the process exits gracefully. 17691cb0ef41Sopenharmony_ci 17701cb0ef41Sopenharmony_ciFor instance, the following example illustrates a _misuse_ of the 17711cb0ef41Sopenharmony_ci`process.exit()` method that could lead to data printed to stdout being 17721cb0ef41Sopenharmony_citruncated and lost: 17731cb0ef41Sopenharmony_ci 17741cb0ef41Sopenharmony_ci```mjs 17751cb0ef41Sopenharmony_ciimport { exit } from 'node:process'; 17761cb0ef41Sopenharmony_ci 17771cb0ef41Sopenharmony_ci// This is an example of what *not* to do: 17781cb0ef41Sopenharmony_ciif (someConditionNotMet()) { 17791cb0ef41Sopenharmony_ci printUsageToStdout(); 17801cb0ef41Sopenharmony_ci exit(1); 17811cb0ef41Sopenharmony_ci} 17821cb0ef41Sopenharmony_ci``` 17831cb0ef41Sopenharmony_ci 17841cb0ef41Sopenharmony_ci```cjs 17851cb0ef41Sopenharmony_ciconst { exit } = require('node:process'); 17861cb0ef41Sopenharmony_ci 17871cb0ef41Sopenharmony_ci// This is an example of what *not* to do: 17881cb0ef41Sopenharmony_ciif (someConditionNotMet()) { 17891cb0ef41Sopenharmony_ci printUsageToStdout(); 17901cb0ef41Sopenharmony_ci exit(1); 17911cb0ef41Sopenharmony_ci} 17921cb0ef41Sopenharmony_ci``` 17931cb0ef41Sopenharmony_ci 17941cb0ef41Sopenharmony_ciThe reason this is problematic is because writes to `process.stdout` in Node.js 17951cb0ef41Sopenharmony_ciare sometimes _asynchronous_ and may occur over multiple ticks of the Node.js 17961cb0ef41Sopenharmony_cievent loop. Calling `process.exit()`, however, forces the process to exit 17971cb0ef41Sopenharmony_ci_before_ those additional writes to `stdout` can be performed. 17981cb0ef41Sopenharmony_ci 17991cb0ef41Sopenharmony_ciRather than calling `process.exit()` directly, the code _should_ set the 18001cb0ef41Sopenharmony_ci`process.exitCode` and allow the process to exit naturally by avoiding 18011cb0ef41Sopenharmony_cischeduling any additional work for the event loop: 18021cb0ef41Sopenharmony_ci 18031cb0ef41Sopenharmony_ci```mjs 18041cb0ef41Sopenharmony_ciimport process from 'node:process'; 18051cb0ef41Sopenharmony_ci 18061cb0ef41Sopenharmony_ci// How to properly set the exit code while letting 18071cb0ef41Sopenharmony_ci// the process exit gracefully. 18081cb0ef41Sopenharmony_ciif (someConditionNotMet()) { 18091cb0ef41Sopenharmony_ci printUsageToStdout(); 18101cb0ef41Sopenharmony_ci process.exitCode = 1; 18111cb0ef41Sopenharmony_ci} 18121cb0ef41Sopenharmony_ci``` 18131cb0ef41Sopenharmony_ci 18141cb0ef41Sopenharmony_ci```cjs 18151cb0ef41Sopenharmony_ciconst process = require('node:process'); 18161cb0ef41Sopenharmony_ci 18171cb0ef41Sopenharmony_ci// How to properly set the exit code while letting 18181cb0ef41Sopenharmony_ci// the process exit gracefully. 18191cb0ef41Sopenharmony_ciif (someConditionNotMet()) { 18201cb0ef41Sopenharmony_ci printUsageToStdout(); 18211cb0ef41Sopenharmony_ci process.exitCode = 1; 18221cb0ef41Sopenharmony_ci} 18231cb0ef41Sopenharmony_ci``` 18241cb0ef41Sopenharmony_ci 18251cb0ef41Sopenharmony_ciIf it is necessary to terminate the Node.js process due to an error condition, 18261cb0ef41Sopenharmony_cithrowing an _uncaught_ error and allowing the process to terminate accordingly 18271cb0ef41Sopenharmony_ciis safer than calling `process.exit()`. 18281cb0ef41Sopenharmony_ci 18291cb0ef41Sopenharmony_ciIn [`Worker`][] threads, this function stops the current thread rather 18301cb0ef41Sopenharmony_cithan the current process. 18311cb0ef41Sopenharmony_ci 18321cb0ef41Sopenharmony_ci## `process.exitCode` 18331cb0ef41Sopenharmony_ci 18341cb0ef41Sopenharmony_ci<!-- YAML 18351cb0ef41Sopenharmony_ciadded: v0.11.8 18361cb0ef41Sopenharmony_ci--> 18371cb0ef41Sopenharmony_ci 18381cb0ef41Sopenharmony_ci* {integer} 18391cb0ef41Sopenharmony_ci 18401cb0ef41Sopenharmony_ciA number which will be the process exit code, when the process either 18411cb0ef41Sopenharmony_ciexits gracefully, or is exited via [`process.exit()`][] without specifying 18421cb0ef41Sopenharmony_cia code. 18431cb0ef41Sopenharmony_ci 18441cb0ef41Sopenharmony_ciSpecifying a code to [`process.exit(code)`][`process.exit()`] will override any 18451cb0ef41Sopenharmony_ciprevious setting of `process.exitCode`. 18461cb0ef41Sopenharmony_ci 18471cb0ef41Sopenharmony_ci## `process.getActiveResourcesInfo()` 18481cb0ef41Sopenharmony_ci 18491cb0ef41Sopenharmony_ci<!-- YAML 18501cb0ef41Sopenharmony_ciadded: 18511cb0ef41Sopenharmony_ci - v17.3.0 18521cb0ef41Sopenharmony_ci - v16.14.0 18531cb0ef41Sopenharmony_ci--> 18541cb0ef41Sopenharmony_ci 18551cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 18561cb0ef41Sopenharmony_ci 18571cb0ef41Sopenharmony_ci* Returns: {string\[]} 18581cb0ef41Sopenharmony_ci 18591cb0ef41Sopenharmony_ciThe `process.getActiveResourcesInfo()` method returns an array of strings 18601cb0ef41Sopenharmony_cicontaining the types of the active resources that are currently keeping the 18611cb0ef41Sopenharmony_cievent loop alive. 18621cb0ef41Sopenharmony_ci 18631cb0ef41Sopenharmony_ci```mjs 18641cb0ef41Sopenharmony_ciimport { getActiveResourcesInfo } from 'node:process'; 18651cb0ef41Sopenharmony_ciimport { setTimeout } from 'node:timers'; 18661cb0ef41Sopenharmony_ci 18671cb0ef41Sopenharmony_ciconsole.log('Before:', getActiveResourcesInfo()); 18681cb0ef41Sopenharmony_cisetTimeout(() => {}, 1000); 18691cb0ef41Sopenharmony_ciconsole.log('After:', getActiveResourcesInfo()); 18701cb0ef41Sopenharmony_ci// Prints: 18711cb0ef41Sopenharmony_ci// Before: [ 'CloseReq', 'TTYWrap', 'TTYWrap', 'TTYWrap' ] 18721cb0ef41Sopenharmony_ci// After: [ 'CloseReq', 'TTYWrap', 'TTYWrap', 'TTYWrap', 'Timeout' ] 18731cb0ef41Sopenharmony_ci``` 18741cb0ef41Sopenharmony_ci 18751cb0ef41Sopenharmony_ci```cjs 18761cb0ef41Sopenharmony_ciconst { getActiveResourcesInfo } = require('node:process'); 18771cb0ef41Sopenharmony_ciconst { setTimeout } = require('node:timers'); 18781cb0ef41Sopenharmony_ci 18791cb0ef41Sopenharmony_ciconsole.log('Before:', getActiveResourcesInfo()); 18801cb0ef41Sopenharmony_cisetTimeout(() => {}, 1000); 18811cb0ef41Sopenharmony_ciconsole.log('After:', getActiveResourcesInfo()); 18821cb0ef41Sopenharmony_ci// Prints: 18831cb0ef41Sopenharmony_ci// Before: [ 'TTYWrap', 'TTYWrap', 'TTYWrap' ] 18841cb0ef41Sopenharmony_ci// After: [ 'TTYWrap', 'TTYWrap', 'TTYWrap', 'Timeout' ] 18851cb0ef41Sopenharmony_ci``` 18861cb0ef41Sopenharmony_ci 18871cb0ef41Sopenharmony_ci## `process.getegid()` 18881cb0ef41Sopenharmony_ci 18891cb0ef41Sopenharmony_ci<!-- YAML 18901cb0ef41Sopenharmony_ciadded: v2.0.0 18911cb0ef41Sopenharmony_ci--> 18921cb0ef41Sopenharmony_ci 18931cb0ef41Sopenharmony_ciThe `process.getegid()` method returns the numerical effective group identity 18941cb0ef41Sopenharmony_ciof the Node.js process. (See getegid(2).) 18951cb0ef41Sopenharmony_ci 18961cb0ef41Sopenharmony_ci```mjs 18971cb0ef41Sopenharmony_ciimport process from 'node:process'; 18981cb0ef41Sopenharmony_ci 18991cb0ef41Sopenharmony_ciif (process.getegid) { 19001cb0ef41Sopenharmony_ci console.log(`Current gid: ${process.getegid()}`); 19011cb0ef41Sopenharmony_ci} 19021cb0ef41Sopenharmony_ci``` 19031cb0ef41Sopenharmony_ci 19041cb0ef41Sopenharmony_ci```cjs 19051cb0ef41Sopenharmony_ciconst process = require('node:process'); 19061cb0ef41Sopenharmony_ci 19071cb0ef41Sopenharmony_ciif (process.getegid) { 19081cb0ef41Sopenharmony_ci console.log(`Current gid: ${process.getegid()}`); 19091cb0ef41Sopenharmony_ci} 19101cb0ef41Sopenharmony_ci``` 19111cb0ef41Sopenharmony_ci 19121cb0ef41Sopenharmony_ciThis function is only available on POSIX platforms (i.e. not Windows or 19131cb0ef41Sopenharmony_ciAndroid). 19141cb0ef41Sopenharmony_ci 19151cb0ef41Sopenharmony_ci## `process.geteuid()` 19161cb0ef41Sopenharmony_ci 19171cb0ef41Sopenharmony_ci<!-- YAML 19181cb0ef41Sopenharmony_ciadded: v2.0.0 19191cb0ef41Sopenharmony_ci--> 19201cb0ef41Sopenharmony_ci 19211cb0ef41Sopenharmony_ci* Returns: {Object} 19221cb0ef41Sopenharmony_ci 19231cb0ef41Sopenharmony_ciThe `process.geteuid()` method returns the numerical effective user identity of 19241cb0ef41Sopenharmony_cithe process. (See geteuid(2).) 19251cb0ef41Sopenharmony_ci 19261cb0ef41Sopenharmony_ci```mjs 19271cb0ef41Sopenharmony_ciimport process from 'node:process'; 19281cb0ef41Sopenharmony_ci 19291cb0ef41Sopenharmony_ciif (process.geteuid) { 19301cb0ef41Sopenharmony_ci console.log(`Current uid: ${process.geteuid()}`); 19311cb0ef41Sopenharmony_ci} 19321cb0ef41Sopenharmony_ci``` 19331cb0ef41Sopenharmony_ci 19341cb0ef41Sopenharmony_ci```cjs 19351cb0ef41Sopenharmony_ciconst process = require('node:process'); 19361cb0ef41Sopenharmony_ci 19371cb0ef41Sopenharmony_ciif (process.geteuid) { 19381cb0ef41Sopenharmony_ci console.log(`Current uid: ${process.geteuid()}`); 19391cb0ef41Sopenharmony_ci} 19401cb0ef41Sopenharmony_ci``` 19411cb0ef41Sopenharmony_ci 19421cb0ef41Sopenharmony_ciThis function is only available on POSIX platforms (i.e. not Windows or 19431cb0ef41Sopenharmony_ciAndroid). 19441cb0ef41Sopenharmony_ci 19451cb0ef41Sopenharmony_ci## `process.getgid()` 19461cb0ef41Sopenharmony_ci 19471cb0ef41Sopenharmony_ci<!-- YAML 19481cb0ef41Sopenharmony_ciadded: v0.1.31 19491cb0ef41Sopenharmony_ci--> 19501cb0ef41Sopenharmony_ci 19511cb0ef41Sopenharmony_ci* Returns: {Object} 19521cb0ef41Sopenharmony_ci 19531cb0ef41Sopenharmony_ciThe `process.getgid()` method returns the numerical group identity of the 19541cb0ef41Sopenharmony_ciprocess. (See getgid(2).) 19551cb0ef41Sopenharmony_ci 19561cb0ef41Sopenharmony_ci```mjs 19571cb0ef41Sopenharmony_ciimport process from 'node:process'; 19581cb0ef41Sopenharmony_ci 19591cb0ef41Sopenharmony_ciif (process.getgid) { 19601cb0ef41Sopenharmony_ci console.log(`Current gid: ${process.getgid()}`); 19611cb0ef41Sopenharmony_ci} 19621cb0ef41Sopenharmony_ci``` 19631cb0ef41Sopenharmony_ci 19641cb0ef41Sopenharmony_ci```cjs 19651cb0ef41Sopenharmony_ciconst process = require('node:process'); 19661cb0ef41Sopenharmony_ci 19671cb0ef41Sopenharmony_ciif (process.getgid) { 19681cb0ef41Sopenharmony_ci console.log(`Current gid: ${process.getgid()}`); 19691cb0ef41Sopenharmony_ci} 19701cb0ef41Sopenharmony_ci``` 19711cb0ef41Sopenharmony_ci 19721cb0ef41Sopenharmony_ciThis function is only available on POSIX platforms (i.e. not Windows or 19731cb0ef41Sopenharmony_ciAndroid). 19741cb0ef41Sopenharmony_ci 19751cb0ef41Sopenharmony_ci## `process.getgroups()` 19761cb0ef41Sopenharmony_ci 19771cb0ef41Sopenharmony_ci<!-- YAML 19781cb0ef41Sopenharmony_ciadded: v0.9.4 19791cb0ef41Sopenharmony_ci--> 19801cb0ef41Sopenharmony_ci 19811cb0ef41Sopenharmony_ci* Returns: {integer\[]} 19821cb0ef41Sopenharmony_ci 19831cb0ef41Sopenharmony_ciThe `process.getgroups()` method returns an array with the supplementary group 19841cb0ef41Sopenharmony_ciIDs. POSIX leaves it unspecified if the effective group ID is included but 19851cb0ef41Sopenharmony_ciNode.js ensures it always is. 19861cb0ef41Sopenharmony_ci 19871cb0ef41Sopenharmony_ci```mjs 19881cb0ef41Sopenharmony_ciimport process from 'node:process'; 19891cb0ef41Sopenharmony_ci 19901cb0ef41Sopenharmony_ciif (process.getgroups) { 19911cb0ef41Sopenharmony_ci console.log(process.getgroups()); // [ 16, 21, 297 ] 19921cb0ef41Sopenharmony_ci} 19931cb0ef41Sopenharmony_ci``` 19941cb0ef41Sopenharmony_ci 19951cb0ef41Sopenharmony_ci```cjs 19961cb0ef41Sopenharmony_ciconst process = require('node:process'); 19971cb0ef41Sopenharmony_ci 19981cb0ef41Sopenharmony_ciif (process.getgroups) { 19991cb0ef41Sopenharmony_ci console.log(process.getgroups()); // [ 16, 21, 297 ] 20001cb0ef41Sopenharmony_ci} 20011cb0ef41Sopenharmony_ci``` 20021cb0ef41Sopenharmony_ci 20031cb0ef41Sopenharmony_ciThis function is only available on POSIX platforms (i.e. not Windows or 20041cb0ef41Sopenharmony_ciAndroid). 20051cb0ef41Sopenharmony_ci 20061cb0ef41Sopenharmony_ci## `process.getuid()` 20071cb0ef41Sopenharmony_ci 20081cb0ef41Sopenharmony_ci<!-- YAML 20091cb0ef41Sopenharmony_ciadded: v0.1.28 20101cb0ef41Sopenharmony_ci--> 20111cb0ef41Sopenharmony_ci 20121cb0ef41Sopenharmony_ci* Returns: {integer} 20131cb0ef41Sopenharmony_ci 20141cb0ef41Sopenharmony_ciThe `process.getuid()` method returns the numeric user identity of the process. 20151cb0ef41Sopenharmony_ci(See getuid(2).) 20161cb0ef41Sopenharmony_ci 20171cb0ef41Sopenharmony_ci```mjs 20181cb0ef41Sopenharmony_ciimport process from 'node:process'; 20191cb0ef41Sopenharmony_ci 20201cb0ef41Sopenharmony_ciif (process.getuid) { 20211cb0ef41Sopenharmony_ci console.log(`Current uid: ${process.getuid()}`); 20221cb0ef41Sopenharmony_ci} 20231cb0ef41Sopenharmony_ci``` 20241cb0ef41Sopenharmony_ci 20251cb0ef41Sopenharmony_ci```cjs 20261cb0ef41Sopenharmony_ciconst process = require('node:process'); 20271cb0ef41Sopenharmony_ci 20281cb0ef41Sopenharmony_ciif (process.getuid) { 20291cb0ef41Sopenharmony_ci console.log(`Current uid: ${process.getuid()}`); 20301cb0ef41Sopenharmony_ci} 20311cb0ef41Sopenharmony_ci``` 20321cb0ef41Sopenharmony_ci 20331cb0ef41Sopenharmony_ciThis function is only available on POSIX platforms (i.e. not Windows or 20341cb0ef41Sopenharmony_ciAndroid). 20351cb0ef41Sopenharmony_ci 20361cb0ef41Sopenharmony_ci## `process.hasUncaughtExceptionCaptureCallback()` 20371cb0ef41Sopenharmony_ci 20381cb0ef41Sopenharmony_ci<!-- YAML 20391cb0ef41Sopenharmony_ciadded: v9.3.0 20401cb0ef41Sopenharmony_ci--> 20411cb0ef41Sopenharmony_ci 20421cb0ef41Sopenharmony_ci* Returns: {boolean} 20431cb0ef41Sopenharmony_ci 20441cb0ef41Sopenharmony_ciIndicates whether a callback has been set using 20451cb0ef41Sopenharmony_ci[`process.setUncaughtExceptionCaptureCallback()`][]. 20461cb0ef41Sopenharmony_ci 20471cb0ef41Sopenharmony_ci## `process.hrtime([time])` 20481cb0ef41Sopenharmony_ci 20491cb0ef41Sopenharmony_ci<!-- YAML 20501cb0ef41Sopenharmony_ciadded: v0.7.6 20511cb0ef41Sopenharmony_ci--> 20521cb0ef41Sopenharmony_ci 20531cb0ef41Sopenharmony_ci> Stability: 3 - Legacy. Use [`process.hrtime.bigint()`][] instead. 20541cb0ef41Sopenharmony_ci 20551cb0ef41Sopenharmony_ci* `time` {integer\[]} The result of a previous call to `process.hrtime()` 20561cb0ef41Sopenharmony_ci* Returns: {integer\[]} 20571cb0ef41Sopenharmony_ci 20581cb0ef41Sopenharmony_ciThis is the legacy version of [`process.hrtime.bigint()`][] 20591cb0ef41Sopenharmony_cibefore `bigint` was introduced in JavaScript. 20601cb0ef41Sopenharmony_ci 20611cb0ef41Sopenharmony_ciThe `process.hrtime()` method returns the current high-resolution real time 20621cb0ef41Sopenharmony_ciin a `[seconds, nanoseconds]` tuple `Array`, where `nanoseconds` is the 20631cb0ef41Sopenharmony_ciremaining part of the real time that can't be represented in second precision. 20641cb0ef41Sopenharmony_ci 20651cb0ef41Sopenharmony_ci`time` is an optional parameter that must be the result of a previous 20661cb0ef41Sopenharmony_ci`process.hrtime()` call to diff with the current time. If the parameter 20671cb0ef41Sopenharmony_cipassed in is not a tuple `Array`, a `TypeError` will be thrown. Passing in a 20681cb0ef41Sopenharmony_ciuser-defined array instead of the result of a previous call to 20691cb0ef41Sopenharmony_ci`process.hrtime()` will lead to undefined behavior. 20701cb0ef41Sopenharmony_ci 20711cb0ef41Sopenharmony_ciThese times are relative to an arbitrary time in the 20721cb0ef41Sopenharmony_cipast, and not related to the time of day and therefore not subject to clock 20731cb0ef41Sopenharmony_cidrift. The primary use is for measuring performance between intervals: 20741cb0ef41Sopenharmony_ci 20751cb0ef41Sopenharmony_ci```mjs 20761cb0ef41Sopenharmony_ciimport { hrtime } from 'node:process'; 20771cb0ef41Sopenharmony_ci 20781cb0ef41Sopenharmony_ciconst NS_PER_SEC = 1e9; 20791cb0ef41Sopenharmony_ciconst time = hrtime(); 20801cb0ef41Sopenharmony_ci// [ 1800216, 25 ] 20811cb0ef41Sopenharmony_ci 20821cb0ef41Sopenharmony_cisetTimeout(() => { 20831cb0ef41Sopenharmony_ci const diff = hrtime(time); 20841cb0ef41Sopenharmony_ci // [ 1, 552 ] 20851cb0ef41Sopenharmony_ci 20861cb0ef41Sopenharmony_ci console.log(`Benchmark took ${diff[0] * NS_PER_SEC + diff[1]} nanoseconds`); 20871cb0ef41Sopenharmony_ci // Benchmark took 1000000552 nanoseconds 20881cb0ef41Sopenharmony_ci}, 1000); 20891cb0ef41Sopenharmony_ci``` 20901cb0ef41Sopenharmony_ci 20911cb0ef41Sopenharmony_ci```cjs 20921cb0ef41Sopenharmony_ciconst { hrtime } = require('node:process'); 20931cb0ef41Sopenharmony_ci 20941cb0ef41Sopenharmony_ciconst NS_PER_SEC = 1e9; 20951cb0ef41Sopenharmony_ciconst time = hrtime(); 20961cb0ef41Sopenharmony_ci// [ 1800216, 25 ] 20971cb0ef41Sopenharmony_ci 20981cb0ef41Sopenharmony_cisetTimeout(() => { 20991cb0ef41Sopenharmony_ci const diff = hrtime(time); 21001cb0ef41Sopenharmony_ci // [ 1, 552 ] 21011cb0ef41Sopenharmony_ci 21021cb0ef41Sopenharmony_ci console.log(`Benchmark took ${diff[0] * NS_PER_SEC + diff[1]} nanoseconds`); 21031cb0ef41Sopenharmony_ci // Benchmark took 1000000552 nanoseconds 21041cb0ef41Sopenharmony_ci}, 1000); 21051cb0ef41Sopenharmony_ci``` 21061cb0ef41Sopenharmony_ci 21071cb0ef41Sopenharmony_ci## `process.hrtime.bigint()` 21081cb0ef41Sopenharmony_ci 21091cb0ef41Sopenharmony_ci<!-- YAML 21101cb0ef41Sopenharmony_ciadded: v10.7.0 21111cb0ef41Sopenharmony_ci--> 21121cb0ef41Sopenharmony_ci 21131cb0ef41Sopenharmony_ci* Returns: {bigint} 21141cb0ef41Sopenharmony_ci 21151cb0ef41Sopenharmony_ciThe `bigint` version of the [`process.hrtime()`][] method returning the 21161cb0ef41Sopenharmony_cicurrent high-resolution real time in nanoseconds as a `bigint`. 21171cb0ef41Sopenharmony_ci 21181cb0ef41Sopenharmony_ciUnlike [`process.hrtime()`][], it does not support an additional `time` 21191cb0ef41Sopenharmony_ciargument since the difference can just be computed directly 21201cb0ef41Sopenharmony_ciby subtraction of the two `bigint`s. 21211cb0ef41Sopenharmony_ci 21221cb0ef41Sopenharmony_ci```mjs 21231cb0ef41Sopenharmony_ciimport { hrtime } from 'node:process'; 21241cb0ef41Sopenharmony_ci 21251cb0ef41Sopenharmony_ciconst start = hrtime.bigint(); 21261cb0ef41Sopenharmony_ci// 191051479007711n 21271cb0ef41Sopenharmony_ci 21281cb0ef41Sopenharmony_cisetTimeout(() => { 21291cb0ef41Sopenharmony_ci const end = hrtime.bigint(); 21301cb0ef41Sopenharmony_ci // 191052633396993n 21311cb0ef41Sopenharmony_ci 21321cb0ef41Sopenharmony_ci console.log(`Benchmark took ${end - start} nanoseconds`); 21331cb0ef41Sopenharmony_ci // Benchmark took 1154389282 nanoseconds 21341cb0ef41Sopenharmony_ci}, 1000); 21351cb0ef41Sopenharmony_ci``` 21361cb0ef41Sopenharmony_ci 21371cb0ef41Sopenharmony_ci```cjs 21381cb0ef41Sopenharmony_ciconst { hrtime } = require('node:process'); 21391cb0ef41Sopenharmony_ci 21401cb0ef41Sopenharmony_ciconst start = hrtime.bigint(); 21411cb0ef41Sopenharmony_ci// 191051479007711n 21421cb0ef41Sopenharmony_ci 21431cb0ef41Sopenharmony_cisetTimeout(() => { 21441cb0ef41Sopenharmony_ci const end = hrtime.bigint(); 21451cb0ef41Sopenharmony_ci // 191052633396993n 21461cb0ef41Sopenharmony_ci 21471cb0ef41Sopenharmony_ci console.log(`Benchmark took ${end - start} nanoseconds`); 21481cb0ef41Sopenharmony_ci // Benchmark took 1154389282 nanoseconds 21491cb0ef41Sopenharmony_ci}, 1000); 21501cb0ef41Sopenharmony_ci``` 21511cb0ef41Sopenharmony_ci 21521cb0ef41Sopenharmony_ci## `process.initgroups(user, extraGroup)` 21531cb0ef41Sopenharmony_ci 21541cb0ef41Sopenharmony_ci<!-- YAML 21551cb0ef41Sopenharmony_ciadded: v0.9.4 21561cb0ef41Sopenharmony_ci--> 21571cb0ef41Sopenharmony_ci 21581cb0ef41Sopenharmony_ci* `user` {string|number} The user name or numeric identifier. 21591cb0ef41Sopenharmony_ci* `extraGroup` {string|number} A group name or numeric identifier. 21601cb0ef41Sopenharmony_ci 21611cb0ef41Sopenharmony_ciThe `process.initgroups()` method reads the `/etc/group` file and initializes 21621cb0ef41Sopenharmony_cithe group access list, using all groups of which the user is a member. This is 21631cb0ef41Sopenharmony_cia privileged operation that requires that the Node.js process either have `root` 21641cb0ef41Sopenharmony_ciaccess or the `CAP_SETGID` capability. 21651cb0ef41Sopenharmony_ci 21661cb0ef41Sopenharmony_ciUse care when dropping privileges: 21671cb0ef41Sopenharmony_ci 21681cb0ef41Sopenharmony_ci```mjs 21691cb0ef41Sopenharmony_ciimport { getgroups, initgroups, setgid } from 'node:process'; 21701cb0ef41Sopenharmony_ci 21711cb0ef41Sopenharmony_ciconsole.log(getgroups()); // [ 0 ] 21721cb0ef41Sopenharmony_ciinitgroups('nodeuser', 1000); // switch user 21731cb0ef41Sopenharmony_ciconsole.log(getgroups()); // [ 27, 30, 46, 1000, 0 ] 21741cb0ef41Sopenharmony_cisetgid(1000); // drop root gid 21751cb0ef41Sopenharmony_ciconsole.log(getgroups()); // [ 27, 30, 46, 1000 ] 21761cb0ef41Sopenharmony_ci``` 21771cb0ef41Sopenharmony_ci 21781cb0ef41Sopenharmony_ci```cjs 21791cb0ef41Sopenharmony_ciconst { getgroups, initgroups, setgid } = require('node:process'); 21801cb0ef41Sopenharmony_ci 21811cb0ef41Sopenharmony_ciconsole.log(getgroups()); // [ 0 ] 21821cb0ef41Sopenharmony_ciinitgroups('nodeuser', 1000); // switch user 21831cb0ef41Sopenharmony_ciconsole.log(getgroups()); // [ 27, 30, 46, 1000, 0 ] 21841cb0ef41Sopenharmony_cisetgid(1000); // drop root gid 21851cb0ef41Sopenharmony_ciconsole.log(getgroups()); // [ 27, 30, 46, 1000 ] 21861cb0ef41Sopenharmony_ci``` 21871cb0ef41Sopenharmony_ci 21881cb0ef41Sopenharmony_ciThis function is only available on POSIX platforms (i.e. not Windows or 21891cb0ef41Sopenharmony_ciAndroid). 21901cb0ef41Sopenharmony_ciThis feature is not available in [`Worker`][] threads. 21911cb0ef41Sopenharmony_ci 21921cb0ef41Sopenharmony_ci## `process.kill(pid[, signal])` 21931cb0ef41Sopenharmony_ci 21941cb0ef41Sopenharmony_ci<!-- YAML 21951cb0ef41Sopenharmony_ciadded: v0.0.6 21961cb0ef41Sopenharmony_ci--> 21971cb0ef41Sopenharmony_ci 21981cb0ef41Sopenharmony_ci* `pid` {number} A process ID 21991cb0ef41Sopenharmony_ci* `signal` {string|number} The signal to send, either as a string or number. 22001cb0ef41Sopenharmony_ci **Default:** `'SIGTERM'`. 22011cb0ef41Sopenharmony_ci 22021cb0ef41Sopenharmony_ciThe `process.kill()` method sends the `signal` to the process identified by 22031cb0ef41Sopenharmony_ci`pid`. 22041cb0ef41Sopenharmony_ci 22051cb0ef41Sopenharmony_ciSignal names are strings such as `'SIGINT'` or `'SIGHUP'`. See [Signal Events][] 22061cb0ef41Sopenharmony_ciand kill(2) for more information. 22071cb0ef41Sopenharmony_ci 22081cb0ef41Sopenharmony_ciThis method will throw an error if the target `pid` does not exist. As a special 22091cb0ef41Sopenharmony_cicase, a signal of `0` can be used to test for the existence of a process. 22101cb0ef41Sopenharmony_ciWindows platforms will throw an error if the `pid` is used to kill a process 22111cb0ef41Sopenharmony_cigroup. 22121cb0ef41Sopenharmony_ci 22131cb0ef41Sopenharmony_ciEven though the name of this function is `process.kill()`, it is really just a 22141cb0ef41Sopenharmony_cisignal sender, like the `kill` system call. The signal sent may do something 22151cb0ef41Sopenharmony_ciother than kill the target process. 22161cb0ef41Sopenharmony_ci 22171cb0ef41Sopenharmony_ci```mjs 22181cb0ef41Sopenharmony_ciimport process, { kill } from 'node:process'; 22191cb0ef41Sopenharmony_ci 22201cb0ef41Sopenharmony_ciprocess.on('SIGHUP', () => { 22211cb0ef41Sopenharmony_ci console.log('Got SIGHUP signal.'); 22221cb0ef41Sopenharmony_ci}); 22231cb0ef41Sopenharmony_ci 22241cb0ef41Sopenharmony_cisetTimeout(() => { 22251cb0ef41Sopenharmony_ci console.log('Exiting.'); 22261cb0ef41Sopenharmony_ci process.exit(0); 22271cb0ef41Sopenharmony_ci}, 100); 22281cb0ef41Sopenharmony_ci 22291cb0ef41Sopenharmony_cikill(process.pid, 'SIGHUP'); 22301cb0ef41Sopenharmony_ci``` 22311cb0ef41Sopenharmony_ci 22321cb0ef41Sopenharmony_ci```cjs 22331cb0ef41Sopenharmony_ciconst process = require('node:process'); 22341cb0ef41Sopenharmony_ci 22351cb0ef41Sopenharmony_ciprocess.on('SIGHUP', () => { 22361cb0ef41Sopenharmony_ci console.log('Got SIGHUP signal.'); 22371cb0ef41Sopenharmony_ci}); 22381cb0ef41Sopenharmony_ci 22391cb0ef41Sopenharmony_cisetTimeout(() => { 22401cb0ef41Sopenharmony_ci console.log('Exiting.'); 22411cb0ef41Sopenharmony_ci process.exit(0); 22421cb0ef41Sopenharmony_ci}, 100); 22431cb0ef41Sopenharmony_ci 22441cb0ef41Sopenharmony_ciprocess.kill(process.pid, 'SIGHUP'); 22451cb0ef41Sopenharmony_ci``` 22461cb0ef41Sopenharmony_ci 22471cb0ef41Sopenharmony_ciWhen `SIGUSR1` is received by a Node.js process, Node.js will start the 22481cb0ef41Sopenharmony_cidebugger. See [Signal Events][]. 22491cb0ef41Sopenharmony_ci 22501cb0ef41Sopenharmony_ci## `process.mainModule` 22511cb0ef41Sopenharmony_ci 22521cb0ef41Sopenharmony_ci<!-- YAML 22531cb0ef41Sopenharmony_ciadded: v0.1.17 22541cb0ef41Sopenharmony_cideprecated: v14.0.0 22551cb0ef41Sopenharmony_ci--> 22561cb0ef41Sopenharmony_ci 22571cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use [`require.main`][] instead. 22581cb0ef41Sopenharmony_ci 22591cb0ef41Sopenharmony_ci* {Object} 22601cb0ef41Sopenharmony_ci 22611cb0ef41Sopenharmony_ciThe `process.mainModule` property provides an alternative way of retrieving 22621cb0ef41Sopenharmony_ci[`require.main`][]. The difference is that if the main module changes at 22631cb0ef41Sopenharmony_ciruntime, [`require.main`][] may still refer to the original main module in 22641cb0ef41Sopenharmony_cimodules that were required before the change occurred. Generally, it's 22651cb0ef41Sopenharmony_cisafe to assume that the two refer to the same module. 22661cb0ef41Sopenharmony_ci 22671cb0ef41Sopenharmony_ciAs with [`require.main`][], `process.mainModule` will be `undefined` if there 22681cb0ef41Sopenharmony_ciis no entry script. 22691cb0ef41Sopenharmony_ci 22701cb0ef41Sopenharmony_ci## `process.memoryUsage()` 22711cb0ef41Sopenharmony_ci 22721cb0ef41Sopenharmony_ci<!-- YAML 22731cb0ef41Sopenharmony_ciadded: v0.1.16 22741cb0ef41Sopenharmony_cichanges: 22751cb0ef41Sopenharmony_ci - version: 22761cb0ef41Sopenharmony_ci - v13.9.0 22771cb0ef41Sopenharmony_ci - v12.17.0 22781cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/31550 22791cb0ef41Sopenharmony_ci description: Added `arrayBuffers` to the returned object. 22801cb0ef41Sopenharmony_ci - version: v7.2.0 22811cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/9587 22821cb0ef41Sopenharmony_ci description: Added `external` to the returned object. 22831cb0ef41Sopenharmony_ci--> 22841cb0ef41Sopenharmony_ci 22851cb0ef41Sopenharmony_ci* Returns: {Object} 22861cb0ef41Sopenharmony_ci * `rss` {integer} 22871cb0ef41Sopenharmony_ci * `heapTotal` {integer} 22881cb0ef41Sopenharmony_ci * `heapUsed` {integer} 22891cb0ef41Sopenharmony_ci * `external` {integer} 22901cb0ef41Sopenharmony_ci * `arrayBuffers` {integer} 22911cb0ef41Sopenharmony_ci 22921cb0ef41Sopenharmony_ciReturns an object describing the memory usage of the Node.js process measured in 22931cb0ef41Sopenharmony_cibytes. 22941cb0ef41Sopenharmony_ci 22951cb0ef41Sopenharmony_ci```mjs 22961cb0ef41Sopenharmony_ciimport { memoryUsage } from 'node:process'; 22971cb0ef41Sopenharmony_ci 22981cb0ef41Sopenharmony_ciconsole.log(memoryUsage()); 22991cb0ef41Sopenharmony_ci// Prints: 23001cb0ef41Sopenharmony_ci// { 23011cb0ef41Sopenharmony_ci// rss: 4935680, 23021cb0ef41Sopenharmony_ci// heapTotal: 1826816, 23031cb0ef41Sopenharmony_ci// heapUsed: 650472, 23041cb0ef41Sopenharmony_ci// external: 49879, 23051cb0ef41Sopenharmony_ci// arrayBuffers: 9386 23061cb0ef41Sopenharmony_ci// } 23071cb0ef41Sopenharmony_ci``` 23081cb0ef41Sopenharmony_ci 23091cb0ef41Sopenharmony_ci```cjs 23101cb0ef41Sopenharmony_ciconst { memoryUsage } = require('node:process'); 23111cb0ef41Sopenharmony_ci 23121cb0ef41Sopenharmony_ciconsole.log(memoryUsage()); 23131cb0ef41Sopenharmony_ci// Prints: 23141cb0ef41Sopenharmony_ci// { 23151cb0ef41Sopenharmony_ci// rss: 4935680, 23161cb0ef41Sopenharmony_ci// heapTotal: 1826816, 23171cb0ef41Sopenharmony_ci// heapUsed: 650472, 23181cb0ef41Sopenharmony_ci// external: 49879, 23191cb0ef41Sopenharmony_ci// arrayBuffers: 9386 23201cb0ef41Sopenharmony_ci// } 23211cb0ef41Sopenharmony_ci``` 23221cb0ef41Sopenharmony_ci 23231cb0ef41Sopenharmony_ci* `heapTotal` and `heapUsed` refer to V8's memory usage. 23241cb0ef41Sopenharmony_ci* `external` refers to the memory usage of C++ objects bound to JavaScript 23251cb0ef41Sopenharmony_ci objects managed by V8. 23261cb0ef41Sopenharmony_ci* `rss`, Resident Set Size, is the amount of space occupied in the main 23271cb0ef41Sopenharmony_ci memory device (that is a subset of the total allocated memory) for the 23281cb0ef41Sopenharmony_ci process, including all C++ and JavaScript objects and code. 23291cb0ef41Sopenharmony_ci* `arrayBuffers` refers to memory allocated for `ArrayBuffer`s and 23301cb0ef41Sopenharmony_ci `SharedArrayBuffer`s, including all Node.js [`Buffer`][]s. 23311cb0ef41Sopenharmony_ci This is also included in the `external` value. When Node.js is used as an 23321cb0ef41Sopenharmony_ci embedded library, this value may be `0` because allocations for `ArrayBuffer`s 23331cb0ef41Sopenharmony_ci may not be tracked in that case. 23341cb0ef41Sopenharmony_ci 23351cb0ef41Sopenharmony_ciWhen using [`Worker`][] threads, `rss` will be a value that is valid for the 23361cb0ef41Sopenharmony_cientire process, while the other fields will only refer to the current thread. 23371cb0ef41Sopenharmony_ci 23381cb0ef41Sopenharmony_ciThe `process.memoryUsage()` method iterates over each page to gather 23391cb0ef41Sopenharmony_ciinformation about memory usage which might be slow depending on the 23401cb0ef41Sopenharmony_ciprogram memory allocations. 23411cb0ef41Sopenharmony_ci 23421cb0ef41Sopenharmony_ci## `process.memoryUsage.rss()` 23431cb0ef41Sopenharmony_ci 23441cb0ef41Sopenharmony_ci<!-- YAML 23451cb0ef41Sopenharmony_ciadded: 23461cb0ef41Sopenharmony_ci - v15.6.0 23471cb0ef41Sopenharmony_ci - v14.18.0 23481cb0ef41Sopenharmony_ci--> 23491cb0ef41Sopenharmony_ci 23501cb0ef41Sopenharmony_ci* Returns: {integer} 23511cb0ef41Sopenharmony_ci 23521cb0ef41Sopenharmony_ciThe `process.memoryUsage.rss()` method returns an integer representing the 23531cb0ef41Sopenharmony_ciResident Set Size (RSS) in bytes. 23541cb0ef41Sopenharmony_ci 23551cb0ef41Sopenharmony_ciThe Resident Set Size, is the amount of space occupied in the main 23561cb0ef41Sopenharmony_cimemory device (that is a subset of the total allocated memory) for the 23571cb0ef41Sopenharmony_ciprocess, including all C++ and JavaScript objects and code. 23581cb0ef41Sopenharmony_ci 23591cb0ef41Sopenharmony_ciThis is the same value as the `rss` property provided by `process.memoryUsage()` 23601cb0ef41Sopenharmony_cibut `process.memoryUsage.rss()` is faster. 23611cb0ef41Sopenharmony_ci 23621cb0ef41Sopenharmony_ci```mjs 23631cb0ef41Sopenharmony_ciimport { memoryUsage } from 'node:process'; 23641cb0ef41Sopenharmony_ci 23651cb0ef41Sopenharmony_ciconsole.log(memoryUsage.rss()); 23661cb0ef41Sopenharmony_ci// 35655680 23671cb0ef41Sopenharmony_ci``` 23681cb0ef41Sopenharmony_ci 23691cb0ef41Sopenharmony_ci```cjs 23701cb0ef41Sopenharmony_ciconst { memoryUsage } = require('node:process'); 23711cb0ef41Sopenharmony_ci 23721cb0ef41Sopenharmony_ciconsole.log(memoryUsage.rss()); 23731cb0ef41Sopenharmony_ci// 35655680 23741cb0ef41Sopenharmony_ci``` 23751cb0ef41Sopenharmony_ci 23761cb0ef41Sopenharmony_ci## `process.nextTick(callback[, ...args])` 23771cb0ef41Sopenharmony_ci 23781cb0ef41Sopenharmony_ci<!-- YAML 23791cb0ef41Sopenharmony_ciadded: v0.1.26 23801cb0ef41Sopenharmony_cichanges: 23811cb0ef41Sopenharmony_ci - version: v18.0.0 23821cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41678 23831cb0ef41Sopenharmony_ci description: Passing an invalid callback to the `callback` argument 23841cb0ef41Sopenharmony_ci now throws `ERR_INVALID_ARG_TYPE` instead of 23851cb0ef41Sopenharmony_ci `ERR_INVALID_CALLBACK`. 23861cb0ef41Sopenharmony_ci - version: v1.8.1 23871cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/1077 23881cb0ef41Sopenharmony_ci description: Additional arguments after `callback` are now supported. 23891cb0ef41Sopenharmony_ci--> 23901cb0ef41Sopenharmony_ci 23911cb0ef41Sopenharmony_ci* `callback` {Function} 23921cb0ef41Sopenharmony_ci* `...args` {any} Additional arguments to pass when invoking the `callback` 23931cb0ef41Sopenharmony_ci 23941cb0ef41Sopenharmony_ci`process.nextTick()` adds `callback` to the "next tick queue". This queue is 23951cb0ef41Sopenharmony_cifully drained after the current operation on the JavaScript stack runs to 23961cb0ef41Sopenharmony_cicompletion and before the event loop is allowed to continue. It's possible to 23971cb0ef41Sopenharmony_cicreate an infinite loop if one were to recursively call `process.nextTick()`. 23981cb0ef41Sopenharmony_ciSee the [Event Loop][] guide for more background. 23991cb0ef41Sopenharmony_ci 24001cb0ef41Sopenharmony_ci```mjs 24011cb0ef41Sopenharmony_ciimport { nextTick } from 'node:process'; 24021cb0ef41Sopenharmony_ci 24031cb0ef41Sopenharmony_ciconsole.log('start'); 24041cb0ef41Sopenharmony_cinextTick(() => { 24051cb0ef41Sopenharmony_ci console.log('nextTick callback'); 24061cb0ef41Sopenharmony_ci}); 24071cb0ef41Sopenharmony_ciconsole.log('scheduled'); 24081cb0ef41Sopenharmony_ci// Output: 24091cb0ef41Sopenharmony_ci// start 24101cb0ef41Sopenharmony_ci// scheduled 24111cb0ef41Sopenharmony_ci// nextTick callback 24121cb0ef41Sopenharmony_ci``` 24131cb0ef41Sopenharmony_ci 24141cb0ef41Sopenharmony_ci```cjs 24151cb0ef41Sopenharmony_ciconst { nextTick } = require('node:process'); 24161cb0ef41Sopenharmony_ci 24171cb0ef41Sopenharmony_ciconsole.log('start'); 24181cb0ef41Sopenharmony_cinextTick(() => { 24191cb0ef41Sopenharmony_ci console.log('nextTick callback'); 24201cb0ef41Sopenharmony_ci}); 24211cb0ef41Sopenharmony_ciconsole.log('scheduled'); 24221cb0ef41Sopenharmony_ci// Output: 24231cb0ef41Sopenharmony_ci// start 24241cb0ef41Sopenharmony_ci// scheduled 24251cb0ef41Sopenharmony_ci// nextTick callback 24261cb0ef41Sopenharmony_ci``` 24271cb0ef41Sopenharmony_ci 24281cb0ef41Sopenharmony_ciThis is important when developing APIs in order to give users the opportunity 24291cb0ef41Sopenharmony_cito assign event handlers _after_ an object has been constructed but before any 24301cb0ef41Sopenharmony_ciI/O has occurred: 24311cb0ef41Sopenharmony_ci 24321cb0ef41Sopenharmony_ci```mjs 24331cb0ef41Sopenharmony_ciimport { nextTick } from 'node:process'; 24341cb0ef41Sopenharmony_ci 24351cb0ef41Sopenharmony_cifunction MyThing(options) { 24361cb0ef41Sopenharmony_ci this.setupOptions(options); 24371cb0ef41Sopenharmony_ci 24381cb0ef41Sopenharmony_ci nextTick(() => { 24391cb0ef41Sopenharmony_ci this.startDoingStuff(); 24401cb0ef41Sopenharmony_ci }); 24411cb0ef41Sopenharmony_ci} 24421cb0ef41Sopenharmony_ci 24431cb0ef41Sopenharmony_ciconst thing = new MyThing(); 24441cb0ef41Sopenharmony_cithing.getReadyForStuff(); 24451cb0ef41Sopenharmony_ci 24461cb0ef41Sopenharmony_ci// thing.startDoingStuff() gets called now, not before. 24471cb0ef41Sopenharmony_ci``` 24481cb0ef41Sopenharmony_ci 24491cb0ef41Sopenharmony_ci```cjs 24501cb0ef41Sopenharmony_ciconst { nextTick } = require('node:process'); 24511cb0ef41Sopenharmony_ci 24521cb0ef41Sopenharmony_cifunction MyThing(options) { 24531cb0ef41Sopenharmony_ci this.setupOptions(options); 24541cb0ef41Sopenharmony_ci 24551cb0ef41Sopenharmony_ci nextTick(() => { 24561cb0ef41Sopenharmony_ci this.startDoingStuff(); 24571cb0ef41Sopenharmony_ci }); 24581cb0ef41Sopenharmony_ci} 24591cb0ef41Sopenharmony_ci 24601cb0ef41Sopenharmony_ciconst thing = new MyThing(); 24611cb0ef41Sopenharmony_cithing.getReadyForStuff(); 24621cb0ef41Sopenharmony_ci 24631cb0ef41Sopenharmony_ci// thing.startDoingStuff() gets called now, not before. 24641cb0ef41Sopenharmony_ci``` 24651cb0ef41Sopenharmony_ci 24661cb0ef41Sopenharmony_ciIt is very important for APIs to be either 100% synchronous or 100% 24671cb0ef41Sopenharmony_ciasynchronous. Consider this example: 24681cb0ef41Sopenharmony_ci 24691cb0ef41Sopenharmony_ci```js 24701cb0ef41Sopenharmony_ci// WARNING! DO NOT USE! BAD UNSAFE HAZARD! 24711cb0ef41Sopenharmony_cifunction maybeSync(arg, cb) { 24721cb0ef41Sopenharmony_ci if (arg) { 24731cb0ef41Sopenharmony_ci cb(); 24741cb0ef41Sopenharmony_ci return; 24751cb0ef41Sopenharmony_ci } 24761cb0ef41Sopenharmony_ci 24771cb0ef41Sopenharmony_ci fs.stat('file', cb); 24781cb0ef41Sopenharmony_ci} 24791cb0ef41Sopenharmony_ci``` 24801cb0ef41Sopenharmony_ci 24811cb0ef41Sopenharmony_ciThis API is hazardous because in the following case: 24821cb0ef41Sopenharmony_ci 24831cb0ef41Sopenharmony_ci```js 24841cb0ef41Sopenharmony_ciconst maybeTrue = Math.random() > 0.5; 24851cb0ef41Sopenharmony_ci 24861cb0ef41Sopenharmony_cimaybeSync(maybeTrue, () => { 24871cb0ef41Sopenharmony_ci foo(); 24881cb0ef41Sopenharmony_ci}); 24891cb0ef41Sopenharmony_ci 24901cb0ef41Sopenharmony_cibar(); 24911cb0ef41Sopenharmony_ci``` 24921cb0ef41Sopenharmony_ci 24931cb0ef41Sopenharmony_ciIt is not clear whether `foo()` or `bar()` will be called first. 24941cb0ef41Sopenharmony_ci 24951cb0ef41Sopenharmony_ciThe following approach is much better: 24961cb0ef41Sopenharmony_ci 24971cb0ef41Sopenharmony_ci```mjs 24981cb0ef41Sopenharmony_ciimport { nextTick } from 'node:process'; 24991cb0ef41Sopenharmony_ci 25001cb0ef41Sopenharmony_cifunction definitelyAsync(arg, cb) { 25011cb0ef41Sopenharmony_ci if (arg) { 25021cb0ef41Sopenharmony_ci nextTick(cb); 25031cb0ef41Sopenharmony_ci return; 25041cb0ef41Sopenharmony_ci } 25051cb0ef41Sopenharmony_ci 25061cb0ef41Sopenharmony_ci fs.stat('file', cb); 25071cb0ef41Sopenharmony_ci} 25081cb0ef41Sopenharmony_ci``` 25091cb0ef41Sopenharmony_ci 25101cb0ef41Sopenharmony_ci```cjs 25111cb0ef41Sopenharmony_ciconst { nextTick } = require('node:process'); 25121cb0ef41Sopenharmony_ci 25131cb0ef41Sopenharmony_cifunction definitelyAsync(arg, cb) { 25141cb0ef41Sopenharmony_ci if (arg) { 25151cb0ef41Sopenharmony_ci nextTick(cb); 25161cb0ef41Sopenharmony_ci return; 25171cb0ef41Sopenharmony_ci } 25181cb0ef41Sopenharmony_ci 25191cb0ef41Sopenharmony_ci fs.stat('file', cb); 25201cb0ef41Sopenharmony_ci} 25211cb0ef41Sopenharmony_ci``` 25221cb0ef41Sopenharmony_ci 25231cb0ef41Sopenharmony_ci### When to use `queueMicrotask()` vs. `process.nextTick()` 25241cb0ef41Sopenharmony_ci 25251cb0ef41Sopenharmony_ciThe [`queueMicrotask()`][] API is an alternative to `process.nextTick()` that 25261cb0ef41Sopenharmony_cialso defers execution of a function using the same microtask queue used to 25271cb0ef41Sopenharmony_ciexecute the then, catch, and finally handlers of resolved promises. Within 25281cb0ef41Sopenharmony_ciNode.js, every time the "next tick queue" is drained, the microtask queue 25291cb0ef41Sopenharmony_ciis drained immediately after. 25301cb0ef41Sopenharmony_ci 25311cb0ef41Sopenharmony_ci```mjs 25321cb0ef41Sopenharmony_ciimport { nextTick } from 'node:process'; 25331cb0ef41Sopenharmony_ci 25341cb0ef41Sopenharmony_ciPromise.resolve().then(() => console.log(2)); 25351cb0ef41Sopenharmony_ciqueueMicrotask(() => console.log(3)); 25361cb0ef41Sopenharmony_cinextTick(() => console.log(1)); 25371cb0ef41Sopenharmony_ci// Output: 25381cb0ef41Sopenharmony_ci// 1 25391cb0ef41Sopenharmony_ci// 2 25401cb0ef41Sopenharmony_ci// 3 25411cb0ef41Sopenharmony_ci``` 25421cb0ef41Sopenharmony_ci 25431cb0ef41Sopenharmony_ci```cjs 25441cb0ef41Sopenharmony_ciconst { nextTick } = require('node:process'); 25451cb0ef41Sopenharmony_ci 25461cb0ef41Sopenharmony_ciPromise.resolve().then(() => console.log(2)); 25471cb0ef41Sopenharmony_ciqueueMicrotask(() => console.log(3)); 25481cb0ef41Sopenharmony_cinextTick(() => console.log(1)); 25491cb0ef41Sopenharmony_ci// Output: 25501cb0ef41Sopenharmony_ci// 1 25511cb0ef41Sopenharmony_ci// 2 25521cb0ef41Sopenharmony_ci// 3 25531cb0ef41Sopenharmony_ci``` 25541cb0ef41Sopenharmony_ci 25551cb0ef41Sopenharmony_ciFor _most_ userland use cases, the `queueMicrotask()` API provides a portable 25561cb0ef41Sopenharmony_ciand reliable mechanism for deferring execution that works across multiple 25571cb0ef41Sopenharmony_ciJavaScript platform environments and should be favored over `process.nextTick()`. 25581cb0ef41Sopenharmony_ciIn simple scenarios, `queueMicrotask()` can be a drop-in replacement for 25591cb0ef41Sopenharmony_ci`process.nextTick()`. 25601cb0ef41Sopenharmony_ci 25611cb0ef41Sopenharmony_ci```js 25621cb0ef41Sopenharmony_ciconsole.log('start'); 25631cb0ef41Sopenharmony_ciqueueMicrotask(() => { 25641cb0ef41Sopenharmony_ci console.log('microtask callback'); 25651cb0ef41Sopenharmony_ci}); 25661cb0ef41Sopenharmony_ciconsole.log('scheduled'); 25671cb0ef41Sopenharmony_ci// Output: 25681cb0ef41Sopenharmony_ci// start 25691cb0ef41Sopenharmony_ci// scheduled 25701cb0ef41Sopenharmony_ci// microtask callback 25711cb0ef41Sopenharmony_ci``` 25721cb0ef41Sopenharmony_ci 25731cb0ef41Sopenharmony_ciOne note-worthy difference between the two APIs is that `process.nextTick()` 25741cb0ef41Sopenharmony_ciallows specifying additional values that will be passed as arguments to the 25751cb0ef41Sopenharmony_cideferred function when it is called. Achieving the same result with 25761cb0ef41Sopenharmony_ci`queueMicrotask()` requires using either a closure or a bound function: 25771cb0ef41Sopenharmony_ci 25781cb0ef41Sopenharmony_ci```js 25791cb0ef41Sopenharmony_cifunction deferred(a, b) { 25801cb0ef41Sopenharmony_ci console.log('microtask', a + b); 25811cb0ef41Sopenharmony_ci} 25821cb0ef41Sopenharmony_ci 25831cb0ef41Sopenharmony_ciconsole.log('start'); 25841cb0ef41Sopenharmony_ciqueueMicrotask(deferred.bind(undefined, 1, 2)); 25851cb0ef41Sopenharmony_ciconsole.log('scheduled'); 25861cb0ef41Sopenharmony_ci// Output: 25871cb0ef41Sopenharmony_ci// start 25881cb0ef41Sopenharmony_ci// scheduled 25891cb0ef41Sopenharmony_ci// microtask 3 25901cb0ef41Sopenharmony_ci``` 25911cb0ef41Sopenharmony_ci 25921cb0ef41Sopenharmony_ciThere are minor differences in the way errors raised from within the next tick 25931cb0ef41Sopenharmony_ciqueue and microtask queue are handled. Errors thrown within a queued microtask 25941cb0ef41Sopenharmony_cicallback should be handled within the queued callback when possible. If they are 25951cb0ef41Sopenharmony_cinot, the `process.on('uncaughtException')` event handler can be used to capture 25961cb0ef41Sopenharmony_ciand handle the errors. 25971cb0ef41Sopenharmony_ci 25981cb0ef41Sopenharmony_ciWhen in doubt, unless the specific capabilities of `process.nextTick()` are 25991cb0ef41Sopenharmony_cineeded, use `queueMicrotask()`. 26001cb0ef41Sopenharmony_ci 26011cb0ef41Sopenharmony_ci## `process.noDeprecation` 26021cb0ef41Sopenharmony_ci 26031cb0ef41Sopenharmony_ci<!-- YAML 26041cb0ef41Sopenharmony_ciadded: v0.8.0 26051cb0ef41Sopenharmony_ci--> 26061cb0ef41Sopenharmony_ci 26071cb0ef41Sopenharmony_ci* {boolean} 26081cb0ef41Sopenharmony_ci 26091cb0ef41Sopenharmony_ciThe `process.noDeprecation` property indicates whether the `--no-deprecation` 26101cb0ef41Sopenharmony_ciflag is set on the current Node.js process. See the documentation for 26111cb0ef41Sopenharmony_cithe [`'warning'` event][process_warning] and the 26121cb0ef41Sopenharmony_ci[`emitWarning()` method][process_emit_warning] for more information about this 26131cb0ef41Sopenharmony_ciflag's behavior. 26141cb0ef41Sopenharmony_ci 26151cb0ef41Sopenharmony_ci## `process.pid` 26161cb0ef41Sopenharmony_ci 26171cb0ef41Sopenharmony_ci<!-- YAML 26181cb0ef41Sopenharmony_ciadded: v0.1.15 26191cb0ef41Sopenharmony_ci--> 26201cb0ef41Sopenharmony_ci 26211cb0ef41Sopenharmony_ci* {integer} 26221cb0ef41Sopenharmony_ci 26231cb0ef41Sopenharmony_ciThe `process.pid` property returns the PID of the process. 26241cb0ef41Sopenharmony_ci 26251cb0ef41Sopenharmony_ci```mjs 26261cb0ef41Sopenharmony_ciimport { pid } from 'node:process'; 26271cb0ef41Sopenharmony_ci 26281cb0ef41Sopenharmony_ciconsole.log(`This process is pid ${pid}`); 26291cb0ef41Sopenharmony_ci``` 26301cb0ef41Sopenharmony_ci 26311cb0ef41Sopenharmony_ci```cjs 26321cb0ef41Sopenharmony_ciconst { pid } = require('node:process'); 26331cb0ef41Sopenharmony_ci 26341cb0ef41Sopenharmony_ciconsole.log(`This process is pid ${pid}`); 26351cb0ef41Sopenharmony_ci``` 26361cb0ef41Sopenharmony_ci 26371cb0ef41Sopenharmony_ci## `process.platform` 26381cb0ef41Sopenharmony_ci 26391cb0ef41Sopenharmony_ci<!-- YAML 26401cb0ef41Sopenharmony_ciadded: v0.1.16 26411cb0ef41Sopenharmony_ci--> 26421cb0ef41Sopenharmony_ci 26431cb0ef41Sopenharmony_ci* {string} 26441cb0ef41Sopenharmony_ci 26451cb0ef41Sopenharmony_ciThe `process.platform` property returns a string identifying the operating 26461cb0ef41Sopenharmony_cisystem platform for which the Node.js binary was compiled. 26471cb0ef41Sopenharmony_ci 26481cb0ef41Sopenharmony_ciCurrently possible values are: 26491cb0ef41Sopenharmony_ci 26501cb0ef41Sopenharmony_ci* `'aix'` 26511cb0ef41Sopenharmony_ci* `'darwin'` 26521cb0ef41Sopenharmony_ci* `'freebsd'` 26531cb0ef41Sopenharmony_ci* `'linux'` 26541cb0ef41Sopenharmony_ci* `'openbsd'` 26551cb0ef41Sopenharmony_ci* `'sunos'` 26561cb0ef41Sopenharmony_ci* `'win32'` 26571cb0ef41Sopenharmony_ci 26581cb0ef41Sopenharmony_ci```mjs 26591cb0ef41Sopenharmony_ciimport { platform } from 'node:process'; 26601cb0ef41Sopenharmony_ci 26611cb0ef41Sopenharmony_ciconsole.log(`This platform is ${platform}`); 26621cb0ef41Sopenharmony_ci``` 26631cb0ef41Sopenharmony_ci 26641cb0ef41Sopenharmony_ci```cjs 26651cb0ef41Sopenharmony_ciconst { platform } = require('node:process'); 26661cb0ef41Sopenharmony_ci 26671cb0ef41Sopenharmony_ciconsole.log(`This platform is ${platform}`); 26681cb0ef41Sopenharmony_ci``` 26691cb0ef41Sopenharmony_ci 26701cb0ef41Sopenharmony_ciThe value `'android'` may also be returned if the Node.js is built on the 26711cb0ef41Sopenharmony_ciAndroid operating system. However, Android support in Node.js 26721cb0ef41Sopenharmony_ci[is experimental][Android building]. 26731cb0ef41Sopenharmony_ci 26741cb0ef41Sopenharmony_ci## `process.ppid` 26751cb0ef41Sopenharmony_ci 26761cb0ef41Sopenharmony_ci<!-- YAML 26771cb0ef41Sopenharmony_ciadded: 26781cb0ef41Sopenharmony_ci - v9.2.0 26791cb0ef41Sopenharmony_ci - v8.10.0 26801cb0ef41Sopenharmony_ci - v6.13.0 26811cb0ef41Sopenharmony_ci--> 26821cb0ef41Sopenharmony_ci 26831cb0ef41Sopenharmony_ci* {integer} 26841cb0ef41Sopenharmony_ci 26851cb0ef41Sopenharmony_ciThe `process.ppid` property returns the PID of the parent of the 26861cb0ef41Sopenharmony_cicurrent process. 26871cb0ef41Sopenharmony_ci 26881cb0ef41Sopenharmony_ci```mjs 26891cb0ef41Sopenharmony_ciimport { ppid } from 'node:process'; 26901cb0ef41Sopenharmony_ci 26911cb0ef41Sopenharmony_ciconsole.log(`The parent process is pid ${ppid}`); 26921cb0ef41Sopenharmony_ci``` 26931cb0ef41Sopenharmony_ci 26941cb0ef41Sopenharmony_ci```cjs 26951cb0ef41Sopenharmony_ciconst { ppid } = require('node:process'); 26961cb0ef41Sopenharmony_ci 26971cb0ef41Sopenharmony_ciconsole.log(`The parent process is pid ${ppid}`); 26981cb0ef41Sopenharmony_ci``` 26991cb0ef41Sopenharmony_ci 27001cb0ef41Sopenharmony_ci## `process.release` 27011cb0ef41Sopenharmony_ci 27021cb0ef41Sopenharmony_ci<!-- YAML 27031cb0ef41Sopenharmony_ciadded: v3.0.0 27041cb0ef41Sopenharmony_cichanges: 27051cb0ef41Sopenharmony_ci - version: v4.2.0 27061cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/3212 27071cb0ef41Sopenharmony_ci description: The `lts` property is now supported. 27081cb0ef41Sopenharmony_ci--> 27091cb0ef41Sopenharmony_ci 27101cb0ef41Sopenharmony_ci* {Object} 27111cb0ef41Sopenharmony_ci 27121cb0ef41Sopenharmony_ciThe `process.release` property returns an `Object` containing metadata related 27131cb0ef41Sopenharmony_cito the current release, including URLs for the source tarball and headers-only 27141cb0ef41Sopenharmony_citarball. 27151cb0ef41Sopenharmony_ci 27161cb0ef41Sopenharmony_ci`process.release` contains the following properties: 27171cb0ef41Sopenharmony_ci 27181cb0ef41Sopenharmony_ci* `name` {string} A value that will always be `'node'`. 27191cb0ef41Sopenharmony_ci* `sourceUrl` {string} an absolute URL pointing to a _`.tar.gz`_ file containing 27201cb0ef41Sopenharmony_ci the source code of the current release. 27211cb0ef41Sopenharmony_ci* `headersUrl`{string} an absolute URL pointing to a _`.tar.gz`_ file containing 27221cb0ef41Sopenharmony_ci only the source header files for the current release. This file is 27231cb0ef41Sopenharmony_ci significantly smaller than the full source file and can be used for compiling 27241cb0ef41Sopenharmony_ci Node.js native add-ons. 27251cb0ef41Sopenharmony_ci* `libUrl` {string|undefined} an absolute URL pointing to a _`node.lib`_ file 27261cb0ef41Sopenharmony_ci matching the architecture and version of the current release. This file is 27271cb0ef41Sopenharmony_ci used for compiling Node.js native add-ons. _This property is only present on 27281cb0ef41Sopenharmony_ci Windows builds of Node.js and will be missing on all other platforms._ 27291cb0ef41Sopenharmony_ci* `lts` {string|undefined} a string label identifying the [LTS][] label for this 27301cb0ef41Sopenharmony_ci release. This property only exists for LTS releases and is `undefined` for all 27311cb0ef41Sopenharmony_ci other release types, including _Current_ releases. Valid values include the 27321cb0ef41Sopenharmony_ci LTS Release code names (including those that are no longer supported). 27331cb0ef41Sopenharmony_ci * `'Fermium'` for the 14.x LTS line beginning with 14.15.0. 27341cb0ef41Sopenharmony_ci * `'Gallium'` for the 16.x LTS line beginning with 16.13.0. 27351cb0ef41Sopenharmony_ci * `'Hydrogen'` for the 18.x LTS line beginning with 18.12.0. 27361cb0ef41Sopenharmony_ci For other LTS Release code names, see [Node.js Changelog Archive](https://github.com/nodejs/node/blob/HEAD/doc/changelogs/CHANGELOG_ARCHIVE.md) 27371cb0ef41Sopenharmony_ci 27381cb0ef41Sopenharmony_ci<!-- eslint-skip --> 27391cb0ef41Sopenharmony_ci 27401cb0ef41Sopenharmony_ci```js 27411cb0ef41Sopenharmony_ci{ 27421cb0ef41Sopenharmony_ci name: 'node', 27431cb0ef41Sopenharmony_ci lts: 'Hydrogen', 27441cb0ef41Sopenharmony_ci sourceUrl: 'https://nodejs.org/download/release/v18.12.0/node-v18.12.0.tar.gz', 27451cb0ef41Sopenharmony_ci headersUrl: 'https://nodejs.org/download/release/v18.12.0/node-v18.12.0-headers.tar.gz', 27461cb0ef41Sopenharmony_ci libUrl: 'https://nodejs.org/download/release/v18.12.0/win-x64/node.lib' 27471cb0ef41Sopenharmony_ci} 27481cb0ef41Sopenharmony_ci``` 27491cb0ef41Sopenharmony_ci 27501cb0ef41Sopenharmony_ciIn custom builds from non-release versions of the source tree, only the 27511cb0ef41Sopenharmony_ci`name` property may be present. The additional properties should not be 27521cb0ef41Sopenharmony_cirelied upon to exist. 27531cb0ef41Sopenharmony_ci 27541cb0ef41Sopenharmony_ci## `process.report` 27551cb0ef41Sopenharmony_ci 27561cb0ef41Sopenharmony_ci<!-- YAML 27571cb0ef41Sopenharmony_ciadded: v11.8.0 27581cb0ef41Sopenharmony_cichanges: 27591cb0ef41Sopenharmony_ci - version: 27601cb0ef41Sopenharmony_ci - v13.12.0 27611cb0ef41Sopenharmony_ci - v12.17.0 27621cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/32242 27631cb0ef41Sopenharmony_ci description: This API is no longer experimental. 27641cb0ef41Sopenharmony_ci--> 27651cb0ef41Sopenharmony_ci 27661cb0ef41Sopenharmony_ci* {Object} 27671cb0ef41Sopenharmony_ci 27681cb0ef41Sopenharmony_ci`process.report` is an object whose methods are used to generate diagnostic 27691cb0ef41Sopenharmony_cireports for the current process. Additional documentation is available in the 27701cb0ef41Sopenharmony_ci[report documentation][]. 27711cb0ef41Sopenharmony_ci 27721cb0ef41Sopenharmony_ci### `process.report.compact` 27731cb0ef41Sopenharmony_ci 27741cb0ef41Sopenharmony_ci<!-- YAML 27751cb0ef41Sopenharmony_ciadded: 27761cb0ef41Sopenharmony_ci - v13.12.0 27771cb0ef41Sopenharmony_ci - v12.17.0 27781cb0ef41Sopenharmony_ci--> 27791cb0ef41Sopenharmony_ci 27801cb0ef41Sopenharmony_ci* {boolean} 27811cb0ef41Sopenharmony_ci 27821cb0ef41Sopenharmony_ciWrite reports in a compact format, single-line JSON, more easily consumable 27831cb0ef41Sopenharmony_ciby log processing systems than the default multi-line format designed for 27841cb0ef41Sopenharmony_cihuman consumption. 27851cb0ef41Sopenharmony_ci 27861cb0ef41Sopenharmony_ci```mjs 27871cb0ef41Sopenharmony_ciimport { report } from 'node:process'; 27881cb0ef41Sopenharmony_ci 27891cb0ef41Sopenharmony_ciconsole.log(`Reports are compact? ${report.compact}`); 27901cb0ef41Sopenharmony_ci``` 27911cb0ef41Sopenharmony_ci 27921cb0ef41Sopenharmony_ci```cjs 27931cb0ef41Sopenharmony_ciconst { report } = require('node:process'); 27941cb0ef41Sopenharmony_ci 27951cb0ef41Sopenharmony_ciconsole.log(`Reports are compact? ${report.compact}`); 27961cb0ef41Sopenharmony_ci``` 27971cb0ef41Sopenharmony_ci 27981cb0ef41Sopenharmony_ci### `process.report.directory` 27991cb0ef41Sopenharmony_ci 28001cb0ef41Sopenharmony_ci<!-- YAML 28011cb0ef41Sopenharmony_ciadded: v11.12.0 28021cb0ef41Sopenharmony_cichanges: 28031cb0ef41Sopenharmony_ci - version: 28041cb0ef41Sopenharmony_ci - v13.12.0 28051cb0ef41Sopenharmony_ci - v12.17.0 28061cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/32242 28071cb0ef41Sopenharmony_ci description: This API is no longer experimental. 28081cb0ef41Sopenharmony_ci--> 28091cb0ef41Sopenharmony_ci 28101cb0ef41Sopenharmony_ci* {string} 28111cb0ef41Sopenharmony_ci 28121cb0ef41Sopenharmony_ciDirectory where the report is written. The default value is the empty string, 28131cb0ef41Sopenharmony_ciindicating that reports are written to the current working directory of the 28141cb0ef41Sopenharmony_ciNode.js process. 28151cb0ef41Sopenharmony_ci 28161cb0ef41Sopenharmony_ci```mjs 28171cb0ef41Sopenharmony_ciimport { report } from 'node:process'; 28181cb0ef41Sopenharmony_ci 28191cb0ef41Sopenharmony_ciconsole.log(`Report directory is ${report.directory}`); 28201cb0ef41Sopenharmony_ci``` 28211cb0ef41Sopenharmony_ci 28221cb0ef41Sopenharmony_ci```cjs 28231cb0ef41Sopenharmony_ciconst { report } = require('node:process'); 28241cb0ef41Sopenharmony_ci 28251cb0ef41Sopenharmony_ciconsole.log(`Report directory is ${report.directory}`); 28261cb0ef41Sopenharmony_ci``` 28271cb0ef41Sopenharmony_ci 28281cb0ef41Sopenharmony_ci### `process.report.filename` 28291cb0ef41Sopenharmony_ci 28301cb0ef41Sopenharmony_ci<!-- YAML 28311cb0ef41Sopenharmony_ciadded: v11.12.0 28321cb0ef41Sopenharmony_cichanges: 28331cb0ef41Sopenharmony_ci - version: 28341cb0ef41Sopenharmony_ci - v13.12.0 28351cb0ef41Sopenharmony_ci - v12.17.0 28361cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/32242 28371cb0ef41Sopenharmony_ci description: This API is no longer experimental. 28381cb0ef41Sopenharmony_ci--> 28391cb0ef41Sopenharmony_ci 28401cb0ef41Sopenharmony_ci* {string} 28411cb0ef41Sopenharmony_ci 28421cb0ef41Sopenharmony_ciFilename where the report is written. If set to the empty string, the output 28431cb0ef41Sopenharmony_cifilename will be comprised of a timestamp, PID, and sequence number. The default 28441cb0ef41Sopenharmony_civalue is the empty string. 28451cb0ef41Sopenharmony_ci 28461cb0ef41Sopenharmony_ciIf the value of `process.report.filename` is set to `'stdout'` or `'stderr'`, 28471cb0ef41Sopenharmony_cithe report is written to the stdout or stderr of the process respectively. 28481cb0ef41Sopenharmony_ci 28491cb0ef41Sopenharmony_ci```mjs 28501cb0ef41Sopenharmony_ciimport { report } from 'node:process'; 28511cb0ef41Sopenharmony_ci 28521cb0ef41Sopenharmony_ciconsole.log(`Report filename is ${report.filename}`); 28531cb0ef41Sopenharmony_ci``` 28541cb0ef41Sopenharmony_ci 28551cb0ef41Sopenharmony_ci```cjs 28561cb0ef41Sopenharmony_ciconst { report } = require('node:process'); 28571cb0ef41Sopenharmony_ci 28581cb0ef41Sopenharmony_ciconsole.log(`Report filename is ${report.filename}`); 28591cb0ef41Sopenharmony_ci``` 28601cb0ef41Sopenharmony_ci 28611cb0ef41Sopenharmony_ci### `process.report.getReport([err])` 28621cb0ef41Sopenharmony_ci 28631cb0ef41Sopenharmony_ci<!-- YAML 28641cb0ef41Sopenharmony_ciadded: v11.8.0 28651cb0ef41Sopenharmony_cichanges: 28661cb0ef41Sopenharmony_ci - version: 28671cb0ef41Sopenharmony_ci - v13.12.0 28681cb0ef41Sopenharmony_ci - v12.17.0 28691cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/32242 28701cb0ef41Sopenharmony_ci description: This API is no longer experimental. 28711cb0ef41Sopenharmony_ci--> 28721cb0ef41Sopenharmony_ci 28731cb0ef41Sopenharmony_ci* `err` {Error} A custom error used for reporting the JavaScript stack. 28741cb0ef41Sopenharmony_ci* Returns: {Object} 28751cb0ef41Sopenharmony_ci 28761cb0ef41Sopenharmony_ciReturns a JavaScript Object representation of a diagnostic report for the 28771cb0ef41Sopenharmony_cirunning process. The report's JavaScript stack trace is taken from `err`, if 28781cb0ef41Sopenharmony_cipresent. 28791cb0ef41Sopenharmony_ci 28801cb0ef41Sopenharmony_ci```mjs 28811cb0ef41Sopenharmony_ciimport { report } from 'node:process'; 28821cb0ef41Sopenharmony_ciimport util from 'node:util'; 28831cb0ef41Sopenharmony_ci 28841cb0ef41Sopenharmony_ciconst data = report.getReport(); 28851cb0ef41Sopenharmony_ciconsole.log(data.header.nodejsVersion); 28861cb0ef41Sopenharmony_ci 28871cb0ef41Sopenharmony_ci// Similar to process.report.writeReport() 28881cb0ef41Sopenharmony_ciimport fs from 'node:fs'; 28891cb0ef41Sopenharmony_cifs.writeFileSync('my-report.log', util.inspect(data), 'utf8'); 28901cb0ef41Sopenharmony_ci``` 28911cb0ef41Sopenharmony_ci 28921cb0ef41Sopenharmony_ci```cjs 28931cb0ef41Sopenharmony_ciconst { report } = require('node:process'); 28941cb0ef41Sopenharmony_ciconst util = require('node:util'); 28951cb0ef41Sopenharmony_ci 28961cb0ef41Sopenharmony_ciconst data = report.getReport(); 28971cb0ef41Sopenharmony_ciconsole.log(data.header.nodejsVersion); 28981cb0ef41Sopenharmony_ci 28991cb0ef41Sopenharmony_ci// Similar to process.report.writeReport() 29001cb0ef41Sopenharmony_ciconst fs = require('node:fs'); 29011cb0ef41Sopenharmony_cifs.writeFileSync('my-report.log', util.inspect(data), 'utf8'); 29021cb0ef41Sopenharmony_ci``` 29031cb0ef41Sopenharmony_ci 29041cb0ef41Sopenharmony_ciAdditional documentation is available in the [report documentation][]. 29051cb0ef41Sopenharmony_ci 29061cb0ef41Sopenharmony_ci### `process.report.reportOnFatalError` 29071cb0ef41Sopenharmony_ci 29081cb0ef41Sopenharmony_ci<!-- YAML 29091cb0ef41Sopenharmony_ciadded: v11.12.0 29101cb0ef41Sopenharmony_cichanges: 29111cb0ef41Sopenharmony_ci - version: 29121cb0ef41Sopenharmony_ci - v15.0.0 29131cb0ef41Sopenharmony_ci - v14.17.0 29141cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/35654 29151cb0ef41Sopenharmony_ci description: This API is no longer experimental. 29161cb0ef41Sopenharmony_ci--> 29171cb0ef41Sopenharmony_ci 29181cb0ef41Sopenharmony_ci* {boolean} 29191cb0ef41Sopenharmony_ci 29201cb0ef41Sopenharmony_ciIf `true`, a diagnostic report is generated on fatal errors, such as out of 29211cb0ef41Sopenharmony_cimemory errors or failed C++ assertions. 29221cb0ef41Sopenharmony_ci 29231cb0ef41Sopenharmony_ci```mjs 29241cb0ef41Sopenharmony_ciimport { report } from 'node:process'; 29251cb0ef41Sopenharmony_ci 29261cb0ef41Sopenharmony_ciconsole.log(`Report on fatal error: ${report.reportOnFatalError}`); 29271cb0ef41Sopenharmony_ci``` 29281cb0ef41Sopenharmony_ci 29291cb0ef41Sopenharmony_ci```cjs 29301cb0ef41Sopenharmony_ciconst { report } = require('node:process'); 29311cb0ef41Sopenharmony_ci 29321cb0ef41Sopenharmony_ciconsole.log(`Report on fatal error: ${report.reportOnFatalError}`); 29331cb0ef41Sopenharmony_ci``` 29341cb0ef41Sopenharmony_ci 29351cb0ef41Sopenharmony_ci### `process.report.reportOnSignal` 29361cb0ef41Sopenharmony_ci 29371cb0ef41Sopenharmony_ci<!-- YAML 29381cb0ef41Sopenharmony_ciadded: v11.12.0 29391cb0ef41Sopenharmony_cichanges: 29401cb0ef41Sopenharmony_ci - version: 29411cb0ef41Sopenharmony_ci - v13.12.0 29421cb0ef41Sopenharmony_ci - v12.17.0 29431cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/32242 29441cb0ef41Sopenharmony_ci description: This API is no longer experimental. 29451cb0ef41Sopenharmony_ci--> 29461cb0ef41Sopenharmony_ci 29471cb0ef41Sopenharmony_ci* {boolean} 29481cb0ef41Sopenharmony_ci 29491cb0ef41Sopenharmony_ciIf `true`, a diagnostic report is generated when the process receives the 29501cb0ef41Sopenharmony_cisignal specified by `process.report.signal`. 29511cb0ef41Sopenharmony_ci 29521cb0ef41Sopenharmony_ci```mjs 29531cb0ef41Sopenharmony_ciimport { report } from 'node:process'; 29541cb0ef41Sopenharmony_ci 29551cb0ef41Sopenharmony_ciconsole.log(`Report on signal: ${report.reportOnSignal}`); 29561cb0ef41Sopenharmony_ci``` 29571cb0ef41Sopenharmony_ci 29581cb0ef41Sopenharmony_ci```cjs 29591cb0ef41Sopenharmony_ciconst { report } = require('node:process'); 29601cb0ef41Sopenharmony_ci 29611cb0ef41Sopenharmony_ciconsole.log(`Report on signal: ${report.reportOnSignal}`); 29621cb0ef41Sopenharmony_ci``` 29631cb0ef41Sopenharmony_ci 29641cb0ef41Sopenharmony_ci### `process.report.reportOnUncaughtException` 29651cb0ef41Sopenharmony_ci 29661cb0ef41Sopenharmony_ci<!-- YAML 29671cb0ef41Sopenharmony_ciadded: v11.12.0 29681cb0ef41Sopenharmony_cichanges: 29691cb0ef41Sopenharmony_ci - version: 29701cb0ef41Sopenharmony_ci - v13.12.0 29711cb0ef41Sopenharmony_ci - v12.17.0 29721cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/32242 29731cb0ef41Sopenharmony_ci description: This API is no longer experimental. 29741cb0ef41Sopenharmony_ci--> 29751cb0ef41Sopenharmony_ci 29761cb0ef41Sopenharmony_ci* {boolean} 29771cb0ef41Sopenharmony_ci 29781cb0ef41Sopenharmony_ciIf `true`, a diagnostic report is generated on uncaught exception. 29791cb0ef41Sopenharmony_ci 29801cb0ef41Sopenharmony_ci```mjs 29811cb0ef41Sopenharmony_ciimport { report } from 'node:process'; 29821cb0ef41Sopenharmony_ci 29831cb0ef41Sopenharmony_ciconsole.log(`Report on exception: ${report.reportOnUncaughtException}`); 29841cb0ef41Sopenharmony_ci``` 29851cb0ef41Sopenharmony_ci 29861cb0ef41Sopenharmony_ci```cjs 29871cb0ef41Sopenharmony_ciconst { report } = require('node:process'); 29881cb0ef41Sopenharmony_ci 29891cb0ef41Sopenharmony_ciconsole.log(`Report on exception: ${report.reportOnUncaughtException}`); 29901cb0ef41Sopenharmony_ci``` 29911cb0ef41Sopenharmony_ci 29921cb0ef41Sopenharmony_ci### `process.report.signal` 29931cb0ef41Sopenharmony_ci 29941cb0ef41Sopenharmony_ci<!-- YAML 29951cb0ef41Sopenharmony_ciadded: v11.12.0 29961cb0ef41Sopenharmony_cichanges: 29971cb0ef41Sopenharmony_ci - version: 29981cb0ef41Sopenharmony_ci - v13.12.0 29991cb0ef41Sopenharmony_ci - v12.17.0 30001cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/32242 30011cb0ef41Sopenharmony_ci description: This API is no longer experimental. 30021cb0ef41Sopenharmony_ci--> 30031cb0ef41Sopenharmony_ci 30041cb0ef41Sopenharmony_ci* {string} 30051cb0ef41Sopenharmony_ci 30061cb0ef41Sopenharmony_ciThe signal used to trigger the creation of a diagnostic report. Defaults to 30071cb0ef41Sopenharmony_ci`'SIGUSR2'`. 30081cb0ef41Sopenharmony_ci 30091cb0ef41Sopenharmony_ci```mjs 30101cb0ef41Sopenharmony_ciimport { report } from 'node:process'; 30111cb0ef41Sopenharmony_ci 30121cb0ef41Sopenharmony_ciconsole.log(`Report signal: ${report.signal}`); 30131cb0ef41Sopenharmony_ci``` 30141cb0ef41Sopenharmony_ci 30151cb0ef41Sopenharmony_ci```cjs 30161cb0ef41Sopenharmony_ciconst { report } = require('node:process'); 30171cb0ef41Sopenharmony_ci 30181cb0ef41Sopenharmony_ciconsole.log(`Report signal: ${report.signal}`); 30191cb0ef41Sopenharmony_ci``` 30201cb0ef41Sopenharmony_ci 30211cb0ef41Sopenharmony_ci### `process.report.writeReport([filename][, err])` 30221cb0ef41Sopenharmony_ci 30231cb0ef41Sopenharmony_ci<!-- YAML 30241cb0ef41Sopenharmony_ciadded: v11.8.0 30251cb0ef41Sopenharmony_cichanges: 30261cb0ef41Sopenharmony_ci - version: 30271cb0ef41Sopenharmony_ci - v13.12.0 30281cb0ef41Sopenharmony_ci - v12.17.0 30291cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/32242 30301cb0ef41Sopenharmony_ci description: This API is no longer experimental. 30311cb0ef41Sopenharmony_ci--> 30321cb0ef41Sopenharmony_ci 30331cb0ef41Sopenharmony_ci* `filename` {string} Name of the file where the report is written. This 30341cb0ef41Sopenharmony_ci should be a relative path, that will be appended to the directory specified in 30351cb0ef41Sopenharmony_ci `process.report.directory`, or the current working directory of the Node.js 30361cb0ef41Sopenharmony_ci process, if unspecified. 30371cb0ef41Sopenharmony_ci 30381cb0ef41Sopenharmony_ci* `err` {Error} A custom error used for reporting the JavaScript stack. 30391cb0ef41Sopenharmony_ci 30401cb0ef41Sopenharmony_ci* Returns: {string} Returns the filename of the generated report. 30411cb0ef41Sopenharmony_ci 30421cb0ef41Sopenharmony_ciWrites a diagnostic report to a file. If `filename` is not provided, the default 30431cb0ef41Sopenharmony_cifilename includes the date, time, PID, and a sequence number. The report's 30441cb0ef41Sopenharmony_ciJavaScript stack trace is taken from `err`, if present. 30451cb0ef41Sopenharmony_ci 30461cb0ef41Sopenharmony_ciIf the value of `filename` is set to `'stdout'` or `'stderr'`, the report is 30471cb0ef41Sopenharmony_ciwritten to the stdout or stderr of the process respectively. 30481cb0ef41Sopenharmony_ci 30491cb0ef41Sopenharmony_ci```mjs 30501cb0ef41Sopenharmony_ciimport { report } from 'node:process'; 30511cb0ef41Sopenharmony_ci 30521cb0ef41Sopenharmony_cireport.writeReport(); 30531cb0ef41Sopenharmony_ci``` 30541cb0ef41Sopenharmony_ci 30551cb0ef41Sopenharmony_ci```cjs 30561cb0ef41Sopenharmony_ciconst { report } = require('node:process'); 30571cb0ef41Sopenharmony_ci 30581cb0ef41Sopenharmony_cireport.writeReport(); 30591cb0ef41Sopenharmony_ci``` 30601cb0ef41Sopenharmony_ci 30611cb0ef41Sopenharmony_ciAdditional documentation is available in the [report documentation][]. 30621cb0ef41Sopenharmony_ci 30631cb0ef41Sopenharmony_ci## `process.resourceUsage()` 30641cb0ef41Sopenharmony_ci 30651cb0ef41Sopenharmony_ci<!-- YAML 30661cb0ef41Sopenharmony_ciadded: v12.6.0 30671cb0ef41Sopenharmony_ci--> 30681cb0ef41Sopenharmony_ci 30691cb0ef41Sopenharmony_ci* Returns: {Object} the resource usage for the current process. All of these 30701cb0ef41Sopenharmony_ci values come from the `uv_getrusage` call which returns 30711cb0ef41Sopenharmony_ci a [`uv_rusage_t` struct][uv_rusage_t]. 30721cb0ef41Sopenharmony_ci * `userCPUTime` {integer} maps to `ru_utime` computed in microseconds. 30731cb0ef41Sopenharmony_ci It is the same value as [`process.cpuUsage().user`][process.cpuUsage]. 30741cb0ef41Sopenharmony_ci * `systemCPUTime` {integer} maps to `ru_stime` computed in microseconds. 30751cb0ef41Sopenharmony_ci It is the same value as [`process.cpuUsage().system`][process.cpuUsage]. 30761cb0ef41Sopenharmony_ci * `maxRSS` {integer} maps to `ru_maxrss` which is the maximum resident set 30771cb0ef41Sopenharmony_ci size used in kilobytes. 30781cb0ef41Sopenharmony_ci * `sharedMemorySize` {integer} maps to `ru_ixrss` but is not supported by 30791cb0ef41Sopenharmony_ci any platform. 30801cb0ef41Sopenharmony_ci * `unsharedDataSize` {integer} maps to `ru_idrss` but is not supported by 30811cb0ef41Sopenharmony_ci any platform. 30821cb0ef41Sopenharmony_ci * `unsharedStackSize` {integer} maps to `ru_isrss` but is not supported by 30831cb0ef41Sopenharmony_ci any platform. 30841cb0ef41Sopenharmony_ci * `minorPageFault` {integer} maps to `ru_minflt` which is the number of 30851cb0ef41Sopenharmony_ci minor page faults for the process, see 30861cb0ef41Sopenharmony_ci [this article for more details][wikipedia_minor_fault]. 30871cb0ef41Sopenharmony_ci * `majorPageFault` {integer} maps to `ru_majflt` which is the number of 30881cb0ef41Sopenharmony_ci major page faults for the process, see 30891cb0ef41Sopenharmony_ci [this article for more details][wikipedia_major_fault]. This field is not 30901cb0ef41Sopenharmony_ci supported on Windows. 30911cb0ef41Sopenharmony_ci * `swappedOut` {integer} maps to `ru_nswap` but is not supported by any 30921cb0ef41Sopenharmony_ci platform. 30931cb0ef41Sopenharmony_ci * `fsRead` {integer} maps to `ru_inblock` which is the number of times the 30941cb0ef41Sopenharmony_ci file system had to perform input. 30951cb0ef41Sopenharmony_ci * `fsWrite` {integer} maps to `ru_oublock` which is the number of times the 30961cb0ef41Sopenharmony_ci file system had to perform output. 30971cb0ef41Sopenharmony_ci * `ipcSent` {integer} maps to `ru_msgsnd` but is not supported by any 30981cb0ef41Sopenharmony_ci platform. 30991cb0ef41Sopenharmony_ci * `ipcReceived` {integer} maps to `ru_msgrcv` but is not supported by any 31001cb0ef41Sopenharmony_ci platform. 31011cb0ef41Sopenharmony_ci * `signalsCount` {integer} maps to `ru_nsignals` but is not supported by any 31021cb0ef41Sopenharmony_ci platform. 31031cb0ef41Sopenharmony_ci * `voluntaryContextSwitches` {integer} maps to `ru_nvcsw` which is the 31041cb0ef41Sopenharmony_ci number of times a CPU context switch resulted due to a process voluntarily 31051cb0ef41Sopenharmony_ci giving up the processor before its time slice was completed (usually to 31061cb0ef41Sopenharmony_ci await availability of a resource). This field is not supported on Windows. 31071cb0ef41Sopenharmony_ci * `involuntaryContextSwitches` {integer} maps to `ru_nivcsw` which is the 31081cb0ef41Sopenharmony_ci number of times a CPU context switch resulted due to a higher priority 31091cb0ef41Sopenharmony_ci process becoming runnable or because the current process exceeded its 31101cb0ef41Sopenharmony_ci time slice. This field is not supported on Windows. 31111cb0ef41Sopenharmony_ci 31121cb0ef41Sopenharmony_ci```mjs 31131cb0ef41Sopenharmony_ciimport { resourceUsage } from 'node:process'; 31141cb0ef41Sopenharmony_ci 31151cb0ef41Sopenharmony_ciconsole.log(resourceUsage()); 31161cb0ef41Sopenharmony_ci/* 31171cb0ef41Sopenharmony_ci Will output: 31181cb0ef41Sopenharmony_ci { 31191cb0ef41Sopenharmony_ci userCPUTime: 82872, 31201cb0ef41Sopenharmony_ci systemCPUTime: 4143, 31211cb0ef41Sopenharmony_ci maxRSS: 33164, 31221cb0ef41Sopenharmony_ci sharedMemorySize: 0, 31231cb0ef41Sopenharmony_ci unsharedDataSize: 0, 31241cb0ef41Sopenharmony_ci unsharedStackSize: 0, 31251cb0ef41Sopenharmony_ci minorPageFault: 2469, 31261cb0ef41Sopenharmony_ci majorPageFault: 0, 31271cb0ef41Sopenharmony_ci swappedOut: 0, 31281cb0ef41Sopenharmony_ci fsRead: 0, 31291cb0ef41Sopenharmony_ci fsWrite: 8, 31301cb0ef41Sopenharmony_ci ipcSent: 0, 31311cb0ef41Sopenharmony_ci ipcReceived: 0, 31321cb0ef41Sopenharmony_ci signalsCount: 0, 31331cb0ef41Sopenharmony_ci voluntaryContextSwitches: 79, 31341cb0ef41Sopenharmony_ci involuntaryContextSwitches: 1 31351cb0ef41Sopenharmony_ci } 31361cb0ef41Sopenharmony_ci*/ 31371cb0ef41Sopenharmony_ci``` 31381cb0ef41Sopenharmony_ci 31391cb0ef41Sopenharmony_ci```cjs 31401cb0ef41Sopenharmony_ciconst { resourceUsage } = require('node:process'); 31411cb0ef41Sopenharmony_ci 31421cb0ef41Sopenharmony_ciconsole.log(resourceUsage()); 31431cb0ef41Sopenharmony_ci/* 31441cb0ef41Sopenharmony_ci Will output: 31451cb0ef41Sopenharmony_ci { 31461cb0ef41Sopenharmony_ci userCPUTime: 82872, 31471cb0ef41Sopenharmony_ci systemCPUTime: 4143, 31481cb0ef41Sopenharmony_ci maxRSS: 33164, 31491cb0ef41Sopenharmony_ci sharedMemorySize: 0, 31501cb0ef41Sopenharmony_ci unsharedDataSize: 0, 31511cb0ef41Sopenharmony_ci unsharedStackSize: 0, 31521cb0ef41Sopenharmony_ci minorPageFault: 2469, 31531cb0ef41Sopenharmony_ci majorPageFault: 0, 31541cb0ef41Sopenharmony_ci swappedOut: 0, 31551cb0ef41Sopenharmony_ci fsRead: 0, 31561cb0ef41Sopenharmony_ci fsWrite: 8, 31571cb0ef41Sopenharmony_ci ipcSent: 0, 31581cb0ef41Sopenharmony_ci ipcReceived: 0, 31591cb0ef41Sopenharmony_ci signalsCount: 0, 31601cb0ef41Sopenharmony_ci voluntaryContextSwitches: 79, 31611cb0ef41Sopenharmony_ci involuntaryContextSwitches: 1 31621cb0ef41Sopenharmony_ci } 31631cb0ef41Sopenharmony_ci*/ 31641cb0ef41Sopenharmony_ci``` 31651cb0ef41Sopenharmony_ci 31661cb0ef41Sopenharmony_ci## `process.send(message[, sendHandle[, options]][, callback])` 31671cb0ef41Sopenharmony_ci 31681cb0ef41Sopenharmony_ci<!-- YAML 31691cb0ef41Sopenharmony_ciadded: v0.5.9 31701cb0ef41Sopenharmony_ci--> 31711cb0ef41Sopenharmony_ci 31721cb0ef41Sopenharmony_ci* `message` {Object} 31731cb0ef41Sopenharmony_ci* `sendHandle` {net.Server|net.Socket} 31741cb0ef41Sopenharmony_ci* `options` {Object} used to parameterize the sending of certain types of 31751cb0ef41Sopenharmony_ci handles.`options` supports the following properties: 31761cb0ef41Sopenharmony_ci * `keepOpen` {boolean} A value that can be used when passing instances of 31771cb0ef41Sopenharmony_ci `net.Socket`. When `true`, the socket is kept open in the sending process. 31781cb0ef41Sopenharmony_ci **Default:** `false`. 31791cb0ef41Sopenharmony_ci* `callback` {Function} 31801cb0ef41Sopenharmony_ci* Returns: {boolean} 31811cb0ef41Sopenharmony_ci 31821cb0ef41Sopenharmony_ciIf Node.js is spawned with an IPC channel, the `process.send()` method can be 31831cb0ef41Sopenharmony_ciused to send messages to the parent process. Messages will be received as a 31841cb0ef41Sopenharmony_ci[`'message'`][] event on the parent's [`ChildProcess`][] object. 31851cb0ef41Sopenharmony_ci 31861cb0ef41Sopenharmony_ciIf Node.js was not spawned with an IPC channel, `process.send` will be 31871cb0ef41Sopenharmony_ci`undefined`. 31881cb0ef41Sopenharmony_ci 31891cb0ef41Sopenharmony_ciThe message goes through serialization and parsing. The resulting message might 31901cb0ef41Sopenharmony_cinot be the same as what is originally sent. 31911cb0ef41Sopenharmony_ci 31921cb0ef41Sopenharmony_ci## `process.setegid(id)` 31931cb0ef41Sopenharmony_ci 31941cb0ef41Sopenharmony_ci<!-- YAML 31951cb0ef41Sopenharmony_ciadded: v2.0.0 31961cb0ef41Sopenharmony_ci--> 31971cb0ef41Sopenharmony_ci 31981cb0ef41Sopenharmony_ci* `id` {string|number} A group name or ID 31991cb0ef41Sopenharmony_ci 32001cb0ef41Sopenharmony_ciThe `process.setegid()` method sets the effective group identity of the process. 32011cb0ef41Sopenharmony_ci(See setegid(2).) The `id` can be passed as either a numeric ID or a group 32021cb0ef41Sopenharmony_ciname string. If a group name is specified, this method blocks while resolving 32031cb0ef41Sopenharmony_cithe associated a numeric ID. 32041cb0ef41Sopenharmony_ci 32051cb0ef41Sopenharmony_ci```mjs 32061cb0ef41Sopenharmony_ciimport process from 'node:process'; 32071cb0ef41Sopenharmony_ci 32081cb0ef41Sopenharmony_ciif (process.getegid && process.setegid) { 32091cb0ef41Sopenharmony_ci console.log(`Current gid: ${process.getegid()}`); 32101cb0ef41Sopenharmony_ci try { 32111cb0ef41Sopenharmony_ci process.setegid(501); 32121cb0ef41Sopenharmony_ci console.log(`New gid: ${process.getegid()}`); 32131cb0ef41Sopenharmony_ci } catch (err) { 32141cb0ef41Sopenharmony_ci console.error(`Failed to set gid: ${err}`); 32151cb0ef41Sopenharmony_ci } 32161cb0ef41Sopenharmony_ci} 32171cb0ef41Sopenharmony_ci``` 32181cb0ef41Sopenharmony_ci 32191cb0ef41Sopenharmony_ci```cjs 32201cb0ef41Sopenharmony_ciconst process = require('node:process'); 32211cb0ef41Sopenharmony_ci 32221cb0ef41Sopenharmony_ciif (process.getegid && process.setegid) { 32231cb0ef41Sopenharmony_ci console.log(`Current gid: ${process.getegid()}`); 32241cb0ef41Sopenharmony_ci try { 32251cb0ef41Sopenharmony_ci process.setegid(501); 32261cb0ef41Sopenharmony_ci console.log(`New gid: ${process.getegid()}`); 32271cb0ef41Sopenharmony_ci } catch (err) { 32281cb0ef41Sopenharmony_ci console.error(`Failed to set gid: ${err}`); 32291cb0ef41Sopenharmony_ci } 32301cb0ef41Sopenharmony_ci} 32311cb0ef41Sopenharmony_ci``` 32321cb0ef41Sopenharmony_ci 32331cb0ef41Sopenharmony_ciThis function is only available on POSIX platforms (i.e. not Windows or 32341cb0ef41Sopenharmony_ciAndroid). 32351cb0ef41Sopenharmony_ciThis feature is not available in [`Worker`][] threads. 32361cb0ef41Sopenharmony_ci 32371cb0ef41Sopenharmony_ci## `process.seteuid(id)` 32381cb0ef41Sopenharmony_ci 32391cb0ef41Sopenharmony_ci<!-- YAML 32401cb0ef41Sopenharmony_ciadded: v2.0.0 32411cb0ef41Sopenharmony_ci--> 32421cb0ef41Sopenharmony_ci 32431cb0ef41Sopenharmony_ci* `id` {string|number} A user name or ID 32441cb0ef41Sopenharmony_ci 32451cb0ef41Sopenharmony_ciThe `process.seteuid()` method sets the effective user identity of the process. 32461cb0ef41Sopenharmony_ci(See seteuid(2).) The `id` can be passed as either a numeric ID or a username 32471cb0ef41Sopenharmony_cistring. If a username is specified, the method blocks while resolving the 32481cb0ef41Sopenharmony_ciassociated numeric ID. 32491cb0ef41Sopenharmony_ci 32501cb0ef41Sopenharmony_ci```mjs 32511cb0ef41Sopenharmony_ciimport process from 'node:process'; 32521cb0ef41Sopenharmony_ci 32531cb0ef41Sopenharmony_ciif (process.geteuid && process.seteuid) { 32541cb0ef41Sopenharmony_ci console.log(`Current uid: ${process.geteuid()}`); 32551cb0ef41Sopenharmony_ci try { 32561cb0ef41Sopenharmony_ci process.seteuid(501); 32571cb0ef41Sopenharmony_ci console.log(`New uid: ${process.geteuid()}`); 32581cb0ef41Sopenharmony_ci } catch (err) { 32591cb0ef41Sopenharmony_ci console.error(`Failed to set uid: ${err}`); 32601cb0ef41Sopenharmony_ci } 32611cb0ef41Sopenharmony_ci} 32621cb0ef41Sopenharmony_ci``` 32631cb0ef41Sopenharmony_ci 32641cb0ef41Sopenharmony_ci```cjs 32651cb0ef41Sopenharmony_ciconst process = require('node:process'); 32661cb0ef41Sopenharmony_ci 32671cb0ef41Sopenharmony_ciif (process.geteuid && process.seteuid) { 32681cb0ef41Sopenharmony_ci console.log(`Current uid: ${process.geteuid()}`); 32691cb0ef41Sopenharmony_ci try { 32701cb0ef41Sopenharmony_ci process.seteuid(501); 32711cb0ef41Sopenharmony_ci console.log(`New uid: ${process.geteuid()}`); 32721cb0ef41Sopenharmony_ci } catch (err) { 32731cb0ef41Sopenharmony_ci console.error(`Failed to set uid: ${err}`); 32741cb0ef41Sopenharmony_ci } 32751cb0ef41Sopenharmony_ci} 32761cb0ef41Sopenharmony_ci``` 32771cb0ef41Sopenharmony_ci 32781cb0ef41Sopenharmony_ciThis function is only available on POSIX platforms (i.e. not Windows or 32791cb0ef41Sopenharmony_ciAndroid). 32801cb0ef41Sopenharmony_ciThis feature is not available in [`Worker`][] threads. 32811cb0ef41Sopenharmony_ci 32821cb0ef41Sopenharmony_ci## `process.setgid(id)` 32831cb0ef41Sopenharmony_ci 32841cb0ef41Sopenharmony_ci<!-- YAML 32851cb0ef41Sopenharmony_ciadded: v0.1.31 32861cb0ef41Sopenharmony_ci--> 32871cb0ef41Sopenharmony_ci 32881cb0ef41Sopenharmony_ci* `id` {string|number} The group name or ID 32891cb0ef41Sopenharmony_ci 32901cb0ef41Sopenharmony_ciThe `process.setgid()` method sets the group identity of the process. (See 32911cb0ef41Sopenharmony_cisetgid(2).) The `id` can be passed as either a numeric ID or a group name 32921cb0ef41Sopenharmony_cistring. If a group name is specified, this method blocks while resolving the 32931cb0ef41Sopenharmony_ciassociated numeric ID. 32941cb0ef41Sopenharmony_ci 32951cb0ef41Sopenharmony_ci```mjs 32961cb0ef41Sopenharmony_ciimport process from 'node:process'; 32971cb0ef41Sopenharmony_ci 32981cb0ef41Sopenharmony_ciif (process.getgid && process.setgid) { 32991cb0ef41Sopenharmony_ci console.log(`Current gid: ${process.getgid()}`); 33001cb0ef41Sopenharmony_ci try { 33011cb0ef41Sopenharmony_ci process.setgid(501); 33021cb0ef41Sopenharmony_ci console.log(`New gid: ${process.getgid()}`); 33031cb0ef41Sopenharmony_ci } catch (err) { 33041cb0ef41Sopenharmony_ci console.error(`Failed to set gid: ${err}`); 33051cb0ef41Sopenharmony_ci } 33061cb0ef41Sopenharmony_ci} 33071cb0ef41Sopenharmony_ci``` 33081cb0ef41Sopenharmony_ci 33091cb0ef41Sopenharmony_ci```cjs 33101cb0ef41Sopenharmony_ciconst process = require('node:process'); 33111cb0ef41Sopenharmony_ci 33121cb0ef41Sopenharmony_ciif (process.getgid && process.setgid) { 33131cb0ef41Sopenharmony_ci console.log(`Current gid: ${process.getgid()}`); 33141cb0ef41Sopenharmony_ci try { 33151cb0ef41Sopenharmony_ci process.setgid(501); 33161cb0ef41Sopenharmony_ci console.log(`New gid: ${process.getgid()}`); 33171cb0ef41Sopenharmony_ci } catch (err) { 33181cb0ef41Sopenharmony_ci console.error(`Failed to set gid: ${err}`); 33191cb0ef41Sopenharmony_ci } 33201cb0ef41Sopenharmony_ci} 33211cb0ef41Sopenharmony_ci``` 33221cb0ef41Sopenharmony_ci 33231cb0ef41Sopenharmony_ciThis function is only available on POSIX platforms (i.e. not Windows or 33241cb0ef41Sopenharmony_ciAndroid). 33251cb0ef41Sopenharmony_ciThis feature is not available in [`Worker`][] threads. 33261cb0ef41Sopenharmony_ci 33271cb0ef41Sopenharmony_ci## `process.setgroups(groups)` 33281cb0ef41Sopenharmony_ci 33291cb0ef41Sopenharmony_ci<!-- YAML 33301cb0ef41Sopenharmony_ciadded: v0.9.4 33311cb0ef41Sopenharmony_ci--> 33321cb0ef41Sopenharmony_ci 33331cb0ef41Sopenharmony_ci* `groups` {integer\[]} 33341cb0ef41Sopenharmony_ci 33351cb0ef41Sopenharmony_ciThe `process.setgroups()` method sets the supplementary group IDs for the 33361cb0ef41Sopenharmony_ciNode.js process. This is a privileged operation that requires the Node.js 33371cb0ef41Sopenharmony_ciprocess to have `root` or the `CAP_SETGID` capability. 33381cb0ef41Sopenharmony_ci 33391cb0ef41Sopenharmony_ciThe `groups` array can contain numeric group IDs, group names, or both. 33401cb0ef41Sopenharmony_ci 33411cb0ef41Sopenharmony_ci```mjs 33421cb0ef41Sopenharmony_ciimport process from 'node:process'; 33431cb0ef41Sopenharmony_ci 33441cb0ef41Sopenharmony_ciif (process.getgroups && process.setgroups) { 33451cb0ef41Sopenharmony_ci try { 33461cb0ef41Sopenharmony_ci process.setgroups([501]); 33471cb0ef41Sopenharmony_ci console.log(process.getgroups()); // new groups 33481cb0ef41Sopenharmony_ci } catch (err) { 33491cb0ef41Sopenharmony_ci console.error(`Failed to set groups: ${err}`); 33501cb0ef41Sopenharmony_ci } 33511cb0ef41Sopenharmony_ci} 33521cb0ef41Sopenharmony_ci``` 33531cb0ef41Sopenharmony_ci 33541cb0ef41Sopenharmony_ci```cjs 33551cb0ef41Sopenharmony_ciconst process = require('node:process'); 33561cb0ef41Sopenharmony_ci 33571cb0ef41Sopenharmony_ciif (process.getgroups && process.setgroups) { 33581cb0ef41Sopenharmony_ci try { 33591cb0ef41Sopenharmony_ci process.setgroups([501]); 33601cb0ef41Sopenharmony_ci console.log(process.getgroups()); // new groups 33611cb0ef41Sopenharmony_ci } catch (err) { 33621cb0ef41Sopenharmony_ci console.error(`Failed to set groups: ${err}`); 33631cb0ef41Sopenharmony_ci } 33641cb0ef41Sopenharmony_ci} 33651cb0ef41Sopenharmony_ci``` 33661cb0ef41Sopenharmony_ci 33671cb0ef41Sopenharmony_ciThis function is only available on POSIX platforms (i.e. not Windows or 33681cb0ef41Sopenharmony_ciAndroid). 33691cb0ef41Sopenharmony_ciThis feature is not available in [`Worker`][] threads. 33701cb0ef41Sopenharmony_ci 33711cb0ef41Sopenharmony_ci## `process.setuid(id)` 33721cb0ef41Sopenharmony_ci 33731cb0ef41Sopenharmony_ci<!-- YAML 33741cb0ef41Sopenharmony_ciadded: v0.1.28 33751cb0ef41Sopenharmony_ci--> 33761cb0ef41Sopenharmony_ci 33771cb0ef41Sopenharmony_ci* `id` {integer | string} 33781cb0ef41Sopenharmony_ci 33791cb0ef41Sopenharmony_ciThe `process.setuid(id)` method sets the user identity of the process. (See 33801cb0ef41Sopenharmony_cisetuid(2).) The `id` can be passed as either a numeric ID or a username string. 33811cb0ef41Sopenharmony_ciIf a username is specified, the method blocks while resolving the associated 33821cb0ef41Sopenharmony_cinumeric ID. 33831cb0ef41Sopenharmony_ci 33841cb0ef41Sopenharmony_ci```mjs 33851cb0ef41Sopenharmony_ciimport process from 'node:process'; 33861cb0ef41Sopenharmony_ci 33871cb0ef41Sopenharmony_ciif (process.getuid && process.setuid) { 33881cb0ef41Sopenharmony_ci console.log(`Current uid: ${process.getuid()}`); 33891cb0ef41Sopenharmony_ci try { 33901cb0ef41Sopenharmony_ci process.setuid(501); 33911cb0ef41Sopenharmony_ci console.log(`New uid: ${process.getuid()}`); 33921cb0ef41Sopenharmony_ci } catch (err) { 33931cb0ef41Sopenharmony_ci console.error(`Failed to set uid: ${err}`); 33941cb0ef41Sopenharmony_ci } 33951cb0ef41Sopenharmony_ci} 33961cb0ef41Sopenharmony_ci``` 33971cb0ef41Sopenharmony_ci 33981cb0ef41Sopenharmony_ci```cjs 33991cb0ef41Sopenharmony_ciconst process = require('node:process'); 34001cb0ef41Sopenharmony_ci 34011cb0ef41Sopenharmony_ciif (process.getuid && process.setuid) { 34021cb0ef41Sopenharmony_ci console.log(`Current uid: ${process.getuid()}`); 34031cb0ef41Sopenharmony_ci try { 34041cb0ef41Sopenharmony_ci process.setuid(501); 34051cb0ef41Sopenharmony_ci console.log(`New uid: ${process.getuid()}`); 34061cb0ef41Sopenharmony_ci } catch (err) { 34071cb0ef41Sopenharmony_ci console.error(`Failed to set uid: ${err}`); 34081cb0ef41Sopenharmony_ci } 34091cb0ef41Sopenharmony_ci} 34101cb0ef41Sopenharmony_ci``` 34111cb0ef41Sopenharmony_ci 34121cb0ef41Sopenharmony_ciThis function is only available on POSIX platforms (i.e. not Windows or 34131cb0ef41Sopenharmony_ciAndroid). 34141cb0ef41Sopenharmony_ciThis feature is not available in [`Worker`][] threads. 34151cb0ef41Sopenharmony_ci 34161cb0ef41Sopenharmony_ci## `process.setSourceMapsEnabled(val)` 34171cb0ef41Sopenharmony_ci 34181cb0ef41Sopenharmony_ci<!-- YAML 34191cb0ef41Sopenharmony_ciadded: 34201cb0ef41Sopenharmony_ci - v16.6.0 34211cb0ef41Sopenharmony_ci - v14.18.0 34221cb0ef41Sopenharmony_ci--> 34231cb0ef41Sopenharmony_ci 34241cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 34251cb0ef41Sopenharmony_ci 34261cb0ef41Sopenharmony_ci* `val` {boolean} 34271cb0ef41Sopenharmony_ci 34281cb0ef41Sopenharmony_ciThis function enables or disables the [Source Map v3][Source Map] support for 34291cb0ef41Sopenharmony_cistack traces. 34301cb0ef41Sopenharmony_ci 34311cb0ef41Sopenharmony_ciIt provides same features as launching Node.js process with commandline options 34321cb0ef41Sopenharmony_ci`--enable-source-maps`. 34331cb0ef41Sopenharmony_ci 34341cb0ef41Sopenharmony_ciOnly source maps in JavaScript files that are loaded after source maps has been 34351cb0ef41Sopenharmony_cienabled will be parsed and loaded. 34361cb0ef41Sopenharmony_ci 34371cb0ef41Sopenharmony_ci## `process.setUncaughtExceptionCaptureCallback(fn)` 34381cb0ef41Sopenharmony_ci 34391cb0ef41Sopenharmony_ci<!-- YAML 34401cb0ef41Sopenharmony_ciadded: v9.3.0 34411cb0ef41Sopenharmony_ci--> 34421cb0ef41Sopenharmony_ci 34431cb0ef41Sopenharmony_ci* `fn` {Function|null} 34441cb0ef41Sopenharmony_ci 34451cb0ef41Sopenharmony_ciThe `process.setUncaughtExceptionCaptureCallback()` function sets a function 34461cb0ef41Sopenharmony_cithat will be invoked when an uncaught exception occurs, which will receive the 34471cb0ef41Sopenharmony_ciexception value itself as its first argument. 34481cb0ef41Sopenharmony_ci 34491cb0ef41Sopenharmony_ciIf such a function is set, the [`'uncaughtException'`][] event will 34501cb0ef41Sopenharmony_cinot be emitted. If `--abort-on-uncaught-exception` was passed from the 34511cb0ef41Sopenharmony_cicommand line or set through [`v8.setFlagsFromString()`][], the process will 34521cb0ef41Sopenharmony_cinot abort. Actions configured to take place on exceptions such as report 34531cb0ef41Sopenharmony_cigenerations will be affected too 34541cb0ef41Sopenharmony_ci 34551cb0ef41Sopenharmony_ciTo unset the capture function, 34561cb0ef41Sopenharmony_ci`process.setUncaughtExceptionCaptureCallback(null)` may be used. Calling this 34571cb0ef41Sopenharmony_cimethod with a non-`null` argument while another capture function is set will 34581cb0ef41Sopenharmony_cithrow an error. 34591cb0ef41Sopenharmony_ci 34601cb0ef41Sopenharmony_ciUsing this function is mutually exclusive with using the deprecated 34611cb0ef41Sopenharmony_ci[`domain`][] built-in module. 34621cb0ef41Sopenharmony_ci 34631cb0ef41Sopenharmony_ci## `process.sourceMapsEnabled` 34641cb0ef41Sopenharmony_ci 34651cb0ef41Sopenharmony_ci<!-- YAML 34661cb0ef41Sopenharmony_ciadded: v18.19.0 34671cb0ef41Sopenharmony_ci--> 34681cb0ef41Sopenharmony_ci 34691cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 34701cb0ef41Sopenharmony_ci 34711cb0ef41Sopenharmony_ci* {boolean} 34721cb0ef41Sopenharmony_ci 34731cb0ef41Sopenharmony_ciThe `process.sourceMapsEnabled` property returns whether the 34741cb0ef41Sopenharmony_ci[Source Map v3][Source Map] support for stack traces is enabled. 34751cb0ef41Sopenharmony_ci 34761cb0ef41Sopenharmony_ci## `process.stderr` 34771cb0ef41Sopenharmony_ci 34781cb0ef41Sopenharmony_ci* {Stream} 34791cb0ef41Sopenharmony_ci 34801cb0ef41Sopenharmony_ciThe `process.stderr` property returns a stream connected to 34811cb0ef41Sopenharmony_ci`stderr` (fd `2`). It is a [`net.Socket`][] (which is a [Duplex][] 34821cb0ef41Sopenharmony_cistream) unless fd `2` refers to a file, in which case it is 34831cb0ef41Sopenharmony_cia [Writable][] stream. 34841cb0ef41Sopenharmony_ci 34851cb0ef41Sopenharmony_ci`process.stderr` differs from other Node.js streams in important ways. See 34861cb0ef41Sopenharmony_ci[note on process I/O][] for more information. 34871cb0ef41Sopenharmony_ci 34881cb0ef41Sopenharmony_ci### `process.stderr.fd` 34891cb0ef41Sopenharmony_ci 34901cb0ef41Sopenharmony_ci* {number} 34911cb0ef41Sopenharmony_ci 34921cb0ef41Sopenharmony_ciThis property refers to the value of underlying file descriptor of 34931cb0ef41Sopenharmony_ci`process.stderr`. The value is fixed at `2`. In [`Worker`][] threads, 34941cb0ef41Sopenharmony_cithis field does not exist. 34951cb0ef41Sopenharmony_ci 34961cb0ef41Sopenharmony_ci## `process.stdin` 34971cb0ef41Sopenharmony_ci 34981cb0ef41Sopenharmony_ci* {Stream} 34991cb0ef41Sopenharmony_ci 35001cb0ef41Sopenharmony_ciThe `process.stdin` property returns a stream connected to 35011cb0ef41Sopenharmony_ci`stdin` (fd `0`). It is a [`net.Socket`][] (which is a [Duplex][] 35021cb0ef41Sopenharmony_cistream) unless fd `0` refers to a file, in which case it is 35031cb0ef41Sopenharmony_cia [Readable][] stream. 35041cb0ef41Sopenharmony_ci 35051cb0ef41Sopenharmony_ciFor details of how to read from `stdin` see [`readable.read()`][]. 35061cb0ef41Sopenharmony_ci 35071cb0ef41Sopenharmony_ciAs a [Duplex][] stream, `process.stdin` can also be used in "old" mode that 35081cb0ef41Sopenharmony_ciis compatible with scripts written for Node.js prior to v0.10. 35091cb0ef41Sopenharmony_ciFor more information see [Stream compatibility][]. 35101cb0ef41Sopenharmony_ci 35111cb0ef41Sopenharmony_ciIn "old" streams mode the `stdin` stream is paused by default, so one 35121cb0ef41Sopenharmony_cimust call `process.stdin.resume()` to read from it. Note also that calling 35131cb0ef41Sopenharmony_ci`process.stdin.resume()` itself would switch stream to "old" mode. 35141cb0ef41Sopenharmony_ci 35151cb0ef41Sopenharmony_ci### `process.stdin.fd` 35161cb0ef41Sopenharmony_ci 35171cb0ef41Sopenharmony_ci* {number} 35181cb0ef41Sopenharmony_ci 35191cb0ef41Sopenharmony_ciThis property refers to the value of underlying file descriptor of 35201cb0ef41Sopenharmony_ci`process.stdin`. The value is fixed at `0`. In [`Worker`][] threads, 35211cb0ef41Sopenharmony_cithis field does not exist. 35221cb0ef41Sopenharmony_ci 35231cb0ef41Sopenharmony_ci## `process.stdout` 35241cb0ef41Sopenharmony_ci 35251cb0ef41Sopenharmony_ci* {Stream} 35261cb0ef41Sopenharmony_ci 35271cb0ef41Sopenharmony_ciThe `process.stdout` property returns a stream connected to 35281cb0ef41Sopenharmony_ci`stdout` (fd `1`). It is a [`net.Socket`][] (which is a [Duplex][] 35291cb0ef41Sopenharmony_cistream) unless fd `1` refers to a file, in which case it is 35301cb0ef41Sopenharmony_cia [Writable][] stream. 35311cb0ef41Sopenharmony_ci 35321cb0ef41Sopenharmony_ciFor example, to copy `process.stdin` to `process.stdout`: 35331cb0ef41Sopenharmony_ci 35341cb0ef41Sopenharmony_ci```mjs 35351cb0ef41Sopenharmony_ciimport { stdin, stdout } from 'node:process'; 35361cb0ef41Sopenharmony_ci 35371cb0ef41Sopenharmony_cistdin.pipe(stdout); 35381cb0ef41Sopenharmony_ci``` 35391cb0ef41Sopenharmony_ci 35401cb0ef41Sopenharmony_ci```cjs 35411cb0ef41Sopenharmony_ciconst { stdin, stdout } = require('node:process'); 35421cb0ef41Sopenharmony_ci 35431cb0ef41Sopenharmony_cistdin.pipe(stdout); 35441cb0ef41Sopenharmony_ci``` 35451cb0ef41Sopenharmony_ci 35461cb0ef41Sopenharmony_ci`process.stdout` differs from other Node.js streams in important ways. See 35471cb0ef41Sopenharmony_ci[note on process I/O][] for more information. 35481cb0ef41Sopenharmony_ci 35491cb0ef41Sopenharmony_ci### `process.stdout.fd` 35501cb0ef41Sopenharmony_ci 35511cb0ef41Sopenharmony_ci* {number} 35521cb0ef41Sopenharmony_ci 35531cb0ef41Sopenharmony_ciThis property refers to the value of underlying file descriptor of 35541cb0ef41Sopenharmony_ci`process.stdout`. The value is fixed at `1`. In [`Worker`][] threads, 35551cb0ef41Sopenharmony_cithis field does not exist. 35561cb0ef41Sopenharmony_ci 35571cb0ef41Sopenharmony_ci### A note on process I/O 35581cb0ef41Sopenharmony_ci 35591cb0ef41Sopenharmony_ci`process.stdout` and `process.stderr` differ from other Node.js streams in 35601cb0ef41Sopenharmony_ciimportant ways: 35611cb0ef41Sopenharmony_ci 35621cb0ef41Sopenharmony_ci1. They are used internally by [`console.log()`][] and [`console.error()`][], 35631cb0ef41Sopenharmony_ci respectively. 35641cb0ef41Sopenharmony_ci2. Writes may be synchronous depending on what the stream is connected to 35651cb0ef41Sopenharmony_ci and whether the system is Windows or POSIX: 35661cb0ef41Sopenharmony_ci * Files: _synchronous_ on Windows and POSIX 35671cb0ef41Sopenharmony_ci * TTYs (Terminals): _asynchronous_ on Windows, _synchronous_ on POSIX 35681cb0ef41Sopenharmony_ci * Pipes (and sockets): _synchronous_ on Windows, _asynchronous_ on POSIX 35691cb0ef41Sopenharmony_ci 35701cb0ef41Sopenharmony_ciThese behaviors are partly for historical reasons, as changing them would 35711cb0ef41Sopenharmony_cicreate backward incompatibility, but they are also expected by some users. 35721cb0ef41Sopenharmony_ci 35731cb0ef41Sopenharmony_ciSynchronous writes avoid problems such as output written with `console.log()` or 35741cb0ef41Sopenharmony_ci`console.error()` being unexpectedly interleaved, or not written at all if 35751cb0ef41Sopenharmony_ci`process.exit()` is called before an asynchronous write completes. See 35761cb0ef41Sopenharmony_ci[`process.exit()`][] for more information. 35771cb0ef41Sopenharmony_ci 35781cb0ef41Sopenharmony_ci_**Warning**_: Synchronous writes block the event loop until the write has 35791cb0ef41Sopenharmony_cicompleted. This can be near instantaneous in the case of output to a file, but 35801cb0ef41Sopenharmony_ciunder high system load, pipes that are not being read at the receiving end, or 35811cb0ef41Sopenharmony_ciwith slow terminals or file systems, it's possible for the event loop to be 35821cb0ef41Sopenharmony_ciblocked often enough and long enough to have severe negative performance 35831cb0ef41Sopenharmony_ciimpacts. This may not be a problem when writing to an interactive terminal 35841cb0ef41Sopenharmony_cisession, but consider this particularly careful when doing production logging to 35851cb0ef41Sopenharmony_cithe process output streams. 35861cb0ef41Sopenharmony_ci 35871cb0ef41Sopenharmony_ciTo check if a stream is connected to a [TTY][] context, check the `isTTY` 35881cb0ef41Sopenharmony_ciproperty. 35891cb0ef41Sopenharmony_ci 35901cb0ef41Sopenharmony_ciFor instance: 35911cb0ef41Sopenharmony_ci 35921cb0ef41Sopenharmony_ci```console 35931cb0ef41Sopenharmony_ci$ node -p "Boolean(process.stdin.isTTY)" 35941cb0ef41Sopenharmony_citrue 35951cb0ef41Sopenharmony_ci$ echo "foo" | node -p "Boolean(process.stdin.isTTY)" 35961cb0ef41Sopenharmony_cifalse 35971cb0ef41Sopenharmony_ci$ node -p "Boolean(process.stdout.isTTY)" 35981cb0ef41Sopenharmony_citrue 35991cb0ef41Sopenharmony_ci$ node -p "Boolean(process.stdout.isTTY)" | cat 36001cb0ef41Sopenharmony_cifalse 36011cb0ef41Sopenharmony_ci``` 36021cb0ef41Sopenharmony_ci 36031cb0ef41Sopenharmony_ciSee the [TTY][] documentation for more information. 36041cb0ef41Sopenharmony_ci 36051cb0ef41Sopenharmony_ci## `process.throwDeprecation` 36061cb0ef41Sopenharmony_ci 36071cb0ef41Sopenharmony_ci<!-- YAML 36081cb0ef41Sopenharmony_ciadded: v0.9.12 36091cb0ef41Sopenharmony_ci--> 36101cb0ef41Sopenharmony_ci 36111cb0ef41Sopenharmony_ci* {boolean} 36121cb0ef41Sopenharmony_ci 36131cb0ef41Sopenharmony_ciThe initial value of `process.throwDeprecation` indicates whether the 36141cb0ef41Sopenharmony_ci`--throw-deprecation` flag is set on the current Node.js process. 36151cb0ef41Sopenharmony_ci`process.throwDeprecation` is mutable, so whether or not deprecation 36161cb0ef41Sopenharmony_ciwarnings result in errors may be altered at runtime. See the 36171cb0ef41Sopenharmony_cidocumentation for the [`'warning'` event][process_warning] and the 36181cb0ef41Sopenharmony_ci[`emitWarning()` method][process_emit_warning] for more information. 36191cb0ef41Sopenharmony_ci 36201cb0ef41Sopenharmony_ci```console 36211cb0ef41Sopenharmony_ci$ node --throw-deprecation -p "process.throwDeprecation" 36221cb0ef41Sopenharmony_citrue 36231cb0ef41Sopenharmony_ci$ node -p "process.throwDeprecation" 36241cb0ef41Sopenharmony_ciundefined 36251cb0ef41Sopenharmony_ci$ node 36261cb0ef41Sopenharmony_ci> process.emitWarning('test', 'DeprecationWarning'); 36271cb0ef41Sopenharmony_ciundefined 36281cb0ef41Sopenharmony_ci> (node:26598) DeprecationWarning: test 36291cb0ef41Sopenharmony_ci> process.throwDeprecation = true; 36301cb0ef41Sopenharmony_citrue 36311cb0ef41Sopenharmony_ci> process.emitWarning('test', 'DeprecationWarning'); 36321cb0ef41Sopenharmony_ciThrown: 36331cb0ef41Sopenharmony_ci[DeprecationWarning: test] { name: 'DeprecationWarning' } 36341cb0ef41Sopenharmony_ci``` 36351cb0ef41Sopenharmony_ci 36361cb0ef41Sopenharmony_ci## `process.title` 36371cb0ef41Sopenharmony_ci 36381cb0ef41Sopenharmony_ci<!-- YAML 36391cb0ef41Sopenharmony_ciadded: v0.1.104 36401cb0ef41Sopenharmony_ci--> 36411cb0ef41Sopenharmony_ci 36421cb0ef41Sopenharmony_ci* {string} 36431cb0ef41Sopenharmony_ci 36441cb0ef41Sopenharmony_ciThe `process.title` property returns the current process title (i.e. returns 36451cb0ef41Sopenharmony_cithe current value of `ps`). Assigning a new value to `process.title` modifies 36461cb0ef41Sopenharmony_cithe current value of `ps`. 36471cb0ef41Sopenharmony_ci 36481cb0ef41Sopenharmony_ciWhen a new value is assigned, different platforms will impose different maximum 36491cb0ef41Sopenharmony_cilength restrictions on the title. Usually such restrictions are quite limited. 36501cb0ef41Sopenharmony_ciFor instance, on Linux and macOS, `process.title` is limited to the size of the 36511cb0ef41Sopenharmony_cibinary name plus the length of the command-line arguments because setting the 36521cb0ef41Sopenharmony_ci`process.title` overwrites the `argv` memory of the process. Node.js v0.8 36531cb0ef41Sopenharmony_ciallowed for longer process title strings by also overwriting the `environ` 36541cb0ef41Sopenharmony_cimemory but that was potentially insecure and confusing in some (rather obscure) 36551cb0ef41Sopenharmony_cicases. 36561cb0ef41Sopenharmony_ci 36571cb0ef41Sopenharmony_ciAssigning a value to `process.title` might not result in an accurate label 36581cb0ef41Sopenharmony_ciwithin process manager applications such as macOS Activity Monitor or Windows 36591cb0ef41Sopenharmony_ciServices Manager. 36601cb0ef41Sopenharmony_ci 36611cb0ef41Sopenharmony_ci## `process.traceDeprecation` 36621cb0ef41Sopenharmony_ci 36631cb0ef41Sopenharmony_ci<!-- YAML 36641cb0ef41Sopenharmony_ciadded: v0.8.0 36651cb0ef41Sopenharmony_ci--> 36661cb0ef41Sopenharmony_ci 36671cb0ef41Sopenharmony_ci* {boolean} 36681cb0ef41Sopenharmony_ci 36691cb0ef41Sopenharmony_ciThe `process.traceDeprecation` property indicates whether the 36701cb0ef41Sopenharmony_ci`--trace-deprecation` flag is set on the current Node.js process. See the 36711cb0ef41Sopenharmony_cidocumentation for the [`'warning'` event][process_warning] and the 36721cb0ef41Sopenharmony_ci[`emitWarning()` method][process_emit_warning] for more information about this 36731cb0ef41Sopenharmony_ciflag's behavior. 36741cb0ef41Sopenharmony_ci 36751cb0ef41Sopenharmony_ci## `process.umask()` 36761cb0ef41Sopenharmony_ci 36771cb0ef41Sopenharmony_ci<!-- YAML 36781cb0ef41Sopenharmony_ciadded: v0.1.19 36791cb0ef41Sopenharmony_cichanges: 36801cb0ef41Sopenharmony_ci - version: 36811cb0ef41Sopenharmony_ci - v14.0.0 36821cb0ef41Sopenharmony_ci - v12.19.0 36831cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/32499 36841cb0ef41Sopenharmony_ci description: Calling `process.umask()` with no arguments is deprecated. 36851cb0ef41Sopenharmony_ci--> 36861cb0ef41Sopenharmony_ci 36871cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated. Calling `process.umask()` with no argument causes 36881cb0ef41Sopenharmony_ci> the process-wide umask to be written twice. This introduces a race condition 36891cb0ef41Sopenharmony_ci> between threads, and is a potential security vulnerability. There is no safe, 36901cb0ef41Sopenharmony_ci> cross-platform alternative API. 36911cb0ef41Sopenharmony_ci 36921cb0ef41Sopenharmony_ci`process.umask()` returns the Node.js process's file mode creation mask. Child 36931cb0ef41Sopenharmony_ciprocesses inherit the mask from the parent process. 36941cb0ef41Sopenharmony_ci 36951cb0ef41Sopenharmony_ci## `process.umask(mask)` 36961cb0ef41Sopenharmony_ci 36971cb0ef41Sopenharmony_ci<!-- YAML 36981cb0ef41Sopenharmony_ciadded: v0.1.19 36991cb0ef41Sopenharmony_ci--> 37001cb0ef41Sopenharmony_ci 37011cb0ef41Sopenharmony_ci* `mask` {string|integer} 37021cb0ef41Sopenharmony_ci 37031cb0ef41Sopenharmony_ci`process.umask(mask)` sets the Node.js process's file mode creation mask. Child 37041cb0ef41Sopenharmony_ciprocesses inherit the mask from the parent process. Returns the previous mask. 37051cb0ef41Sopenharmony_ci 37061cb0ef41Sopenharmony_ci```mjs 37071cb0ef41Sopenharmony_ciimport { umask } from 'node:process'; 37081cb0ef41Sopenharmony_ci 37091cb0ef41Sopenharmony_ciconst newmask = 0o022; 37101cb0ef41Sopenharmony_ciconst oldmask = umask(newmask); 37111cb0ef41Sopenharmony_ciconsole.log( 37121cb0ef41Sopenharmony_ci `Changed umask from ${oldmask.toString(8)} to ${newmask.toString(8)}`, 37131cb0ef41Sopenharmony_ci); 37141cb0ef41Sopenharmony_ci``` 37151cb0ef41Sopenharmony_ci 37161cb0ef41Sopenharmony_ci```cjs 37171cb0ef41Sopenharmony_ciconst { umask } = require('node:process'); 37181cb0ef41Sopenharmony_ci 37191cb0ef41Sopenharmony_ciconst newmask = 0o022; 37201cb0ef41Sopenharmony_ciconst oldmask = umask(newmask); 37211cb0ef41Sopenharmony_ciconsole.log( 37221cb0ef41Sopenharmony_ci `Changed umask from ${oldmask.toString(8)} to ${newmask.toString(8)}`, 37231cb0ef41Sopenharmony_ci); 37241cb0ef41Sopenharmony_ci``` 37251cb0ef41Sopenharmony_ci 37261cb0ef41Sopenharmony_ciIn [`Worker`][] threads, `process.umask(mask)` will throw an exception. 37271cb0ef41Sopenharmony_ci 37281cb0ef41Sopenharmony_ci## `process.uptime()` 37291cb0ef41Sopenharmony_ci 37301cb0ef41Sopenharmony_ci<!-- YAML 37311cb0ef41Sopenharmony_ciadded: v0.5.0 37321cb0ef41Sopenharmony_ci--> 37331cb0ef41Sopenharmony_ci 37341cb0ef41Sopenharmony_ci* Returns: {number} 37351cb0ef41Sopenharmony_ci 37361cb0ef41Sopenharmony_ciThe `process.uptime()` method returns the number of seconds the current Node.js 37371cb0ef41Sopenharmony_ciprocess has been running. 37381cb0ef41Sopenharmony_ci 37391cb0ef41Sopenharmony_ciThe return value includes fractions of a second. Use `Math.floor()` to get whole 37401cb0ef41Sopenharmony_ciseconds. 37411cb0ef41Sopenharmony_ci 37421cb0ef41Sopenharmony_ci## `process.version` 37431cb0ef41Sopenharmony_ci 37441cb0ef41Sopenharmony_ci<!-- YAML 37451cb0ef41Sopenharmony_ciadded: v0.1.3 37461cb0ef41Sopenharmony_ci--> 37471cb0ef41Sopenharmony_ci 37481cb0ef41Sopenharmony_ci* {string} 37491cb0ef41Sopenharmony_ci 37501cb0ef41Sopenharmony_ciThe `process.version` property contains the Node.js version string. 37511cb0ef41Sopenharmony_ci 37521cb0ef41Sopenharmony_ci```mjs 37531cb0ef41Sopenharmony_ciimport { version } from 'node:process'; 37541cb0ef41Sopenharmony_ci 37551cb0ef41Sopenharmony_ciconsole.log(`Version: ${version}`); 37561cb0ef41Sopenharmony_ci// Version: v14.8.0 37571cb0ef41Sopenharmony_ci``` 37581cb0ef41Sopenharmony_ci 37591cb0ef41Sopenharmony_ci```cjs 37601cb0ef41Sopenharmony_ciconst { version } = require('node:process'); 37611cb0ef41Sopenharmony_ci 37621cb0ef41Sopenharmony_ciconsole.log(`Version: ${version}`); 37631cb0ef41Sopenharmony_ci// Version: v14.8.0 37641cb0ef41Sopenharmony_ci``` 37651cb0ef41Sopenharmony_ci 37661cb0ef41Sopenharmony_ciTo get the version string without the prepended _v_, use 37671cb0ef41Sopenharmony_ci`process.versions.node`. 37681cb0ef41Sopenharmony_ci 37691cb0ef41Sopenharmony_ci## `process.versions` 37701cb0ef41Sopenharmony_ci 37711cb0ef41Sopenharmony_ci<!-- YAML 37721cb0ef41Sopenharmony_ciadded: v0.2.0 37731cb0ef41Sopenharmony_cichanges: 37741cb0ef41Sopenharmony_ci - version: v9.0.0 37751cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/15785 37761cb0ef41Sopenharmony_ci description: The `v8` property now includes a Node.js specific suffix. 37771cb0ef41Sopenharmony_ci - version: v4.2.0 37781cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/3102 37791cb0ef41Sopenharmony_ci description: The `icu` property is now supported. 37801cb0ef41Sopenharmony_ci--> 37811cb0ef41Sopenharmony_ci 37821cb0ef41Sopenharmony_ci* {Object} 37831cb0ef41Sopenharmony_ci 37841cb0ef41Sopenharmony_ciThe `process.versions` property returns an object listing the version strings of 37851cb0ef41Sopenharmony_ciNode.js and its dependencies. `process.versions.modules` indicates the current 37861cb0ef41Sopenharmony_ciABI version, which is increased whenever a C++ API changes. Node.js will refuse 37871cb0ef41Sopenharmony_cito load modules that were compiled against a different module ABI version. 37881cb0ef41Sopenharmony_ci 37891cb0ef41Sopenharmony_ci```mjs 37901cb0ef41Sopenharmony_ciimport { versions } from 'node:process'; 37911cb0ef41Sopenharmony_ci 37921cb0ef41Sopenharmony_ciconsole.log(versions); 37931cb0ef41Sopenharmony_ci``` 37941cb0ef41Sopenharmony_ci 37951cb0ef41Sopenharmony_ci```cjs 37961cb0ef41Sopenharmony_ciconst { versions } = require('node:process'); 37971cb0ef41Sopenharmony_ci 37981cb0ef41Sopenharmony_ciconsole.log(versions); 37991cb0ef41Sopenharmony_ci``` 38001cb0ef41Sopenharmony_ci 38011cb0ef41Sopenharmony_ciWill generate an object similar to: 38021cb0ef41Sopenharmony_ci 38031cb0ef41Sopenharmony_ci```console 38041cb0ef41Sopenharmony_ci{ node: '11.13.0', 38051cb0ef41Sopenharmony_ci v8: '7.0.276.38-node.18', 38061cb0ef41Sopenharmony_ci uv: '1.27.0', 38071cb0ef41Sopenharmony_ci zlib: '1.2.11', 38081cb0ef41Sopenharmony_ci brotli: '1.0.7', 38091cb0ef41Sopenharmony_ci ares: '1.15.0', 38101cb0ef41Sopenharmony_ci modules: '67', 38111cb0ef41Sopenharmony_ci nghttp2: '1.34.0', 38121cb0ef41Sopenharmony_ci napi: '4', 38131cb0ef41Sopenharmony_ci llhttp: '1.1.1', 38141cb0ef41Sopenharmony_ci openssl: '1.1.1b', 38151cb0ef41Sopenharmony_ci cldr: '34.0', 38161cb0ef41Sopenharmony_ci icu: '63.1', 38171cb0ef41Sopenharmony_ci tz: '2018e', 38181cb0ef41Sopenharmony_ci unicode: '11.0' } 38191cb0ef41Sopenharmony_ci``` 38201cb0ef41Sopenharmony_ci 38211cb0ef41Sopenharmony_ci## Exit codes 38221cb0ef41Sopenharmony_ci 38231cb0ef41Sopenharmony_ciNode.js will normally exit with a `0` status code when no more async 38241cb0ef41Sopenharmony_cioperations are pending. The following status codes are used in other 38251cb0ef41Sopenharmony_cicases: 38261cb0ef41Sopenharmony_ci 38271cb0ef41Sopenharmony_ci* `1` **Uncaught Fatal Exception**: There was an uncaught exception, 38281cb0ef41Sopenharmony_ci and it was not handled by a domain or an [`'uncaughtException'`][] event 38291cb0ef41Sopenharmony_ci handler. 38301cb0ef41Sopenharmony_ci* `2`: Unused (reserved by Bash for builtin misuse) 38311cb0ef41Sopenharmony_ci* `3` **Internal JavaScript Parse Error**: The JavaScript source code 38321cb0ef41Sopenharmony_ci internal in the Node.js bootstrapping process caused a parse error. This 38331cb0ef41Sopenharmony_ci is extremely rare, and generally can only happen during development 38341cb0ef41Sopenharmony_ci of Node.js itself. 38351cb0ef41Sopenharmony_ci* `4` **Internal JavaScript Evaluation Failure**: The JavaScript 38361cb0ef41Sopenharmony_ci source code internal in the Node.js bootstrapping process failed to 38371cb0ef41Sopenharmony_ci return a function value when evaluated. This is extremely rare, and 38381cb0ef41Sopenharmony_ci generally can only happen during development of Node.js itself. 38391cb0ef41Sopenharmony_ci* `5` **Fatal Error**: There was a fatal unrecoverable error in V8. 38401cb0ef41Sopenharmony_ci Typically a message will be printed to stderr with the prefix `FATAL 38411cb0ef41Sopenharmony_ci ERROR`. 38421cb0ef41Sopenharmony_ci* `6` **Non-function Internal Exception Handler**: There was an 38431cb0ef41Sopenharmony_ci uncaught exception, but the internal fatal exception handler 38441cb0ef41Sopenharmony_ci function was somehow set to a non-function, and could not be called. 38451cb0ef41Sopenharmony_ci* `7` **Internal Exception Handler Run-Time Failure**: There was an 38461cb0ef41Sopenharmony_ci uncaught exception, and the internal fatal exception handler 38471cb0ef41Sopenharmony_ci function itself threw an error while attempting to handle it. This 38481cb0ef41Sopenharmony_ci can happen, for example, if an [`'uncaughtException'`][] or 38491cb0ef41Sopenharmony_ci `domain.on('error')` handler throws an error. 38501cb0ef41Sopenharmony_ci* `8`: Unused. In previous versions of Node.js, exit code 8 sometimes 38511cb0ef41Sopenharmony_ci indicated an uncaught exception. 38521cb0ef41Sopenharmony_ci* `9` **Invalid Argument**: Either an unknown option was specified, 38531cb0ef41Sopenharmony_ci or an option requiring a value was provided without a value. 38541cb0ef41Sopenharmony_ci* `10` **Internal JavaScript Run-Time Failure**: The JavaScript 38551cb0ef41Sopenharmony_ci source code internal in the Node.js bootstrapping process threw an error 38561cb0ef41Sopenharmony_ci when the bootstrapping function was called. This is extremely rare, 38571cb0ef41Sopenharmony_ci and generally can only happen during development of Node.js itself. 38581cb0ef41Sopenharmony_ci* `12` **Invalid Debug Argument**: The `--inspect` and/or `--inspect-brk` 38591cb0ef41Sopenharmony_ci options were set, but the port number chosen was invalid or unavailable. 38601cb0ef41Sopenharmony_ci* `13` **Unfinished Top-Level Await**: `await` was used outside of a function 38611cb0ef41Sopenharmony_ci in the top-level code, but the passed `Promise` never resolved. 38621cb0ef41Sopenharmony_ci* `14` **Snapshot Failure**: Node.js was started to build a V8 startup 38631cb0ef41Sopenharmony_ci snapshot and it failed because certain requirements of the state of 38641cb0ef41Sopenharmony_ci the application were not met. 38651cb0ef41Sopenharmony_ci* `>128` **Signal Exits**: If Node.js receives a fatal signal such as 38661cb0ef41Sopenharmony_ci `SIGKILL` or `SIGHUP`, then its exit code will be `128` plus the 38671cb0ef41Sopenharmony_ci value of the signal code. This is a standard POSIX practice, since 38681cb0ef41Sopenharmony_ci exit codes are defined to be 7-bit integers, and signal exits set 38691cb0ef41Sopenharmony_ci the high-order bit, and then contain the value of the signal code. 38701cb0ef41Sopenharmony_ci For example, signal `SIGABRT` has value `6`, so the expected exit 38711cb0ef41Sopenharmony_ci code will be `128` + `6`, or `134`. 38721cb0ef41Sopenharmony_ci 38731cb0ef41Sopenharmony_ci[Advanced serialization for `child_process`]: child_process.md#advanced-serialization 38741cb0ef41Sopenharmony_ci[Android building]: https://github.com/nodejs/node/blob/HEAD/BUILDING.md#androidandroid-based-devices-eg-firefox-os 38751cb0ef41Sopenharmony_ci[Child Process]: child_process.md 38761cb0ef41Sopenharmony_ci[Cluster]: cluster.md 38771cb0ef41Sopenharmony_ci[Duplex]: stream.md#duplex-and-transform-streams 38781cb0ef41Sopenharmony_ci[Event Loop]: https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#process-nexttick 38791cb0ef41Sopenharmony_ci[LTS]: https://github.com/nodejs/Release 38801cb0ef41Sopenharmony_ci[Readable]: stream.md#readable-streams 38811cb0ef41Sopenharmony_ci[Signal Events]: #signal-events 38821cb0ef41Sopenharmony_ci[Source Map]: https://sourcemaps.info/spec.html 38831cb0ef41Sopenharmony_ci[Stream compatibility]: stream.md#compatibility-with-older-nodejs-versions 38841cb0ef41Sopenharmony_ci[TTY]: tty.md#tty 38851cb0ef41Sopenharmony_ci[Writable]: stream.md#writable-streams 38861cb0ef41Sopenharmony_ci[`'exit'`]: #event-exit 38871cb0ef41Sopenharmony_ci[`'message'`]: child_process.md#event-message 38881cb0ef41Sopenharmony_ci[`'uncaughtException'`]: #event-uncaughtexception 38891cb0ef41Sopenharmony_ci[`--unhandled-rejections`]: cli.md#--unhandled-rejectionsmode 38901cb0ef41Sopenharmony_ci[`Buffer`]: buffer.md 38911cb0ef41Sopenharmony_ci[`ChildProcess.disconnect()`]: child_process.md#subprocessdisconnect 38921cb0ef41Sopenharmony_ci[`ChildProcess.send()`]: child_process.md#subprocesssendmessage-sendhandle-options-callback 38931cb0ef41Sopenharmony_ci[`ChildProcess`]: child_process.md#class-childprocess 38941cb0ef41Sopenharmony_ci[`Error`]: errors.md#class-error 38951cb0ef41Sopenharmony_ci[`EventEmitter`]: events.md#class-eventemitter 38961cb0ef41Sopenharmony_ci[`NODE_OPTIONS`]: cli.md#node_optionsoptions 38971cb0ef41Sopenharmony_ci[`Promise.race()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race 38981cb0ef41Sopenharmony_ci[`Worker`]: worker_threads.md#class-worker 38991cb0ef41Sopenharmony_ci[`Worker` constructor]: worker_threads.md#new-workerfilename-options 39001cb0ef41Sopenharmony_ci[`console.error()`]: console.md#consoleerrordata-args 39011cb0ef41Sopenharmony_ci[`console.log()`]: console.md#consolelogdata-args 39021cb0ef41Sopenharmony_ci[`domain`]: domain.md 39031cb0ef41Sopenharmony_ci[`net.Server`]: net.md#class-netserver 39041cb0ef41Sopenharmony_ci[`net.Socket`]: net.md#class-netsocket 39051cb0ef41Sopenharmony_ci[`os.constants.dlopen`]: os.md#dlopen-constants 39061cb0ef41Sopenharmony_ci[`process.argv`]: #processargv 39071cb0ef41Sopenharmony_ci[`process.config`]: #processconfig 39081cb0ef41Sopenharmony_ci[`process.execPath`]: #processexecpath 39091cb0ef41Sopenharmony_ci[`process.exit()`]: #processexitcode 39101cb0ef41Sopenharmony_ci[`process.exitCode`]: #processexitcode_1 39111cb0ef41Sopenharmony_ci[`process.hrtime()`]: #processhrtimetime 39121cb0ef41Sopenharmony_ci[`process.hrtime.bigint()`]: #processhrtimebigint 39131cb0ef41Sopenharmony_ci[`process.kill()`]: #processkillpid-signal 39141cb0ef41Sopenharmony_ci[`process.setUncaughtExceptionCaptureCallback()`]: #processsetuncaughtexceptioncapturecallbackfn 39151cb0ef41Sopenharmony_ci[`promise.catch()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch 39161cb0ef41Sopenharmony_ci[`queueMicrotask()`]: globals.md#queuemicrotaskcallback 39171cb0ef41Sopenharmony_ci[`readable.read()`]: stream.md#readablereadsize 39181cb0ef41Sopenharmony_ci[`require()`]: globals.md#require 39191cb0ef41Sopenharmony_ci[`require.main`]: modules.md#accessing-the-main-module 39201cb0ef41Sopenharmony_ci[`subprocess.kill()`]: child_process.md#subprocesskillsignal 39211cb0ef41Sopenharmony_ci[`v8.setFlagsFromString()`]: v8.md#v8setflagsfromstringflags 39221cb0ef41Sopenharmony_ci[debugger]: debugger.md 39231cb0ef41Sopenharmony_ci[deprecation code]: deprecations.md 39241cb0ef41Sopenharmony_ci[note on process I/O]: #a-note-on-process-io 39251cb0ef41Sopenharmony_ci[process.cpuUsage]: #processcpuusagepreviousvalue 39261cb0ef41Sopenharmony_ci[process_emit_warning]: #processemitwarningwarning-type-code-ctor 39271cb0ef41Sopenharmony_ci[process_warning]: #event-warning 39281cb0ef41Sopenharmony_ci[report documentation]: report.md 39291cb0ef41Sopenharmony_ci[terminal raw mode]: tty.md#readstreamsetrawmodemode 39301cb0ef41Sopenharmony_ci[uv_get_constrained_memory]: https://docs.libuv.org/en/v1.x/misc.html#c.uv_get_constrained_memory 39311cb0ef41Sopenharmony_ci[uv_rusage_t]: https://docs.libuv.org/en/v1.x/misc.html#c.uv_rusage_t 39321cb0ef41Sopenharmony_ci[wikipedia_major_fault]: https://en.wikipedia.org/wiki/Page_fault#Major 39331cb0ef41Sopenharmony_ci[wikipedia_minor_fault]: https://en.wikipedia.org/wiki/Page_fault#Minor 3934