Vipin
Vipin

Reputation: 153

is there any difference between process.exitcode and process.exit() in node?

What is the difference between process.exitcode and process.exit() ? If I use process.exitode = 1 and process.exit(1) does this create any difference or its just a alternate way to do ?

Upvotes: 2

Views: 3930

Answers (2)

Dennis
Dennis

Reputation: 59529

Yes, there is a difference.

  • process.exitCode only sets the exit code that will be used when the process eventually exits. It does NOT tell the process to exit, only what code to use when it does.

  • process.exit([code]) will terminate the process with the given exit code, or with the value of process.exitCode if it has been set, or with the exit code 0 (success) by default.

The difference is that exit will exit as quickly as possible (after all 'exit' event listeners are called), even if there are pending async operations, including I/O operations. This can lead to surprising behaviour!

If you don't need to exit as soon as possible or if your code has a lot of async operations, it's safer to use exitCode and let the process exit gracefully when all operations have completed.

Upvotes: 3

Amadan
Amadan

Reputation: 198344

process.exitcode = 1 doesn't do anything consequential. Be careful of capitalisation.

From docs on process.exitCode:

A number which will be the process exit code, when the process either exits gracefully, or is exited via process.exit() without specifying a code.

Thus,

process.exitCode = 1;
// ...
process.exit();

is equivalent to

process.exit(1);

Notably,

process.exitCode = 1;
process.exitCode = 2;
process.exit();

is not equivalent to

process.exit(1);
process.exit(2);

Also,

process.exitCode = 1;
console.log("bye");
process.exit();

is not equivalent to

process.exit(1);
console.log("bye");

Upvotes: 2

Related Questions