Installing NodeJS 5.x on Raspberry Pi

You are reading this page because you have tried installing NodeJS from the repository only to realise that the version available is 0.10 (as at 2016-04-03) but you wanted the latest stable version 5.x. Read on for the solution, tested on Pi 2.

Installing NodeJS 5.x

Referring to NodeJS guide on how to download, I followed the instructions on Debian-based linux distributions.

If you have installed NodeJS from the repository, I suggest that you purge them to get back to clean slate state:

sudo apt-get purge npm
sudo apt-get purge nodejs

You will need to install cURL if you do not have it:

sudo apt-get install -y curl

Next, run the following command to download and install NodeJS 5.x (as per the official NodeJS guide):

curl -sL | sudo -E bash -
sudo apt-get install -y nodejs

To confirm the version installed:

node -v

As at 3rd April 2016, the version I see is v5.10.0.

Hello World test

Run a simple Hello World web server on NodeJS to verify that it works.

File: server.js

var http = require('http');
var port = 3000;
http.createServer(function (req, res) {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Hello World\n');
}).listen(port, "localhost");

Run the app and return to console:

node server.js &

The Process ID (PID) will be shown. Take note of that ID, it will be different per run.

To verify, use cURL to query the node:

curl http://localhost:3000/

The output should be: Hello World

Running alongside Apache

NodeJS runs on its own port as defined by the server.js script. Normally, we do not want this exposed to public. Instead, most would prefer to have http://<domain>/api/ instead of http://<domain>:3000/.

Assuming Apache 2.x is your web server, you could edit the virtual host config to proxy the requests on that path to your NodeJS instance.

Firstly, enable the Apache proxy_http module:

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo service apache2 restart

Next, add the following lines into vhost config:

ProxyPreserveHost on
ProxyPass /api/ http://localhost:3000/
ProxyPassReverse /api/ http://localhost:3000/

For the vhost changes to take effect:

sudo service apache2 reload

To verify, use cURL to query the node again but this time via path on Apache web server:

curl http://localhost/api/

The output should be: Hello World

Support Me

If this post helped you, would you buy me a cup of tea/coffee?