Wednesday, January 7, 2015

How to Install Node.js on Your Raspberry Pi

Do you want to install Node.js on your Raspberry Pi? It’s actually quite a bit easier than it used to be. I just recently installed Node.js 0.8.16 on my Raspberry Pi and decided to document it. For this tutorial I’m using a setup with the latest Raspian image.
Installation of Node.Js 0.8.16
For this tutorial I’m using the Raspian “Wheezy” Image but since we’re compiling from source it shouldn’t matter too much what version you’re using.To do this, you’ll need to build Node.js from source. It’s not as bad as it sounds, and actually pretty easy. We’ll be getting the source from the downloads page at Nodejs.org and compiling it from there.
You’ll want to go into your /usr/src directory (or some temporary directory) and use wget to download the file.

1
2
cd /usr/src
sudo wget http://nodejs.org/dist/v0.8.16/node-v0.8.16.tar.gz

This will pull down the source tarball to your machine. You must have your Raspberry Pi connected to the internet, if you don’t then download the file here and put it on your Pi.

1
2
sudo tar xvzf node-v0.8.16.tar.gz
cd node-v0.8.16

Next steps will build the program:

1
2
3
sudo ./configure
sudo make
sudo make install

Note: The make process can take quite a while, well over an hour in most cases. You may want to start the build and do something else, or if you’re in a hurry you can overclock your Pi to make it build a little faster.

Testing your Node.js Install

Once it’s built, if you don’t see any errors, it should be ready to go. You can run some tests with the following command:

1
sudo make test

Not all of the tests will pass, but most of them should. For a quick web test, put the following code in a file called test.js:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 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 8000, IP defaults to 127.0.0.1
server.listen(8000);

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

And save it. Then run the following command:

1
sudo node test.js

And then point your web browser to http://127.0.0.1:8000 and you will see a hello world! message.

Summary

I hope this helps folks wanting to try out node on their Raspberry Pi. In the past it was a bit tricky and for some people compiling from source can scary. But as you can see this is a pretty straightforward process.
While it may not be the best node server to use it’s great for a development environment, as most hosting companies do not offer Node.js yet. The Raspberry Pi is designed for learning and experimenting and provides a great easy platform to do that.
So get out there and start building some node apps with non-blocking goodness!

1 comment:

  1. I was searching for loan to sort out my bills& debts, then i saw comments about Blank ATM Credit Card that can be hacked to withdraw money from any ATM machines around you . I doubted thus but decided to give it a try by contacting {skylinktechnes@yahoo.com} they responded with their guidelines on how the card works. I was assured that the card can withdraw $5,000 instant per day & was credited with $50,000 so i requested for one & paid the delivery fee to obtain the card, i was shock to see the UPS agent in my resident with a parcel{card} i signed and went back inside and confirmed the card work's after the agent left. This is no doubts because i have the card & has made used of the card. This hackers are USA based hackers set out to help people with financial freedom!! Contact these email if you wants to get rich with this Via email skylinktechnes@yahoo.com whatsapp/t: +1(213)785-1553

    ReplyDelete