GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — doc: expand documentation for process.exit() · nodejs/node@b3f75ec

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • doc/api
    • process.md
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/process.md‎

Copy file name to clipboardExpand all lines: doc/api/process.md
+52-5Lines changed: 52 additions & 5 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -729,26 +729,73 @@ Example:
729729

730730
## process.exit([code])
731731

732-
Ends the process with the specified `code`. If omitted, exit uses the
733-
'success' code `0`.
732+
* `code` {Integer} The exit code. Defaults to `0`.
733+
734+
The `process.exit()` method instructs Node.js to terminate the process as
735+
quickly as possible with the specified exit `code`. If the `code` is omitted,
736+
exit uses either the 'success' code `0` or the value of `process.exitCode` if
737+
specified.
734738

735739
To exit with a 'failure' code:
736740

737741
```js
738742
process.exit(1);
739743
```
740744

741-
The shell that executed Node.js should see the exit code as 1.
745+
The shell that executed Node.js should see the exit code as `1`.
746+
747+
It is important to note that calling `process.exit()` will force the process to
748+
exit as quickly as possible *even if there are still asynchronous operations
749+
pending* that have not yet completed fully, *including* I/O operations to
750+
`process.stdout` and `process.stderr`.
751+
752+
In most situations, it is not actually necessary to call `process.exit()`
753+
explicitly. The Node.js process will exit on it's own *if there is no additional
754+
work pending* in the event loop. The `process.exitCode` property can be set to
755+
tell the process which exit code to use when the process exits gracefully.
756+
757+
For instance, the following example illustrates a *misuse* of the
758+
`process.exit()` method that could lead to data printed to stdout being
759+
truncated and lost:
760+
761+
```js
762+
// This is an example of what *not* to do:
763+
if (someConditionNotMet()) {
764+
printUsageToStdout();
765+
process.exit(1);
766+
}
767+
```
768+
769+
The reason this is problematic is because writes to `process.stdout` in Node.js
770+
are *non-blocking* and may occur over multiple ticks of the Node.js event loop.
771+
Calling `process.exit()`, however, forces the process to exit *before* those
772+
additional writes to `stdout` can be performed.
773+
774+
Rather than calling `process.exit()` directly, the code *should* set the
775+
`process.exitCode` and allow the process to exit naturally by avoiding
776+
scheduling any additional work for the event loop:
777+
778+
```js
779+
// How to properly set the exit code while letting
780+
// the process exit gracefully.
781+
if (someConditionNotMet()) {
782+
printUsageToStdout();
783+
process.exitCode = 1;
784+
}
785+
```
742786

787+
If it is necessary to terminate the Node.js process due to an error condition,
788+
throwing an *uncaught* error and allowing the process to terminate accordingly
789+
is safer than calling `process.exit()`.
743790

744791
## process.exitCode
745792

746793
A number which will be the process exit code, when the process either
747794
exits gracefully, or is exited via [`process.exit()`][] without specifying
748795
a code.
749796

750-
Specifying a code to [`process.exit(code)`][`process.exit()`] will override any previous
751-
setting of `process.exitCode`.
797+
Specifying a code to [`process.exit(code)`][`process.exit()`] will override any
798+
previous setting of `process.exitCode`.
752799

753800

754801
## process.getegid()

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.