How to exit or stop a Node.js script

How to exit or stop a Node.js script

easier-docs-js-hero-image

Short Answer:

process.exit()

Long answer:

This function will end the script or process with the specified exit code upon invoking it.

If there is an error, if and you want to end the function with an exit code of -1, You can pass it as an argument to the function.

process.exit(-1);

You will be able to see the exit code from which the shell you invoked it from.

Related Info: How to stop the interactive nodejs prompt

If you are in an interactive nodejs prompt, You can also use .exit to exit the nodejs interactive prompt.

$ node
> .exit

Alternatively, Pressing Ctrl+C twice or pressing Ctrl+D once works.

Last updated on