When we exit command prompt when npm start is still running (Node.js), will the process end and the server stop?

StackOverflow https://stackoverflow.com/questions/23645085

Question

I'm new to node and don't really know how things will turn out.

Was it helpful?

Solution

The short answer is: yes, the process will end.

When you run a process in a command prompt (or terminal), then the command processor basically pauses while your process is running. When you shut down the command prompt, you kill whatever process is running in that prompt.

On Linux systems, there's a couple ways of keeping the program around when killing the terminal:

node server.js &

The & tells the terminal to run the job in the background. You'll notice that it returns you immediately to a prompt.

However, when you actually log out, your process will get killed. In order to prevent that, you can use the nohup command (as indicated in the comments by @YTowOnt9):

nohup node server.js &

This tells the process to ignore the HUP (hangup) signal:

From Wikipedia:

nohup is a POSIX command to ignore the HUP (hangup) signal. The HUP signal is, by convention, the way a terminal warns dependent processes of logout.

On Windows system, you can use the START command, but as far as I know there's no way to keep the program running after logging out short of turning the program into a Windows Service (which is a whole other topic):

start node server.js

If you want to prevent the creation of a new window:

start /b node server.js
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top