Raspberry Alpha Omega

Raspberry Pi from start to finish

Installing and using node.js on Raspberry Pi

Jun 11, 2014 - 4 minute read -

The programming language landscape is always changing, and in my "day job" as a software developer I use a wide range of languages. Over the last year or so I can remember working with Awk, C, C++, Java, JavaScript, Ruby, shell script and SQL. And that's not counting all the specialist configuration, scripting and data languages. Although there is still a lot of jobs looking after enterprise systems written in the likes of C, C++ and Java, I am also seeing more and more development being done in more dynamic languages. In particular, the rising star seems to be node.js.

If you are not familiar with node.js I recommend giving it a look. It's JavaScript, but running on a server rather than in a browser. It's free, runs on most systems, easy to work with, surprisingly fast, and has a wide array of third-party software available though its npm package management system.

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.

install javascript javascript node node.js npm


Comments

  1. Jason Finch

    I can confirm this tutorial works on the latest arch linux version for 12/6/14. I also found that the lastest node version at the time was v0.11.12 many thanks for the tutorial

  2. Frank

    Thanks for checking that out, Jason. I was a bit more cautious and stuck with the 0.10 "stable" version, but I'm glad that 0.11.12 works too.

  3. Matt

    I believe the order of "upgrade" and "update" for "sudo apt-get" should be reversed. You need to update your local database of packages before you can upgrade any installed packages with changes. Otherwise a nice tutorial.

  4. Frank

    Yes, I wondered about that. That's the way round it was in the source I based this on, and I just left it as it was.

  5. Jim

    Matt's right, it should be "update" before "upgrade".

  6. Jim

    Here's a brief reference:

  7. Frank

    OK. I'll change the article. Did you mean to paste in a URL or something in that last comment?

  8. Brice Morin

    Just a small update: It seems v0.10.28 is now the latest stable version pre-compiled for the Raspberry Pi

  9. Jake

    Why not just use "apt-get install nodejs"?

  10. geekmonster

    I keep getting this error and I cant figure out why I cant get past this. The original tutorial is from here: http://notejs.com/?p=205#comment-1740 But I cant get any feedback about my mistakes. Am I better off just installing MEAN.js ? Please Help ...(o_0)... Error: Cannot find module 'express' at Function.Module._resolveFilename (module.js:336:15) at Function.Module._load (module.js:278:25) at Module.require (module.js:365:17) at require (module.js:384:17) at Object. (/home/geekmonster/projects/node/server.js:1:84) at Module._compile (module.js:460:26) at Object.Module._extensions..js (module.js:478:10) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) at Function.Module.runMain (module.js:501:10)

  11. Frank

    This usually indicates that you have not installed the 'express' npm module. I note that on the page you mention it states:

    Next, I installed ExpressJS: npm install express

    Is it possible you accidentally skipped this step? Or possibly installed it while you were in the wrong directory? If either of these is the case you could try npm install -g express which will install it for all applications.

  12. geekmonster

    Hello Frank thanks for the reply. what had happened was: 1.) I installed express inside the opt folder instead of the home folder 2.) I was trying to start up the server outside of the node folder. Now as far as my projects folder. could I place all my IoT projects inside (side by side) in this folder? And do you have any tuts on creating my own dashboard for like lets say a different icon for each task inside and outside my house, and accessing them through by use of sockets and what not.

  13. Frank

    Node.js is pretty flexible about where you put your projects. Each Node.js project is just a folder with a typical structure inside it: a file package.json with project metadata, dependencies etc. and a folder node_modules where third-party code is installed when you use npm install. So feel free to put several of these in one "workspace". I've not done a dashboard like that myself, but I'll have a look around. Sounds like fun!

Leave a Reply

Your email address will not be published.