]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/src/rules/no-process-exit.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / no-process-exit.md
CommitLineData
8f9d1d4d
DC
1---
2title: no-process-exit
8f9d1d4d
DC
3rule_type: suggestion
4---
5
eb39fafa 6
f2a92ac6 7This rule was **deprecated** in ESLint v7.0.0. Please use the corresponding rule in [`eslint-plugin-n`](https://github.com/eslint-community/eslint-plugin-n).
56c4a2cb 8
eb39fafa
DC
9The `process.exit()` method in Node.js is used to immediately stop the Node.js process and exit. This is a dangerous operation because it can occur in any method at any point in time, potentially stopping a Node.js application completely when an error occurs. For example:
10
11```js
12if (somethingBadHappened) {
13 console.error("Something bad happened!");
14 process.exit(1);
15}
16```
17
18This code could appear in any module and will stop the entire application when `somethingBadHappened` is truthy. This doesn't give the application any chance to respond to the error. It's usually better to throw an error and allow the application to handle it appropriately:
19
20```js
21if (somethingBadHappened) {
22 throw new Error("Something bad happened!");
23}
24```
25
26By throwing an error in this way, other parts of the application have an opportunity to handle the error rather than stopping the application altogether. If the error bubbles all the way up to the process without being handled, then the process will exit and a non-zero exit code will returned, so the end result is the same.
27
28If you are using `process.exit()` only for specifying the exit code, you can set [`process.exitCode`](https://nodejs.org/api/process.html#process_process_exitcode) (introduced in Node.js 0.11.8) instead.
29
30## Rule Details
31
32This rule aims to prevent the use of `process.exit()` in Node.js JavaScript. As such, it warns whenever `process.exit()` is found in code.
33
34Examples of **incorrect** code for this rule:
35
8f9d1d4d
DC
36::: incorrect
37
eb39fafa
DC
38```js
39/*eslint no-process-exit: "error"*/
40
41process.exit(1);
42process.exit(0);
43```
44
8f9d1d4d
DC
45:::
46
eb39fafa
DC
47Examples of **correct** code for this rule:
48
8f9d1d4d
DC
49::: correct
50
eb39fafa
DC
51```js
52/*eslint no-process-exit: "error"*/
53
54Process.exit();
55var exit = process.exit;
56```
57
8f9d1d4d
DC
58:::
59
eb39fafa
DC
60## When Not To Use It
61
62There may be a part of a Node.js application that is responsible for determining the correct exit code to return upon exiting. In that case, you should turn this rule off to allow proper handling of the exit code.