Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I exit a nodeJS script from within the script?

Tags:

node.js

Right now I have a nodeJS script that sets up a database for me. Whenever I run it in my command line, I need to ctrl-C to exit the script. I'm assuming that there is some kind of command within nodeJS to have it exit on its own when it's done, but I can't seem to find any such command by doing searches on the interwebz. Does anybody happen to know if this is possible, and if so, what the command is?

like image 938
thisissami Avatar asked Aug 12 '11 23:08

thisissami


People also ask

How do I stop a node js code from running?

Method 1: Using the Ctrl+C key This shortcut can be used to stop any running process by hitting this command on terminal.

How do you exit a JavaScript program?

Using return to exit a function in javascript Using return is the easiest way to exit a function. You can use return by itself or even return a value.

What is process exit 1 in Node JS?

Node normally exits with a 0 status code when no more async operations are pending. There are other exit codes which are described below: 1 - Uncaught Fatal Exception: There was an uncaught exception, and it was not handled by a domain or an uncaughtException event handler.

How can you exit from the REPL environment for node JS?

To exit the REPL, you can type . exit , or press CTRL+D once, or press CTRL+C twice, which will return you to the shell prompt.


2 Answers

you can exit the script by calling process.exit() (see http://nodejs.org/docs/v0.5.4/api/process.html#process.exit)

like image 191
marcelog Avatar answered Sep 17 '22 16:09

marcelog


you should be using process.exit(). You can pass any status also which will be displayed on the console log.

like image 23
Vickrant Avatar answered Sep 17 '22 16:09

Vickrant