Using forever (NodeJS) with Cloud 9 IDE

Chintan Pathak
4 min readDec 11, 2015

--

Do check out forever for NodeJS if you haven't done it yet. It allows you to keep a node process persistent and is therefore ideal for servers, where the you want your process to never go down. If some god-forbid exception does occur, forever comes to rescue and re-spawns the process. In this post I will tell you a little about forever and how it can be used with Cloud 9 IDE.

forever has a rather simple and straight-forward command-line way of adding a process as well some API level functionalities for use with your code.

Without going into details, let us now install it in our system.

sudo npm install forever -g

The “-g” specifier makes the forever module globally accessible. Once we have it installed, just pick any of your existing applications, that you used to run like

node server.js

to

forever start server.js

If everything is installed perfectly, you should see the command prompt again with some messages from forever telling you about minimum uptime etc. The forever process is running in the background, which you can check by doing:

sudo forever list

And this will give you a list of all the processes that are forever-ed. To stop a forever process, just issue a:

forever stop server.js

And this will stop the process from being running forever.

Using forever with Cloud 9

Cloud 9 IDE is a revolutionary and beautiful browser based cloud connected and locally installable development environment that is also, infinitely customizable as it is open-source. To learn how to install it on a Raspberry Pi, read this.

Cloud 9 IDE uses a concept of “runners” for executing and debugging your files. These are nothing but configuration files with information about the run-time parameters and so on. In Cloud 9 IDE you can find the runner location as shown in the image below, along side the command, CWD and ENV blocks.

The location of current runner — Node.js

When you click this block, the whole list of (default) runners is visible, one runner each for the language that Cloud 9 IDE supports. You can select any runner you wish for your application, however choosing a “C” runner for a python application may not be that useful. Having said that, there is an option of editing an existing runner or creating a new runner altogether based on your requirements.

Using forever with Cloud 9 IDE requires us to do just that. Not to worry it is very very simple to do that. Just open the existing runner for Node.js, labelled Node.js in the runner list. This can be done by clicking the “Edit Runner” option in the runner list.

Opening a runner for editing — Node.js

This should open the last active runner for editing. Now, you can change the default runner if you want to, but what I like to do is create my own runner and leave the default as it is, just in case I do something disastrous. So, go ahead, and save this runner file called “Node.js.run” to “Node.js-forever.run” where we will make changes to make a process run with forever rather than node. Choose the default save location, which was .c9/runners/ for me on my Raspberry Pi. It can be different for you, but these will show all the custom runners, that you have created. As soon as you save the file, this name will appear in the list of runners, which you can check by clicking the “Runner “ tab in the bottom.

You can read the runner script and try and understand it. The original node script is this and the forever script is this.

The main changes to observe are:

“node”

is replaced by

“/usr/local/lib/node_modules/forever/bin/forever”

which happens to be the location of the forever executable on my Raspberry Pi. This can be different for you based on your OS, and you may ever get it working by just writing “forever” in place of the whole path.

The next and only change is the additional parameter on the next line.

“start”

which is the way to start a forever process. And we are done, just try running your existing application with this runner, and see the difference.

This option will not allow you to debug your application, but I guess you already knew that. Since the application is running in background, all the output is suppressed. This is how forever works. If you want to debug and watch the console output, just use the default Node.js runner.

BONUS : More Runner changes

What if you wanted to run more than one Node.js process simultaneously, then the current runner setting is not going to let you do it. It will err with some output like “ECONNREFUSED”, which is because of the use of the same port twice. Some little tweaks and you can get a second runner going at another port. Remember to save with a recognizable name, so you can pick it up from the list, when you want to run another Node.js application simultaneously. This script run your Node.js script on port 15452 in place of the default port of 15454, so you can run them both simultaneously. The debugger may not work with both processes, so you might have to disable it for one.

Let me know how it goes, and any other runner hacks that you have made to make life with Cloud 9 easier.

--

--