Running Node.js app as a service forever

When you start a Node.js server simply using node app.js, the server stops as soon as the shell session is killed. But if you want to keep you server running, as a server would naturally should, you need some way to keep the server proc alive.

Forever is seemingly a popular choice for running a server, as the name suggests, forever; or technically as long as you want.

While trying to install Forever, globally, via npm, it was complaining about the version number of inherits. So I did:

sudo npm install inherits -g

and then did:

sudo npm install -g forever

You are ready to run a nodejs server forever. Hence, do this:

 forever start --minUptime 1000 --spinSleepTime 1000 app.js

To make sure your new service is up

forever list

And if you want to stop, use

forever stop <PID/PID>

Node.js server on Azure

It is easy to use Node.js on a CentOS VM running on Azure, or any other linux VM on Azure. You can simply yum (with EPEL) install Node.js. Then an easy way to check if everything is working fine, a good idea is to test running a server from node. I am using this simple snippet:

var http = require('http')

var server = http.createServer(function (req, response){
  response.writeHead(200, {"Content-Type":"text/plain"});
  response.end("Hello World");


Now you can start it by typing

node app.js

To access this server that you just started, you need to access this port. This server is listening to port 8080. So you need to enable an endpoint on you Azure VM. Go to Microsoft Azure > yourazurevm > Settings > Endpoints and click add.
Then set following:
Endpoint: Any name (e.g. Http 8080)
Protocol: TCP
Public port: 8080
Private port: 8080

Now, hit okay and wait for the VM to enable this endpoint (a.k.a. port).


After this is enabled, you can now go to and see hello world.

Setting up NODE_PATH for using global packages via require(…)

Node.js intends global install for applications only. If you want to use some package in your project via require('...') the recommended way is to put them in projects root. However, you may want some packages to install globally via npm install -g package and use them on your node projects. Then, you have to make sure you have the %NODE_PATH% system variable set up to point global node_modules directory, e.g.: %USERPROFILE%\AppData\Roaming\npm\node_modules for Windows 8.