Javascript on a Raspberry Pi – How to install Node.JS

Why install Node.JS on a Raspberry Pi ? 

Nodejs_logo_lightI’ve been using Node.JS as the backend framework for building single-page web apps recently. On top of providing the advantage of an asynchronous, event-based programming model on the backend, it means I can code in Javascript on both the frontend and the backend again – just like in the good old days coding client-server applications in C.

Raspi-PGB001-300x267

And so when I come up with an application where I want to use my Raspberry Pi as a micro web server, but one that needs more than the ability to serve static webpages, I  right away think of Node.

How to install Node.JS on your Raspbery Pi 

Installing node on the RPi is not difficult, and no longer requires patches to the standard source or even a local compile, as it did the first time i went through the exercise.
I’m using the Debian Wheezy image on a v2 RPi Model B, and the installation was straightforward. The Raspberry Pi’s CPU is based on ARMv6, and there is a release on nodejs.org especially for it – I’m currently using v0.8.21.

1. Start by getting the URL to the pre-built binaries you want to install. The latest version can be found at http://nodejs.org/download/
Click “Other release files” and get the URL of the packages named

  • node-v0.8.XXlinux-arm-pi.tar.gz,

where XX is the latest stable version number

2. You will need to install Node using the command line interface on your Raspberry Pi, so either ssh in to the Raspberry Pi from your Mac or PC as described here, or just open a terminal window if you’re using the Debian GUI desktop.

3. Download the Node tar file you identified above using wget, then go to the /user/local directory and unpack the tar file there, as shown in the following steps:

$wget http://nodejs.org/dist/v0.8.21/node-v0.8.21-linux-arm-pi.tar.gz
$cd /usr/local
$sudo tar xzvf ~/node-v0.8.21-linux-arm-pi.tar.gz --strip=1

That’s as easy as it is; you should now have a working Node.JS install !
Verify this by typing:

$ node –v

which should display the Node version number, and

$ npm -v

should show you the version of the Node Package Manager.

Now you’re ready to start coding in Javascript on your Raspberry Pi, and to use npm to download and install any other modules you need to build your web application. I will be using express, connect, socket.io and mongoose among other packages.

A working Node.JS example on the RPi

A “Hello World” Web Server

First, note down the IP address of your Raspberry Pi within your local network by executing :

$ ifconfig

In my own network, I’ve configured my Router to reserve the fixed IP address 192.168.0.22 for my Pi (identified by its MAC hardware address), rather than have a different address dynamically assigned by the Router’s DHCP service each time the Pi starts up. Check the instructions for your Router if you’re not sure how to do this.

Now create a file called helloworld.js and copy the code below for a simple web server in server-side Javascript, which in tribute to Dennis Ritchie simply responds to every incoming HTTP request on port 8000 with the text “Hello World!“.
Be sure to use the IP address of your Pi which you noted down earlier.

var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World!n');
}).listen(8000,'192.168.0.22');

console.log('Server running at http://192.168.0.22:8000/');

Start up your  RPi Web Server

To start up your new web server, execute your application using node:

$ node helloworld.js
Server running at http://192.168.0.22:8000/
$

The web server is now running continuously in the background.

Finally, using another machine on the same local network, open a web browser and navigate to the URL of your webserver:

http://192.168.0.22:8000

If you’re in luck, the text “Hello World!” should appear in your browser window !

What’s Next ?

In our next exercise, we will turn this simple webserver into a web application server providing a RESTful web API. That will open up a world of possibilities for interactions between our RPi’s environment and the rest of the world, via the internet. Next – How to build a RESTful API on a Raspberry Pi

I hope this recipe has been useful. If you have any feedback or spot any errors or omissions, feel free to leave a note in the comments section below.

18 comments

  1. Florian W.

    Thank you very much for this REALLY simple way to install nodejs on my Raspberry Pi…
    I tried to do this for about the last 3 hours and the most tutorials had about 10-20 lines of shell “code” and at the end did not work at all or just partly.. but your solution seems to be just perfect 😉

  2. mike

    I get a runtime complaint about the line in helloworld.js that contains ‘text/plain’. “text” is underscored as ‘syntax error: unexpected identifier.

  3. mike

    Actually, I had a typo in that line. But when I run the script, I dont get a prompt back. The script is running because I can pull up the page on another machine and it works as expected. But my SSH command line is stuck. If I ctl-c out to a prompt the script stops.

  4. Doug Thomason

    When I run which on node, it shows it at: /home/pi/bin/node
    I am almost sure I changed directory to /usr/local/ before running sudo tar.
    I have also installed express and npm for the RESTful tutorial. They are also located at /home/pi/bin/~. Anything wrong here?

  5. Doug Thomason

    Duh, pat me on the head. I didn’t unpack the tar where you told us to.

    Some other tutorials suggest to use /opt/node for install location. Would there be a difference with regard to access privileges between these?

  6. http://Www.slideshare.Net/theonejosephpantelv

    Hi terrific blog! Does running a blog like this require a lot of work?
    I’ve very little expertise in programming however I was
    hoping to start my own blog soon. Anyhow, if you
    have any suggestions or tips for new blog owners please
    share. I know this is off topic however I simply wanted to ask.
    Thanks a lot!

  7. Geert Vancompernolle

    There isn’t a file named node…linux-arm-pi.tar.gz located on the “Other release files” page, as mentioned in the article above. At least not when I looked on June, 11th…
    Where to find?

  8. Boxwood

    Confused by the npm -init step.
    There’s no instructions as to how it should be setup?
    Do I need a README.md file? What about scripts?
    Can I just leave it all blank and be okay?

  9. Edgar Bonet

    Hi! Just found a small typo: “node –v” does not work, you should type “node -v” instead. The character before the “v” should be U+002D HYPHEN-MINUS (the plain old ASCII hyphen) instead of U+2013 EN DASH. Regards.

  10. Lerosen

    Thanks for this very comprehensive tutorial!

    I just want to note that for me the script did not work in Microsoft Edge or Internet Explorer, if it is not my fault maybe some people will get less frustrated with this guide ^^
    Regards.

  11. Paul Steingröver

    I put my helloworld.js into the /usr/local/bin/ folder and tryed to start node by using the command.
    I always get this:

    /usr/local/bin/node: l: /usr/local/bin/node: Syntax error: “(” unexpected

    what should I do? I can’t find an Syntax error in my source code.

  12. Uche Nwoko

    Hi,
    Thanks you for this tutorial, mine worked fine, but my question is, how can I read in multiple gpio inputs?
    I just found out the I cannot read in more than the 2 inputs on the website.

    kind Regards
    Uche Nwoko

Leave a reply to ceeb Cancel reply