How to restart node server forever/automically | Nodejs forever npm library

This tutorial shows how to keep the node server alive continuously and also how to restart the node server after an error or crash.

forever npm library provides a CLI tool to keep the node server continuously running.

It restarts a server if the node server stopped because of an error.

It provides the following ways to integrate into the node application

Nodejs forever server alive

First, install the forever npm library into an application

In the terminal, run the below command

npm install forever -g

It installs forever CLI globally.

Here is a command to start the script and run it continuously

forever start main.js

you can see the output as follows process is started as a daemon process and runs in the background.

info: Running action: start
info: Forever processing file: main.js

You can see different options for the forever command.

type forever in the terminal to get available options and configurations.

forever

you can pass the options to the command line or copy them to a json file.

Create a dev.json file

{
  "append": true,
  "watch": true,
  "script": "index.js"
}

then, you can run the below command as follows

forever start dev.json

You can also check for more about forever npm🔗

You can also start forever using code.

It also provides start, stop,startall, restart,restartall, and a list of all scripts.

How do I start node on forever?

To start the node on forever, Please follow the below steps

  • usually, the node server is started with node servercode.js
  • It starts the server, if stop it using ctrl + c, you need to start it again using the node command.
  • instead, use forever start servercode.json to start forever

How do I restart the node server automatically?

To restart the node server automatically, Please follow the below steps you can use nodemon or forever npm library

  • install using npm install nodemon -g or npm install forever -g
  • instead, use one of below two commands to start automatically and reload changes
  forever start servercode.json

or

 nodemon servercode.json

How do I leave the nodeJS server on ec2 running forever?

To set up the node server on the ec2 machine, Please follow the below steps

  • first, install the pm2 npm library
  • next, set the server code with the pm2 command to start automatically
  • start a server forever

How do I restart a nodeJS server?

To start the node on the restart, please follow the below steps

  • usually, the node server is started with node servercode.js
  • It starts the server. if it stops using ctrl + c, it stops its execution you need to start it again using the node command.
  • instead, use forever start servercode.json to restart forever

How do you restart a node without downtime?

To start the node on restart without downtime, Please follow the below steps

  • usually, the node server starts with node servercode.js and started the server -if it stops using ctrl + c or closing terminal, it stops its execution you need to start it again using the node command.
  • instead, use forever start servercode.json to restart forever

Can I run NodeJS without a server?

Yes nodejs can be started without a server, You need third-party libraries such as nodemon, forever, or pm2 to start in the webserver