Skip to content

Latest commit

Β 

History

History
2578 lines (2047 loc) Β· 78.5 KB

File metadata and controls

2578 lines (2047 loc) Β· 78.5 KB
Β 
Feb 10, 2017
Feb 10, 2017
1
# Process
Feb 27, 2012
Feb 27, 2012
2
Aug 28, 2017
Aug 28, 2017
3
<!-- introduced_in=v0.10.0 -->
Feb 27, 2012
Feb 27, 2012
4
<!-- type=global -->
Oct 28, 2010
Oct 28, 2010
5
Jun 6, 2016
Jun 6, 2016
6
The `process` object is a `global` that provides information about, and control
7
over, the current Node.js process. As a global, it is always available to
Mar 26, 2019
Mar 26, 2019
8
Node.js applications without using `require()`. It can also be explicitly
9
accessed using `require()`:
10
11
```js
12
const process = require('process');
13
```
Oct 28, 2010
Oct 28, 2010
14
Jun 6, 2016
Jun 6, 2016
15
## Process Events
16
17
The `process` object is an instance of [`EventEmitter`][].
18
Dec 27, 2019
Dec 27, 2019
19
### Event: `'beforeExit'`
May 9, 2016
May 9, 2016
20
<!-- YAML
21
added: v0.11.12
22
-->
Sep 7, 2013
Sep 7, 2013
23
Jun 6, 2016
Jun 6, 2016
24
The `'beforeExit'` event is emitted when Node.js empties its event loop and has
25
no additional work to schedule. Normally, the Node.js process will exit when
26
there is no work scheduled, but a listener registered on the `'beforeExit'`
27
event can make asynchronous calls, and thereby cause the Node.js process to
Mar 3, 2016
Mar 3, 2016
28
continue.
Sep 7, 2013
Sep 7, 2013
29
Jun 6, 2016
Jun 6, 2016
30
The listener callback function is invoked with the value of
31
[`process.exitCode`][] passed as the only argument.
32
33
The `'beforeExit'` event is *not* emitted for conditions causing explicit
34
termination, such as calling [`process.exit()`][] or uncaught exceptions.
35
36
The `'beforeExit'` should *not* be used as an alternative to the `'exit'` event
37
unless the intention is to schedule additional work.
Oct 28, 2010
Oct 28, 2010
38
Jul 18, 2019
Jul 18, 2019
39
```js
40
process.on('beforeExit', (code) => {
41
console.log('Process beforeExit event with code: ', code);
42
});
43
44
process.on('exit', (code) => {
45
console.log('Process exit event with code: ', code);
46
});
47
48
console.log('This message is displayed first.');
49
50
// Prints:
51
// This message is displayed first.
52
// Process beforeExit event with code: 0
53
// Process exit event with code: 0
54
```
55
Dec 27, 2019
Dec 27, 2019
56
### Event: `'disconnect'`
May 9, 2016
May 9, 2016
57
<!-- YAML
58
added: v0.7.7
59
-->
Apr 22, 2016
Apr 22, 2016
60
Jun 6, 2016
Jun 6, 2016
61
If the Node.js process is spawned with an IPC channel (see the [Child Process][]
62
and [Cluster][] documentation), the `'disconnect'` event will be emitted when
63
the IPC channel is closed.
Apr 22, 2016
Apr 22, 2016
64
Dec 27, 2019
Dec 27, 2019
65
### Event: `'exit'`
May 9, 2016
May 9, 2016
66
<!-- YAML
67
added: v0.1.7
68
-->
Oct 28, 2010
Oct 28, 2010
69
Apr 29, 2018
Apr 29, 2018
70
* `code` {integer}
71
Jun 6, 2016
Jun 6, 2016
72
The `'exit'` event is emitted when the Node.js process is about to exit as a
73
result of either:
74
75
* The `process.exit()` method being called explicitly;
76
* The Node.js event loop no longer having any additional work to perform.
77
78
There is no way to prevent the exiting of the event loop at this point, and once
79
all `'exit'` listeners have finished running the Node.js process will terminate.
80
81
The listener callback function is invoked with the exit code specified either
82
by the [`process.exitCode`][] property, or the `exitCode` argument passed to the
Oct 4, 2019
Oct 4, 2019
83
[`process.exit()`][] method.
Oct 28, 2010
Oct 28, 2010
84
Jun 6, 2016
Jun 6, 2016
85
```js
86
process.on('exit', (code) => {
87
console.log(`About to exit with code: ${code}`);
88
});
89
```
Sep 16, 2015
Sep 16, 2015
90
Jun 6, 2016
Jun 6, 2016
91
Listener functions **must** only perform **synchronous** operations. The Node.js
92
process will exit immediately after calling the `'exit'` event listeners
93
causing any additional work still queued in the event loop to be abandoned.
94
In the following example, for instance, the timeout will never occur:
Oct 28, 2010
Oct 28, 2010
95
Jan 21, 2016
Jan 21, 2016
96
```js
97
process.on('exit', (code) => {
98
setTimeout(() => {
99
console.log('This will not run');
100
}, 0);
101
});
102
```
Oct 28, 2010
Oct 28, 2010
103
Dec 27, 2019
Dec 27, 2019
104
### Event: `'message'`
May 9, 2016
May 9, 2016
105
<!-- YAML
106
added: v0.5.10
107
-->
Sep 9, 2015
Sep 9, 2015
108
May 2, 2018
May 2, 2018
109
* `message` { Object | boolean | number | string | null } a parsed JSON object
110
or a serializable primitive value.
Apr 29, 2018
Apr 29, 2018
111
* `sendHandle` {net.Server|net.Socket} a [`net.Server`][] or [`net.Socket`][]
112
object, or undefined.
113
Jun 6, 2016
Jun 6, 2016
114
If the Node.js process is spawned with an IPC channel (see the [Child Process][]
115
and [Cluster][] documentation), the `'message'` event is emitted whenever a
116
message sent by a parent process using [`childprocess.send()`][] is received by
117
the child process.
118
Feb 8, 2018
Feb 8, 2018
119
The message goes through serialization and parsing. The resulting message might
120
not be the same as what is originally sent.
Sep 9, 2015
Sep 9, 2015
121
Nov 5, 2019
Nov 5, 2019
122
If the `serialization` option was set to `advanced` used when spawning the
123
process, the `message` argument can contain data that JSON is not able
124
to represent.
125
See [Advanced Serialization for `child_process`][] for more details.
126
Dec 27, 2019
Dec 27, 2019
127
### Event: `'multipleResolves'`
Sep 25, 2018
Sep 25, 2018
128
<!-- YAML
Oct 10, 2018
Oct 10, 2018
129
added: v10.12.0
Sep 25, 2018
Sep 25, 2018
130
-->
131
Jun 22, 2019
Jun 22, 2019
132
* `type` {string} The resolution type. One of `'resolve'` or `'reject'`.
Sep 25, 2018
Sep 25, 2018
133
* `promise` {Promise} The promise that resolved or rejected more than once.
134
* `value` {any} The value with which the promise was either resolved or
135
rejected after the original resolve.
136
137
The `'multipleResolves'` event is emitted whenever a `Promise` has been either:
138
139
* Resolved more than once.
140
* Rejected more than once.
141
* Rejected after resolve.
142
* Resolved after reject.
143
Jun 22, 2019
Jun 22, 2019
144
This is useful for tracking potential errors in an application while using the
145
`Promise` constructor, as multiple resolutions are silently swallowed. However,
146
the occurrence of this event does not necessarily indicate an error. For
147
example, [`Promise.race()`][] can trigger a `'multipleResolves'` event.
Sep 25, 2018
Sep 25, 2018
148
149
```js
150
process.on('multipleResolves', (type, promise, reason) => {
151
console.error(type, promise, reason);
152
setImmediate(() => process.exit(1));
153
});
154
155
async function main() {
156
try {
157
return await new Promise((resolve, reject) => {
158
resolve('First call');
159
resolve('Swallowed resolve');
160
reject(new Error('Swallowed reject'));
161
});
162
} catch {
163
throw new Error('Failed');
164
}
165
}
166
167
main().then(console.log);
168
// resolve: Promise { 'First call' } 'Swallowed resolve'
169
// reject: Promise { 'First call' } Error: Swallowed reject
170
// at Promise (*)
171
// at new Promise (<anonymous>)
172
// at main (*)
173
// First call
174
```
175
Dec 27, 2019
Dec 27, 2019
176
### Event: `'rejectionHandled'`
May 9, 2016
May 9, 2016
177
<!-- YAML
178
added: v1.4.1
179
-->
Sep 9, 2015
Sep 9, 2015
180
Apr 29, 2018
Apr 29, 2018
181
* `promise` {Promise} The late handled promise.
182
Jun 6, 2016
Jun 6, 2016
183
The `'rejectionHandled'` event is emitted whenever a `Promise` has been rejected
184
and an error handler was attached to it (using [`promise.catch()`][], for
185
example) later than one turn of the Node.js event loop.
186
187
The `Promise` object would have previously been emitted in an
188
`'unhandledRejection'` event, but during the course of processing gained a
189
rejection handler.
Feb 28, 2014
Feb 28, 2014
190
Jun 6, 2016
Jun 6, 2016
191
There is no notion of a top level for a `Promise` chain at which rejections can
192
always be handled. Being inherently asynchronous in nature, a `Promise`
193
rejection can be handled at a future point in time β€” possibly much later than
194
the event loop turn it takes for the `'unhandledRejection'` event to be emitted.
Nov 13, 2015
Nov 13, 2015
195
196
Another way of stating this is that, unlike in synchronous code where there is
Jun 6, 2016
Jun 6, 2016
197
an ever-growing list of unhandled exceptions, with Promises there can be a
198
growing-and-shrinking list of unhandled rejections.
199
200
In synchronous code, the `'uncaughtException'` event is emitted when the list of
201
unhandled exceptions grows.
Nov 13, 2015
Nov 13, 2015
202
Jun 6, 2016
Jun 6, 2016
203
In asynchronous code, the `'unhandledRejection'` event is emitted when the list
204
of unhandled rejections grows, and the `'rejectionHandled'` event is emitted
205
when the list of unhandled rejections shrinks.
206
Jan 21, 2016
Jan 21, 2016
207
```js
208
const unhandledRejections = new Map();
Apr 29, 2018
Apr 29, 2018
209
process.on('unhandledRejection', (reason, promise) => {
210
unhandledRejections.set(promise, reason);
Jan 21, 2016
Jan 21, 2016
211
});
Apr 29, 2018
Apr 29, 2018
212
process.on('rejectionHandled', (promise) => {
213
unhandledRejections.delete(promise);
Jan 21, 2016
Jan 21, 2016
214
});
215
```
Feb 28, 2014
Feb 28, 2014
216
Jun 6, 2016
Jun 6, 2016
217
In this example, the `unhandledRejections` `Map` will grow and shrink over time,
218
reflecting rejections that start unhandled and then become handled. It is
219
possible to record such errors in an error log, either periodically (which is
220
likely best for long-running application) or upon process exit (which is likely
221
most convenient for scripts).
Feb 28, 2014
Feb 28, 2014
222
Dec 27, 2019
Dec 27, 2019
223
### Event: `'uncaughtException'`
May 9, 2016
May 9, 2016
224
<!-- YAML
225
added: v0.1.18
Apr 15, 2019
Apr 15, 2019
226
changes:
Apr 23, 2019
Apr 23, 2019
227
- version: v12.0.0
Apr 15, 2019
Apr 15, 2019
228
pr-url: https://github.com/nodejs/node/pull/26599
229
description: Added the `origin` argument.
May 9, 2016
May 9, 2016
230
-->
Oct 28, 2010
Oct 28, 2010
231
Apr 15, 2019
Apr 15, 2019
232
* `err` {Error} The uncaught exception.
233
* `origin` {string} Indicates if the exception originates from an unhandled
234
rejection or from synchronous errors. Can either be `'uncaughtException'` or
235
`'unhandledRejection'`.
236
Jul 15, 2016
Jul 15, 2016
237
The `'uncaughtException'` event is emitted when an uncaught JavaScript
238
exception bubbles all the way back to the event loop. By default, Node.js
Jul 14, 2018
Jul 14, 2018
239
handles such exceptions by printing the stack trace to `stderr` and exiting
240
with code 1, overriding any previously set [`process.exitCode`][].
Jul 15, 2016
Jul 15, 2016
241
Adding a handler for the `'uncaughtException'` event overrides this default
Oct 11, 2018
Oct 11, 2018
242
behavior. Alternatively, change the [`process.exitCode`][] in the
243
`'uncaughtException'` handler which will result in the process exiting with the
244
provided exit code. Otherwise, in the presence of such handler the process will
Jul 14, 2018
Jul 14, 2018
245
exit with 0.
Oct 28, 2010
Oct 28, 2010
246
Jan 21, 2016
Jan 21, 2016
247
```js
Apr 15, 2019
Apr 15, 2019
248
process.on('uncaughtException', (err, origin) => {
249
fs.writeSync(
250
process.stderr.fd,
251
`Caught exception: ${err}\n` +
252
`Exception origin: ${origin}`
253
);
Jan 21, 2016
Jan 21, 2016
254
});
Oct 28, 2010
Oct 28, 2010
255
Jan 21, 2016
Jan 21, 2016
256
setTimeout(() => {
257
console.log('This will still run.');
258
}, 500);
Oct 28, 2010
Oct 28, 2010
259
Jan 21, 2016
Jan 21, 2016
260
// Intentionally cause an exception, but don't catch it.
261
nonexistentFunc();
262
console.log('This will not run.');
263
```
Oct 28, 2010
Oct 28, 2010
264
Jan 11, 2020
Jan 11, 2020
265
It is possible to monitor `'uncaughtException'` events without overriding the
266
default behavior to exit the process by installing a
267
`'uncaughtExceptionMonitor'` listener.
268
Jun 6, 2016
Jun 6, 2016
269
#### Warning: Using `'uncaughtException'` correctly
Oct 28, 2010
Oct 28, 2010
270
Jun 23, 2019
Jun 23, 2019
271
`'uncaughtException'` is a crude mechanism for exception handling
Mar 3, 2016
Mar 3, 2016
272
intended to be used only as a last resort. The event *should not* be used as
273
an equivalent to `On Error Resume Next`. Unhandled exceptions inherently mean
274
that an application is in an undefined state. Attempting to resume application
275
code without properly recovering from the exception can cause additional
276
unforeseen and unpredictable issues.
Jul 18, 2012
Jul 18, 2012
277
Feb 17, 2016
Feb 17, 2016
278
Exceptions thrown from within the event handler will not be caught. Instead the
Jul 1, 2016
Jul 1, 2016
279
process will exit with a non-zero exit code and the stack trace will be printed.
Feb 17, 2016
Feb 17, 2016
280
This is to avoid infinite recursion.
281
Mar 3, 2016
Mar 3, 2016
282
Attempting to resume normally after an uncaught exception can be similar to
Oct 26, 2019
Oct 26, 2019
283
pulling out the power cord when upgrading a computer. Nine out of ten
284
times, nothing happens. But the tenth time, the system becomes corrupted.
Jul 18, 2012
Jul 18, 2012
285
Mar 3, 2016
Mar 3, 2016
286
The correct use of `'uncaughtException'` is to perform synchronous cleanup
287
of allocated resources (e.g. file descriptors, handles, etc) before shutting
Jul 15, 2016
Jul 15, 2016
288
down the process. **It is not safe to resume normal operation after
289
`'uncaughtException'`.**
290
Feb 23, 2018
Feb 23, 2018
291
To restart a crashed application in a more reliable way, whether
Apr 12, 2018
Apr 12, 2018
292
`'uncaughtException'` is emitted or not, an external monitor should be employed
Feb 23, 2018
Feb 23, 2018
293
in a separate process to detect application failures and recover or restart as
294
needed.
Oct 28, 2010
Oct 28, 2010
295
Jan 11, 2020
Jan 11, 2020
296
### Event: `'uncaughtExceptionMonitor'`
297
<!-- YAML
Jan 21, 2020
Jan 21, 2020
298
added: v13.7.0
Jan 11, 2020
Jan 11, 2020
299
-->
300
301
* `err` {Error} The uncaught exception.
302
* `origin` {string} Indicates if the exception originates from an unhandled
303
rejection or from synchronous errors. Can either be `'uncaughtException'` or
304
`'unhandledRejection'`.
305
306
The `'uncaughtExceptionMonitor'` event is emitted before an
307
`'uncaughtException'` event is emitted or a hook installed via
308
[`process.setUncaughtExceptionCaptureCallback()`][] is called.
309
310
Installing an `'uncaughtExceptionMonitor'` listener does not change the behavior
311
once an `'uncaughtException'` event is emitted. The process will
312
still crash if no `'uncaughtException'` listener is installed.
313
314
```js
315
process.on('uncaughtExceptionMonitor', (err, origin) => {
316
MyMonitoringTool.logSync(err, origin);
317
});
318
319
// Intentionally cause an exception, but don't catch it.
320
nonexistentFunc();
321
// Still crashes Node.js
322
```
323
Dec 27, 2019
Dec 27, 2019
324
### Event: `'unhandledRejection'`
May 9, 2016
May 9, 2016
325
<!-- YAML
326
added: v1.4.1
Feb 24, 2017
Feb 24, 2017
327
changes:
328
- version: v7.0.0
329
pr-url: https://github.com/nodejs/node/pull/8217
Jun 7, 2018
Jun 7, 2018
330
description: Not handling `Promise` rejections is deprecated.
Feb 24, 2017
Feb 24, 2017
331
- version: v6.6.0
332
pr-url: https://github.com/nodejs/node/pull/8223
May 2, 2018
May 2, 2018
333
description: Unhandled `Promise` rejections will now emit
Feb 24, 2017
Feb 24, 2017
334
a process warning.
May 9, 2016
May 9, 2016
335
-->
Feb 25, 2015
Feb 25, 2015
336
Apr 15, 2019
Apr 15, 2019
337
* `reason` {Error|any} The object with which the promise was rejected
338
(typically an [`Error`][] object).
339
* `promise` {Promise} The rejected promise.
340
Apr 1, 2018
Apr 1, 2018
341
The `'unhandledRejection'` event is emitted whenever a `Promise` is rejected and
Jul 15, 2016
Jul 15, 2016
342
no error handler is attached to the promise within a turn of the event loop.
Jun 6, 2016
Jun 6, 2016
343
When programming with Promises, exceptions are encapsulated as "rejected
344
promises". Rejections can be caught and handled using [`promise.catch()`][] and
345
are propagated through a `Promise` chain. The `'unhandledRejection'` event is
Jul 15, 2016
Jul 15, 2016
346
useful for detecting and keeping track of promises that were rejected whose
Jun 6, 2016
Jun 6, 2016
347
rejections have not yet been handled.
Feb 25, 2015
Feb 25, 2015
348
Jan 21, 2016
Jan 21, 2016
349
```js
Apr 15, 2019
Apr 15, 2019
350
process.on('unhandledRejection', (reason, promise) => {
351
console.log('Unhandled Rejection at:', promise, 'reason:', reason);
Dec 10, 2018
Dec 10, 2018
352
// Application specific logging, throwing an error, or other logic here
Jan 21, 2016
Jan 21, 2016
353
});
Feb 25, 2015
Feb 25, 2015
354
Jan 21, 2016
Jan 21, 2016
355
somePromise.then((res) => {
Mar 27, 2019
Mar 27, 2019
356
return reportToUser(JSON.pasre(res)); // Note the typo (`pasre`)
357
}); // No `.catch()` or `.then()`
Jan 21, 2016
Jan 21, 2016
358
```
Feb 25, 2015
Feb 25, 2015
359
Jun 6, 2016
Jun 6, 2016
360
The following will also trigger the `'unhandledRejection'` event to be
361
emitted:
Oct 21, 2015
Oct 21, 2015
362
Jan 21, 2016
Jan 21, 2016
363
```js
364
function SomeResource() {
365
// Initially set the loaded status to a rejected promise
366
this.loaded = Promise.reject(new Error('Resource not yet loaded!'));
367
}
Oct 21, 2015
Oct 21, 2015
368
Apr 15, 2017
Apr 15, 2017
369
const resource = new SomeResource();
Jan 21, 2016
Jan 21, 2016
370
// no .catch or .then on resource.loaded for at least a turn
371
```
Oct 21, 2015
Oct 21, 2015
372
Jun 6, 2016
Jun 6, 2016
373
In this example case, it is possible to track the rejection as a developer error
374
as would typically be the case for other `'unhandledRejection'` events. To
375
address such failures, a non-operational
376
[`.catch(() => { })`][`promise.catch()`] handler may be attached to
377
`resource.loaded`, which would prevent the `'unhandledRejection'` event from
Apr 15, 2019
Apr 15, 2019
378
being emitted.
Oct 21, 2015
Oct 21, 2015
379
Dec 27, 2019
Dec 27, 2019
380
### Event: `'warning'`
May 9, 2016
May 9, 2016
381
<!-- YAML
382
added: v6.0.0
383
-->
Mar 24, 2016
Mar 24, 2016
384
Apr 29, 2018
Apr 29, 2018
385
* `warning` {Error} Key properties of the warning are:
386
* `name` {string} The name of the warning. **Default:** `'Warning'`.
387
* `message` {string} A system-provided description of the warning.
388
* `stack` {string} A stack trace to the location in the code where the warning
389
was issued.
390
Jun 6, 2016
Jun 6, 2016
391
The `'warning'` event is emitted whenever Node.js emits a process warning.
Mar 24, 2016
Mar 24, 2016
392
393
A process warning is similar to an error in that it describes exceptional
394
conditions that are being brought to the user's attention. However, warnings
395
are not part of the normal Node.js and JavaScript error handling flow.
396
Node.js can emit warnings whenever it detects bad coding practices that could
Dec 1, 2017
Dec 1, 2017
397
lead to sub-optimal application performance, bugs, or security vulnerabilities.
Mar 24, 2016
Mar 24, 2016
398
399
```js
400
process.on('warning', (warning) => {
401
console.warn(warning.name); // Print the warning name
402
console.warn(warning.message); // Print the warning message
403
console.warn(warning.stack); // Print the stack trace
404
});
405
```
406
407
By default, Node.js will print process warnings to `stderr`. The `--no-warnings`
408
command-line option can be used to suppress the default console output but the
409
`'warning'` event will still be emitted by the `process` object.
410
411
The following example illustrates the warning that is printed to `stderr` when
May 2, 2018
May 2, 2018
412
too many listeners have been added to an event:
Mar 24, 2016
Mar 24, 2016
413
Sep 3, 2019
Sep 3, 2019
414
```console
Mar 24, 2016
Mar 24, 2016
415
$ node
Mar 15, 2017
Mar 15, 2017
416
> events.defaultMaxListeners = 1;
Mar 24, 2016
Mar 24, 2016
417
> process.on('foo', () => {});
418
> process.on('foo', () => {});
Apr 15, 2017
Apr 15, 2017
419
> (node:38638) MaxListenersExceededWarning: Possible EventEmitter memory leak
420
detected. 2 foo listeners added. Use emitter.setMaxListeners() to increase limit
Mar 24, 2016
Mar 24, 2016
421
```
422
423
In contrast, the following example turns off the default warning output and
424
adds a custom handler to the `'warning'` event:
425
Sep 3, 2019
Sep 3, 2019
426
```console
Mar 24, 2016
Mar 24, 2016
427
$ node --no-warnings
Apr 15, 2017
Apr 15, 2017
428
> const p = process.on('warning', (warning) => console.warn('Do not do that!'));
Mar 15, 2017
Mar 15, 2017
429
> events.defaultMaxListeners = 1;
Mar 24, 2016
Mar 24, 2016
430
> process.on('foo', () => {});
431
> process.on('foo', () => {});
432
> Do not do that!
433
```
434
435
The `--trace-warnings` command-line option can be used to have the default
436
console output for warnings include the full stack trace of the warning.
437
438
Launching Node.js using the `--throw-deprecation` command line flag will
439
cause custom deprecation warnings to be thrown as exceptions.
440
441
Using the `--trace-deprecation` command line flag will cause the custom
442
deprecation to be printed to `stderr` along with the stack trace.
443
444
Using the `--no-deprecation` command line flag will suppress all reporting
445
of the custom deprecation.
446
447
The `*-deprecation` command line flags only affect warnings that use the name
Apr 12, 2018
Apr 12, 2018
448
`'DeprecationWarning'`.
Mar 24, 2016
Mar 24, 2016
449
Nov 16, 2016
Nov 16, 2016
450
#### Emitting custom warnings
451
452
See the [`process.emitWarning()`][process_emit_warning] method for issuing
453
custom or application-specific warnings.
454
Jun 6, 2016
Jun 6, 2016
455
### Signal Events
Feb 27, 2012
Feb 27, 2012
456
457
<!--type=event-->
Sep 19, 2013
Sep 19, 2013
458
<!--name=SIGINT, SIGHUP, etc.-->
Oct 28, 2010
Oct 28, 2010
459
Jun 6, 2016
Jun 6, 2016
460
Signal events will be emitted when the Node.js process receives a signal. Please
Oct 27, 2016
Oct 27, 2016
461
refer to signal(7) for a listing of standard POSIX signal names such as
Apr 12, 2018
Apr 12, 2018
462
`'SIGINT'`, `'SIGHUP'`, etc.
Jun 6, 2016
Jun 6, 2016
463
Dec 18, 2019
Dec 18, 2019
464
Signals are not available on [`Worker`][] threads.
465
Nov 22, 2017
Nov 22, 2017
466
The signal handler will receive the signal's name (`'SIGINT'`,
467
`'SIGTERM'`, etc.) as the first argument.
468
Jun 6, 2016
Jun 6, 2016
469
The name of each event will be the uppercase common name for the signal (e.g.
470
`'SIGINT'` for `SIGINT` signals).
Oct 28, 2010
Oct 28, 2010
471
Jan 21, 2016
Jan 21, 2016
472
```js
Jun 6, 2016
Jun 6, 2016
473
// Begin reading from stdin so the process does not exit.
Jan 21, 2016
Jan 21, 2016
474
process.stdin.resume();
Oct 28, 2010
Oct 28, 2010
475
Jan 21, 2016
Jan 21, 2016
476
process.on('SIGINT', () => {
Apr 4, 2018
Apr 4, 2018
477
console.log('Received SIGINT. Press Control-D to exit.');
Jan 21, 2016
Jan 21, 2016
478
});
Nov 22, 2017
Nov 22, 2017
479
480
// Using a single function to handle multiple signals
481
function handle(signal) {
482
console.log(`Received ${signal}`);
483
}
484
485
process.on('SIGINT', handle);
486
process.on('SIGTERM', handle);
Jan 21, 2016
Jan 21, 2016
487
```
Oct 28, 2010
Oct 28, 2010
488
Apr 12, 2018
Apr 12, 2018
489
* `'SIGUSR1'` is reserved by Node.js to start the [debugger][]. It's possible to
Apr 3, 2018
Apr 3, 2018
490
install a listener but doing so might interfere with the debugger.
Apr 12, 2018
Apr 12, 2018
491
* `'SIGTERM'` and `'SIGINT'` have default handlers on non-Windows platforms that
Jan 4, 2018
Jan 4, 2018
492
reset the terminal mode before exiting with code `128 + signal number`. If one
493
of these signals has a listener installed, its default behavior will be
Mar 3, 2016
Mar 3, 2016
494
removed (Node.js will no longer exit).
Apr 12, 2018
Apr 12, 2018
495
* `'SIGPIPE'` is ignored by default. It can have a listener installed.
496
* `'SIGHUP'` is generated on Windows when the console window is closed, and on
Nov 14, 2018
Nov 14, 2018
497
other platforms under various similar conditions. See signal(7). It can have a
Aug 23, 2015
Aug 23, 2015
498
listener installed, however Node.js will be unconditionally terminated by
Feb 7, 2015
Feb 7, 2015
499
Windows about 10 seconds later. On non-Windows platforms, the default
Sep 10, 2015
Sep 10, 2015
500
behavior of `SIGHUP` is to terminate Node.js, but once a listener has been
501
installed its default behavior will be removed.
Apr 12, 2018
Apr 12, 2018
502
* `'SIGTERM'` is not supported on Windows, it can be listened on.
503
* `'SIGINT'` from the terminal is supported on all platforms, and can usually be
Feb 23, 2018
Feb 23, 2018
504
generated with `<Ctrl>+C` (though this may be configurable). It is not
505
generated when terminal raw mode is enabled.
Apr 12, 2018
Apr 12, 2018
506
* `'SIGBREAK'` is delivered on Windows when `<Ctrl>+<Break>` is pressed, on
Jun 6, 2016
Jun 6, 2016
507
non-Windows platforms it can be listened on, but there is no way to send or
508
generate it.
Apr 12, 2018
Apr 12, 2018
509
* `'SIGWINCH'` is delivered when the console has been resized. On Windows, this
Apr 20, 2016
Apr 20, 2016
510
will only happen on write to the console when the cursor is being moved, or
Mar 3, 2016
Mar 3, 2016
511
when a readable tty is used in raw mode.
Apr 12, 2018
Apr 12, 2018
512
* `'SIGKILL'` cannot have a listener installed, it will unconditionally
513
terminate Node.js on all platforms.
514
* `'SIGSTOP'` cannot have a listener installed.
515
* `'SIGBUS'`, `'SIGFPE'`, `'SIGSEGV'` and `'SIGILL'`, when not raised
516
artificially using kill(2), inherently leave the process in a state from
517
which it is not safe to attempt to call JS listeners. Doing so might lead to
518
the process hanging in an endless loop, since listeners attached using
519
`process.on()` are called asynchronously and therefore unable to correct the
520
underlying problem.
Jan 16, 2014
Jan 16, 2014
521
Feb 8, 2018
Feb 8, 2018
522
Windows does not support sending signals, but Node.js offers some emulation
523
with [`process.kill()`][], and [`subprocess.kill()`][]. Sending signal `0` can
524
be used to test for the existence of a process. Sending `SIGINT`, `SIGTERM`,
525
and `SIGKILL` cause the unconditional termination of the target process.
Sep 16, 2015
Sep 16, 2015
526
Dec 27, 2019
Dec 27, 2019
527
## `process.abort()`
May 9, 2016
May 9, 2016
528
<!-- YAML
529
added: v0.7.0
530
-->
Oct 28, 2010
Oct 28, 2010
531
Jun 6, 2016
Jun 6, 2016
532
The `process.abort()` method causes the Node.js process to exit immediately and
Nov 13, 2015
Nov 13, 2015
533
generate a core file.
Oct 28, 2010
Oct 28, 2010
534
Jun 6, 2018
Jun 6, 2018
535
This feature is not available in [`Worker`][] threads.
536
Dec 27, 2019
Dec 27, 2019
537
## `process.allowedNodeEnvironmentFlags`
Aug 25, 2018
Aug 25, 2018
538
<!-- YAML
Sep 6, 2018
Sep 6, 2018
539
added: v10.10.0
Aug 25, 2018
Aug 25, 2018
540
-->
541
542
* {Set}
543
544
The `process.allowedNodeEnvironmentFlags` property is a special,
545
read-only `Set` of flags allowable within the [`NODE_OPTIONS`][]
546
environment variable.
547
548
`process.allowedNodeEnvironmentFlags` extends `Set`, but overrides
549
`Set.prototype.has` to recognize several different possible flag
550
representations. `process.allowedNodeEnvironmentFlags.has()` will
551
return `true` in the following cases:
552
Sep 16, 2019
Sep 16, 2019
553
* Flags may omit leading single (`-`) or double (`--`) dashes; e.g.,
Aug 25, 2018
Aug 25, 2018
554
`inspect-brk` for `--inspect-brk`, or `r` for `-r`.
Sep 16, 2019
Sep 16, 2019
555
* Flags passed through to V8 (as listed in `--v8-options`) may replace
Aug 25, 2018
Aug 25, 2018
556
one or more *non-leading* dashes for an underscore, or vice-versa;
557
e.g., `--perf_basic_prof`, `--perf-basic-prof`, `--perf_basic-prof`,
558
etc.
Sep 16, 2019
Sep 16, 2019
559
* Flags may contain one or more equals (`=`) characters; all
Aug 25, 2018
Aug 25, 2018
560
characters after and including the first equals will be ignored;
561
e.g., `--stack-trace-limit=100`.
Sep 16, 2019
Sep 16, 2019
562
* Flags *must* be allowable within [`NODE_OPTIONS`][].
Aug 25, 2018
Aug 25, 2018
563
564
When iterating over `process.allowedNodeEnvironmentFlags`, flags will
565
appear only *once*; each will begin with one or more dashes. Flags
566
passed through to V8 will contain underscores instead of non-leading
567
dashes:
568
569
```js
570
process.allowedNodeEnvironmentFlags.forEach((flag) => {
571
// -r
572
// --inspect-brk
573
// --abort_on_uncaught_exception
574
// ...
575
});
576
```
577
578
The methods `add()`, `clear()`, and `delete()` of
579
`process.allowedNodeEnvironmentFlags` do nothing, and will fail
580
silently.
581
582
If Node.js was compiled *without* [`NODE_OPTIONS`][] support (shown in
583
[`process.config`][]), `process.allowedNodeEnvironmentFlags` will
584
contain what *would have* been allowable.
585
Dec 27, 2019
Dec 27, 2019
586
## `process.arch`
May 9, 2016
May 9, 2016
587
<!-- YAML
588
added: v0.5.0
589
-->
Feb 18, 2014
Feb 18, 2014
590
Mar 8, 2017
Mar 8, 2017
591
* {string}
Nov 11, 2016
Nov 11, 2016
592
Oct 26, 2019
Oct 26, 2019
593
The operating system CPU architecture for which the Node.js binary was compiled.
594
Possible values are: `'arm'`, `'arm64'`, `'ia32'`, `'mips'`,`'mipsel'`, `'ppc'`,
595
`'ppc64'`, `'s390'`, `'s390x'`, `'x32'`, and `'x64'`.
Feb 18, 2014
Feb 18, 2014
596
Jan 21, 2016
Jan 21, 2016
597
```js
Jun 6, 2016
Jun 6, 2016
598
console.log(`This processor architecture is ${process.arch}`);
Jan 21, 2016
Jan 21, 2016
599
```
Oct 28, 2010
Oct 28, 2010
600
Dec 27, 2019
Dec 27, 2019
601
## `process.argv`
May 9, 2016
May 9, 2016
602
<!-- YAML
603
added: v0.1.27
604
-->
Oct 28, 2010
Oct 28, 2010
605
Apr 9, 2018
Apr 9, 2018
606
* {string[]}
Nov 11, 2016
Nov 11, 2016
607
Jul 4, 2016
Jul 4, 2016
608
The `process.argv` property returns an array containing the command line
Jun 6, 2016
Jun 6, 2016
609
arguments passed when the Node.js process was launched. The first element will
Oct 4, 2019
Oct 4, 2019
610
be [`process.execPath`][]. See `process.argv0` if access to the original value
611
of `argv[0]` is needed. The second element will be the path to the JavaScript
Aug 8, 2016
Aug 8, 2016
612
file being executed. The remaining elements will be any additional command line
613
arguments.
Jun 6, 2016
Jun 6, 2016
614
615
For example, assuming the following script for `process-args.js`:
Oct 28, 2010
Oct 28, 2010
616
Jan 21, 2016
Jan 21, 2016
617
```js
618
// print process.argv
Jun 6, 2016
Jun 6, 2016
619
process.argv.forEach((val, index) => {
Jan 21, 2016
Jan 21, 2016
620
console.log(`${index}: ${val}`);
621
});
622
```
Oct 28, 2010
Oct 28, 2010
623
Jun 6, 2016
Jun 6, 2016
624
Launching the Node.js process as:
Oct 28, 2010
Oct 28, 2010
625
Sep 25, 2016
Sep 25, 2016
626
```console
Apr 15, 2017
Apr 15, 2017
627
$ node process-args.js one two=three four
Jun 6, 2016
Jun 6, 2016
628
```
629
630
Would generate the output:
631
632
```text
Jul 4, 2016
Jul 4, 2016
633
0: /usr/local/bin/node
Apr 15, 2017
Apr 15, 2017
634
1: /Users/mjr/work/node/process-args.js
Jan 21, 2016
Jan 21, 2016
635
2: one
636
3: two=three
637
4: four
638
```
Oct 28, 2010
Oct 28, 2010
639
Dec 27, 2019
Dec 27, 2019
640
## `process.argv0`
Aug 8, 2016
Aug 8, 2016
641
<!-- YAML
Mar 25, 2018
Mar 25, 2018
642
added: v6.4.0
Aug 8, 2016
Aug 8, 2016
643
-->
644
Mar 8, 2017
Mar 8, 2017
645
* {string}
Nov 11, 2016
Nov 11, 2016
646
Aug 8, 2016
Aug 8, 2016
647
The `process.argv0` property stores a read-only copy of the original value of
648
`argv[0]` passed when Node.js starts.
649
Sep 25, 2016
Sep 25, 2016
650
```console
Aug 8, 2016
Aug 8, 2016
651
$ bash -c 'exec -a customArgv0 ./node'
652
> process.argv[0]
653
'/Volumes/code/external/node/out/Release/node'
654
> process.argv0
655
'customArgv0'
656
```
657
Dec 27, 2019
Dec 27, 2019
658
## `process.channel`
Nov 2, 2016
Nov 2, 2016
659
<!-- YAML
Nov 8, 2016
Nov 8, 2016
660
added: v7.1.0
Jan 3, 2020
Jan 3, 2020
661
changes:
662
- version: REPLACEME
663
pr-url: https://github.com/nodejs/node/pull/30165
664
description: The object no longer accidentally exposes native C++ bindings.
Nov 2, 2016
Nov 2, 2016
665
-->
666
Mar 24, 2018
Mar 24, 2018
667
* {Object}
668
Nov 2, 2016
Nov 2, 2016
669
If the Node.js process was spawned with an IPC channel (see the
670
[Child Process][] documentation), the `process.channel`
671
property is a reference to the IPC channel. If no IPC channel exists, this
672
property is `undefined`.
673
Jan 3, 2020
Jan 3, 2020
674
### `process.channel.ref()`
675
<!-- YAML
676
added: v7.1.0
677
-->
678
679
This method makes the IPC channel keep the event loop of the process
680
running if `.unref()` has been called before.
681
682
Typically, this is managed through the number of `'disconnect'` and `'message'`
683
listeners on the `process` object. However, this method can be used to
684
explicitly request a specific behavior.
685
686
### `process.channel.unref()`
687
<!-- YAML
688
added: v7.1.0
689
-->
690
691
This method makes the IPC channel not keep the event loop of the process
692
running, and lets it finish even while the channel is open.
693
694
Typically, this is managed through the number of `'disconnect'` and `'message'`
695
listeners on the `process` object. However, this method can be used to
696
explicitly request a specific behavior.
697
Dec 27, 2019
Dec 27, 2019
698
## `process.chdir(directory)`
May 9, 2016
May 9, 2016
699
<!-- YAML
700
added: v0.1.17
701
-->
Oct 28, 2010
Oct 28, 2010
702
Mar 2, 2017
Mar 2, 2017
703
* `directory` {string}
Jun 6, 2016
Jun 6, 2016
704
705
The `process.chdir()` method changes the current working directory of the
706
Node.js process or throws an exception if doing so fails (for instance, if
707
the specified `directory` does not exist).
Oct 28, 2010
Oct 28, 2010
708
Jan 21, 2016
Jan 21, 2016
709
```js
710
console.log(`Starting directory: ${process.cwd()}`);
711
try {
712
process.chdir('/tmp');
713
console.log(`New directory: ${process.cwd()}`);
Apr 24, 2017
Apr 24, 2017
714
} catch (err) {
Apr 15, 2017
Apr 15, 2017
715
console.error(`chdir: ${err}`);
Jan 21, 2016
Jan 21, 2016
716
}
717
```
Oct 28, 2010
Oct 28, 2010
718
Jun 6, 2018
Jun 6, 2018
719
This feature is not available in [`Worker`][] threads.
720
Dec 27, 2019
Dec 27, 2019
721
## `process.config`
May 9, 2016
May 9, 2016
722
<!-- YAML
723
added: v0.7.7
724
-->
Oct 28, 2010
Oct 28, 2010
725
Nov 11, 2016
Nov 11, 2016
726
* {Object}
727
May 2, 2018
May 2, 2018
728
The `process.config` property returns an `Object` containing the JavaScript
Jun 6, 2016
Jun 6, 2016
729
representation of the configure options used to compile the current Node.js
730
executable. This is the same as the `config.gypi` file that was produced when
731
running the `./configure` script.
Aug 19, 2013
Aug 19, 2013
732
Nov 13, 2015
Nov 13, 2015
733
An example of the possible output looks like:
Aug 19, 2013
Aug 19, 2013
734
Jul 5, 2017
Jul 5, 2017
735
<!-- eslint-skip -->
Apr 24, 2017
Apr 24, 2017
736
```js
Jan 21, 2016
Jan 21, 2016
737
{
738
target_defaults:
739
{ cflags: [],
740
default_configuration: 'Release',
741
defines: [],
742
include_dirs: [],
743
libraries: [] },
744
variables:
745
{
746
host_arch: 'x64',
Sep 5, 2019
Sep 5, 2019
747
napi_build_version: 5,
Jan 21, 2016
Jan 21, 2016
748
node_install_npm: 'true',
749
node_prefix: '',
750
node_shared_cares: 'false',
751
node_shared_http_parser: 'false',
752
node_shared_libuv: 'false',
753
node_shared_zlib: 'false',
754
node_use_dtrace: 'false',
755
node_use_openssl: 'true',
756
node_shared_openssl: 'false',
757
strict_aliasing: 'true',
758
target_arch: 'x64',
Mar 28, 2019
Mar 28, 2019
759
v8_use_snapshot: 1
Jan 21, 2016
Jan 21, 2016
760
}
761
}
762
```
Dec 15, 2011
Dec 15, 2011
763
Feb 8, 2018
Feb 8, 2018
764
The `process.config` property is **not** read-only and there are existing
765
modules in the ecosystem that are known to extend, modify, or entirely replace
766
the value of `process.config`.
Apr 21, 2016
Apr 21, 2016
767
Dec 27, 2019
Dec 27, 2019
768
## `process.connected`
May 9, 2016
May 9, 2016
769
<!-- YAML
770
added: v0.7.2
771
-->
Dec 15, 2011
Dec 15, 2011
772
Mar 8, 2017
Mar 8, 2017
773
* {boolean}
Nov 11, 2016
Nov 11, 2016
774
Jun 6, 2016
Jun 6, 2016
775
If the Node.js process is spawned with an IPC channel (see the [Child Process][]
776
and [Cluster][] documentation), the `process.connected` property will return
777
`true` so long as the IPC channel is connected and will return `false` after
778
`process.disconnect()` is called.
Oct 28, 2010
Oct 28, 2010
779
Jun 6, 2016
Jun 6, 2016
780
Once `process.connected` is `false`, it is no longer possible to send messages
781
over the IPC channel using `process.send()`.
Oct 28, 2010
Oct 28, 2010
782
Dec 27, 2019
Dec 27, 2019
783
## `process.cpuUsage([previousValue])`
May 20, 2016
May 20, 2016
784
<!-- YAML
785
added: v6.1.0
786
-->
Apr 29, 2016
Apr 29, 2016
787
Aug 22, 2016
Aug 22, 2016
788
* `previousValue` {Object} A previous return value from calling
Jun 6, 2016
Jun 6, 2016
789
`process.cpuUsage()`
Nov 13, 2016
Nov 13, 2016
790
* Returns: {Object}
Sep 4, 2019
Sep 4, 2019
791
* `user` {integer}
792
* `system` {integer}
Jun 6, 2016
Jun 6, 2016
793
794
The `process.cpuUsage()` method returns the user and system CPU time usage of
795
the current process, in an object with properties `user` and `system`, whose
796
values are microsecond values (millionth of a second). These values measure time
797
spent in user and system code respectively, and may end up being greater than
798
actual elapsed time if multiple CPU cores are performing work for this process.
Apr 29, 2016
Apr 29, 2016
799
800
The result of a previous call to `process.cpuUsage()` can be passed as the
801
argument to the function, to get a diff reading.
802
803
```js
804
const startUsage = process.cpuUsage();
805
// { user: 38579, system: 6986 }
806
807
// spin the CPU for 500 milliseconds
808
const now = Date.now();
809
while (Date.now() - now < 500);
810
811
console.log(process.cpuUsage(startUsage));
812
// { user: 514883, system: 11226 }
813
```
814
Dec 27, 2019
Dec 27, 2019
815
## `process.cwd()`
May 9, 2016
May 9, 2016
816
<!-- YAML
817
added: v0.1.8
818
-->
Oct 28, 2010
Oct 28, 2010
819
Mar 8, 2017
Mar 8, 2017
820
* Returns: {string}
Nov 11, 2016
Nov 11, 2016
821
Jun 6, 2016
Jun 6, 2016
822
The `process.cwd()` method returns the current working directory of the Node.js
823
process.
Oct 28, 2010
Oct 28, 2010
824
Jan 21, 2016
Jan 21, 2016
825
```js
826
console.log(`Current directory: ${process.cwd()}`);
827
```
Aug 31, 2019
Aug 31, 2019
828
Dec 27, 2019
Dec 27, 2019
829
## `process.debugPort`
Feb 26, 2018
Feb 26, 2018
830
<!-- YAML
831
added: v0.7.2
832
-->
Sep 9, 2019
Sep 9, 2019
833
Feb 26, 2018
Feb 26, 2018
834
* {number}
Oct 28, 2010
Oct 28, 2010
835
Feb 18, 2020
Feb 18, 2020
836
The port used by the Node.js debugger when enabled.
Feb 26, 2018
Feb 26, 2018
837
838
```js
839
process.debugPort = 5858;
840
```
Aug 31, 2019
Aug 31, 2019
841
Dec 27, 2019
Dec 27, 2019
842
## `process.disconnect()`
May 9, 2016
May 9, 2016
843
<!-- YAML
844
added: v0.7.2
845
-->
Oct 28, 2010
Oct 28, 2010
846
Jun 6, 2016
Jun 6, 2016
847
If the Node.js process is spawned with an IPC channel (see the [Child Process][]
848
and [Cluster][] documentation), the `process.disconnect()` method will close the
849
IPC channel to the parent process, allowing the child process to exit gracefully
850
once there are no other connections keeping it alive.
Oct 28, 2010
Oct 28, 2010
851
Jun 22, 2019
Jun 22, 2019
852
The effect of calling `process.disconnect()` is the same as calling
853
[`ChildProcess.disconnect()`][] from the parent process.
Oct 28, 2010
Oct 28, 2010
854
Jun 6, 2016
Jun 6, 2016
855
If the Node.js process was not spawned with an IPC channel,
856
`process.disconnect()` will be `undefined`.
Oct 28, 2010
Oct 28, 2010
857
Dec 27, 2019
Dec 27, 2019
858
## `process.dlopen(module, filename[, flags])`
Sep 8, 2017
Sep 8, 2017
859
<!-- YAML
860
added: v0.1.16
861
changes:
Oct 31, 2017
Oct 31, 2017
862
- version: v9.0.0
Sep 8, 2017
Sep 8, 2017
863
pr-url: https://github.com/nodejs/node/pull/12794
864
description: Added support for the `flags` argument.
865
-->
866
867
* `module` {Object}
868
* `filename` {string}
Apr 4, 2018
Apr 4, 2018
869
* `flags` {os.constants.dlopen} **Default:** `os.constants.dlopen.RTLD_LAZY`
Sep 8, 2017
Sep 8, 2017
870
871
The `process.dlopen()` method allows to dynamically load shared
872
objects. It is primarily used by `require()` to load
873
C++ Addons, and should not be used directly, except in special
874
cases. In other words, [`require()`][] should be preferred over
875
`process.dlopen()`, unless there are specific reasons.
876
877
The `flags` argument is an integer that allows to specify dlopen
878
behavior. See the [`os.constants.dlopen`][] documentation for details.
879
880
If there are specific reasons to use `process.dlopen()` (for instance,
881
to specify dlopen flags), it's often useful to use [`require.resolve()`][]
882
to look up the module's path.
883
Feb 8, 2018
Feb 8, 2018
884
An important drawback when calling `process.dlopen()` is that the `module`
885
instance must be passed. Functions exported by the C++ Addon will be accessible
886
via `module.exports`.
Sep 8, 2017
Sep 8, 2017
887
888
The example below shows how to load a C++ Addon, named as `binding`,
889
that exports a `foo` function. All the symbols will be loaded before
890
the call returns, by passing the `RTLD_NOW` constant. In this example
891
the constant is assumed to be available.
892
893
```js
894
const os = require('os');
895
process.dlopen(module, require.resolve('binding'),
896
os.constants.dlopen.RTLD_NOW);
897
module.exports.foo();
898
```
899
Dec 27, 2019
Dec 27, 2019
900
## `process.emitWarning(warning[, options])`
May 1, 2017
May 1, 2017
901
<!-- YAML
Mar 25, 2018
Mar 25, 2018
902
added: v8.0.0
May 1, 2017
May 1, 2017
903
-->
904
905
* `warning` {string|Error} The warning to emit.
906
* `options` {Object}
May 2, 2018
May 2, 2018
907
* `type` {string} When `warning` is a `String`, `type` is the name to use
Apr 9, 2018
Apr 9, 2018
908
for the *type* of warning being emitted. **Default:** `'Warning'`.
May 1, 2017
May 1, 2017
909
* `code` {string} A unique identifier for the warning instance being emitted.
May 2, 2018
May 2, 2018
910
* `ctor` {Function} When `warning` is a `String`, `ctor` is an optional
Apr 4, 2018
Apr 4, 2018
911
function used to limit the generated stack trace. **Default:**
912
`process.emitWarning`.
May 1, 2017
May 1, 2017
913
* `detail` {string} Additional text to include with the error.
914
915
The `process.emitWarning()` method can be used to emit custom or application
916
specific process warnings. These can be listened for by adding a handler to the
Apr 12, 2018
Apr 12, 2018
917
[`'warning'`][process_warning] event.
May 1, 2017
May 1, 2017
918
919
```js
920
// Emit a warning with a code and additional detail.
921
process.emitWarning('Something happened!', {
922
code: 'MY_WARNING',
923
detail: 'This is some additional information'
924
});
925
// Emits:
926
// (node:56338) [MY_WARNING] Warning: Something happened!
927
// This is some additional information
928
```
929
930
In this example, an `Error` object is generated internally by
931
`process.emitWarning()` and passed through to the
Apr 12, 2018
Apr 12, 2018
932
[`'warning'`][process_warning] handler.
May 1, 2017
May 1, 2017
933
934
```js
935
process.on('warning', (warning) => {
936
console.warn(warning.name); // 'Warning'
937
console.warn(warning.message); // 'Something happened!'
938
console.warn(warning.code); // 'MY_WARNING'
939
console.warn(warning.stack); // Stack trace
940
console.warn(warning.detail); // 'This is some additional information'
941
});
942
```
943
944
If `warning` is passed as an `Error` object, the `options` argument is ignored.
945
Dec 27, 2019
Dec 27, 2019
946
## `process.emitWarning(warning[, type[, code]][, ctor])`
May 9, 2016
May 9, 2016
947
<!-- YAML
948
added: v6.0.0
949
-->
Mar 24, 2016
Mar 24, 2016
950
Mar 8, 2017
Mar 8, 2017
951
* `warning` {string|Error} The warning to emit.
May 2, 2018
May 2, 2018
952
* `type` {string} When `warning` is a `String`, `type` is the name to use
Apr 9, 2018
Apr 9, 2018
953
for the *type* of warning being emitted. **Default:** `'Warning'`.
Mar 2, 2017
Mar 2, 2017
954
* `code` {string} A unique identifier for the warning instance being emitted.
May 2, 2018
May 2, 2018
955
* `ctor` {Function} When `warning` is a `String`, `ctor` is an optional
Apr 4, 2018
Apr 4, 2018
956
function used to limit the generated stack trace. **Default:**
957
`process.emitWarning`.
Mar 24, 2016
Mar 24, 2016
958
959
The `process.emitWarning()` method can be used to emit custom or application
960
specific process warnings. These can be listened for by adding a handler to the
Apr 12, 2018
Apr 12, 2018
961
[`'warning'`][process_warning] event.
Mar 24, 2016
Mar 24, 2016
962
963
```js
May 1, 2017
May 1, 2017
964
// Emit a warning using a string.
Mar 24, 2016
Mar 24, 2016
965
process.emitWarning('Something happened!');
Nov 16, 2016
Nov 16, 2016
966
// Emits: (node: 56338) Warning: Something happened!
Mar 24, 2016
Mar 24, 2016
967
```
968
Jul 14, 2016
Jul 14, 2016
969
```js
May 1, 2017
May 1, 2017
970
// Emit a warning using a string and a type.
Mar 24, 2016
Mar 24, 2016
971
process.emitWarning('Something Happened!', 'CustomWarning');
Nov 16, 2016
Nov 16, 2016
972
// Emits: (node:56338) CustomWarning: Something Happened!
Mar 24, 2016
Mar 24, 2016
973
```
974
Jan 30, 2017
Jan 30, 2017
975
```js
976
process.emitWarning('Something happened!', 'CustomWarning', 'WARN001');
Apr 15, 2017
Apr 15, 2017
977
// Emits: (node:56338) [WARN001] CustomWarning: Something happened!
Jan 30, 2017
Jan 30, 2017
978
```
979
Mar 24, 2016
Mar 24, 2016
980
In each of the previous examples, an `Error` object is generated internally by
Apr 12, 2018
Apr 12, 2018
981
`process.emitWarning()` and passed through to the [`'warning'`][process_warning]
982
handler.
Mar 24, 2016
Mar 24, 2016
983
Jul 14, 2016
Jul 14, 2016
984
```js
Mar 24, 2016
Mar 24, 2016
985
process.on('warning', (warning) => {
986
console.warn(warning.name);
987
console.warn(warning.message);
Jan 30, 2017
Jan 30, 2017
988
console.warn(warning.code);
Mar 24, 2016
Mar 24, 2016
989
console.warn(warning.stack);
990
});
991
```
992
993
If `warning` is passed as an `Error` object, it will be passed through to the
Apr 12, 2018
Apr 12, 2018
994
`'warning'` event handler unmodified (and the optional `type`,
Jan 30, 2017
Jan 30, 2017
995
`code` and `ctor` arguments will be ignored):
Mar 24, 2016
Mar 24, 2016
996
Jul 14, 2016
Jul 14, 2016
997
```js
May 1, 2017
May 1, 2017
998
// Emit a warning using an Error object.
999
const myWarning = new Error('Something happened!');
Jan 30, 2017
Jan 30, 2017
1000
// Use the Error name property to specify the type name