Skip to content

Latest commit

Β 

History

History
1916 lines (1508 loc) Β· 58 KB

File metadata and controls

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