Running such software on a generic server is all well and good but, naturally, I want to be able to build systems using node.js and Raspberry Pi. I looked around and found an article from “Josh On Design” which got me part of the way, but it did seem a bit vague about a couple of the steps, so I thought I’d describe how I got things set up for a project I’m currently working on.
As always, the first step is to get a Raspbian image on an SD card. I don’t think node.js is particularly picky, and should run on any reasonably recent version. For this project I started with a clean SD card with the latest Raspbian version (2014-01-07 at the time of writing). To install this software you will need a command line. I prefer to connect to the Raspberry Pi over ssh (using my favourite tool MobaXterm), but if you want to use a keyboard and screen attached to the Raspberry Pi you will need to open a terminal window.
If you are concerned that your Raspbian might be getting a bit old, you may want to refresh it using:
sudo apt-get update sudo apt-get upgrade
This is usually a good idea, but if you have specific versions of other software installed, or are just in a hurry, you can skip to the next step.
As Josh points out, compiling a modern programming language and toolset from scratch takes a long time on a little Raspberry Pi, so the node.js team are now providing downloadable versions pre-built for the Pi. Unfortunately, they don’t seem to build these for every released version, so you can’t always just grab the latest. All the downloads are available from http://nodejs.org/dist/, and I found I needed to start with
latest and work backwards through the versions until I found one with
linux-arm-pi in its name. At the time of writing, this is version 0.10.25. Once you have found a recent Raspberry Pi build, you need to download it and install it on the Pi. I decided to install it to
/opt/node as follows. Of course, if you have chosen a different version, you will need to change the names.
sudo su - cd /opt wget http://nodejs.org/dist/v0.10.25/node-v0.10.25-linux-arm-pi.tar.gz tar xvzf node-v0.10.25-linux-arm-pi.tar.gz ln -s node-v0.10.25-linux-arm-pi node chmod a+rw /opt/node/lib/node_modules chmod a+rw /opt/node/bin echo 'PATH=$PATH:/opt/node/bin' > /etc/profile.d/node.sh ^D
The ^D at the end indicates pressing Control-D to exit the root shell and return to your regular user.
Once you have done this installation, either logout and log back in, or open a new terminal session and enter
node --version which should show the version you have installed. You can also try
npm --version, which will tell you the version of npm. Now, whenever you start the system, you will have access to node and npm.
One final step is to install node-gyp. This is not strictly necessary in every case, but you will find it useful if you ever need to build and install any “native” npm modules.
npm install -g node-gyp
That should be enough for now. I will cover some useful things to do with node.js in a future article.