---
title: "Running Node.js"
date: "2015-01-16"
category: "node"
slug: "running-node-js"
---

[Node.js](http://nodejs.org/) is a performant JavaScript backend built on Chrome's V8 engine. Node.js and its package manager [npm](https://www.npmjs.com/) are available on [[determining-platform-version]] (v6+) without needing to [[compiling-programs]] from source. Accounts with [[is-terminal-access-available]] are eligible to use Node.js.

## Running Node.js with Passenger

Newer hosting servers ([[determining-platform-version]]) support running Node.js through Passenger, which automatically manages launching and scaling Node.js instances to meet demand.

### Setup

Create a [[passenger-application-layout]] filesystem layout:

nodejsapp
+-- app.js  <-- main file
+-- public  <-- document root
¦   +-- .htaccess <-- htaccess control file
+-- tmp     <-- passenger control/scratch directory

Create a [[htaccess-guide]] file in `public/` (the [[where-is-site-content-served-from]]) with:

PassengerNodejs /usr/bin/node

**Note** ([[determining-platform-version]]): If the system Node version is insufficient, use [[changing-node-versions]] to install a different version. When specifying the path in `PassengerNodejs`, expand the tilde (`~`) to your [[home-directory-location]].

**Note** (v6 platforms): You may use a custom Node.js version installed under `/usr/local/bin`. Change `PassengerNodejs` from `/usr/bin/node` to `/usr/local/bin/node`.

### Connecting to a Domain

Rename your main file to `app.js` and place it in the application root as shown in the directory layout above. Connect the `public/` folder to a subdomain or domain within the [[logging-into-the-control-panel]].

To specify a different entry-point file, add `PassengerStartupFile newfile.js` to your `.htaccess` file.

### Restarting

Restart Node.js using the same [[restarting-passenger-processes]] method as with Ruby or Python scripts.

## Running Node.js Standalone

### Quickstart

Create a file called `server.js` with the following content. Replace `40201` with a [[listening-ports]] allocated to your account.

// Load the http module to create an http server.
var http = require('http');

// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
 response.writeHead(200, {"Content-Type": "text/plain"});
 response.end("Hello World\\n");
});

// Listen on port 40201, pre-allocated , IP defaults to 127.0.0.1
server.listen(40201);

// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:40201/");

Start the server:

`[myuser ~]$ node server.js Server running at http://127.0.0.1:40201/`

Verify it is working with curl:

`[myuser ~]$ curl http://127.0.0.1:40201 Hello World!`

### Persisting a Server

Use [forever](https://www.npmjs.com/package/forever) (`npm install -g forever`) or `nohup` to keep the server running after logout:

`nohup node server.js &`

### Starting on Boot

1. In the [[logging-into-the-control-panel]], navigate to **Dev** > **Task Scheduler**
2. Under **Command**, enter `node ~/server.js`
3. Under _Scheduling_, select **Server Start**
4. Click **Add**

## Installing Packages

Use npm to install packages: `npm install -g PKGNAME` where _PKGNAME_ is a package [listed on npm](https://www.npmjs.com/).

### Configuring Global Install on Older Platforms

Platforms [[determining-platform-version]] require a [.npmrc](https://docs.npmjs.com/files/npmrc) file in your home directory with these two variables:

prefix = /usr/local
link = yes

This ensures `npm install -g` installs packages under `/usr/local` and makes binaries available in your shell path.

## See Also

- [npm package repository](https://www.npmjs.com/)
- [Node.js documentation](http://nodejs.org/api/)
- [The Node Beginner Book](http://www.nodebeginner.org/)
