How to Setup Node.js on a Raspberry PI

In this short tutorial we’ll look at how to get Node running on your Raspberry PI, run it in the background and automatically have it start when the PI reboots.

pi-node

First things first fire up a terminal or SSH into your PI and download the latest node tarball.
At the time of this writing that is v0.8.19 but of course substitute that with whatever version you choose.

$ wget http://nodejs.org/dist/v0.8.19/node-v0.8.19.tar.gz
$ tar xvzf node-v0.8.19.tar.gz
$ cd node-v0.8.19

Next you’re ready to build Node from source.
On my Model B w/ 512MB of RAM this took about an hour and a half.

$ ./configure && make && sudo make install

If everything went ok you should now have node and npm installed on your PI.

$ node --version
v0.8.19
$ npm --version
1.2.2


Cool, now let’s run a simple test app.
Create a new file and call it hello-node.js and paste the following into it:

// 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);

Now start the server –

$ node ./hello.node.js

and navigate a browser to localhost:8000 and you should see “hello world” print to the screen.

Hurrah it works!

Ok, now let’s setup a daemon to run our node server outside of our shell and automatically have it start whenever the PI reboots.

First cd into /etc/init.d/ and create a new file called node-server.sh

$ cd /etc/init.d
$ sudo nano node-server.sh

Next copy the following into it:
Note to change the path_to_node_app variable to wherever your app lives.

#! /bin/sh
# /etc/init.d/node-server
 
### BEGIN INIT INFO
# Provides:          node-server
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
### END INIT INFO
 
# change this to wherever your node app lives # 
path_to_node_app=/var/node/hello-node.js 
 
# Carry out specific functions when asked to by the system
case "$1" in
  start)
    echo "* starting node-server * "
    echo "* starting node-server * [`date`]" >> /var/log/node-server.log
    /usr/local/bin/node $path_to_node_app >> /var/log/node-server.log 2>&1&
    ;;
  stop)
    echo "* stopping node-server * "
    echo "* stopping node-server * [`date`]" >> /var/log/node-server.log
    killall /usr/local/bin/node
    ;;
  *)
    echo "Usage: /etc/init.d/node-server {start|stop}"
    exit 1
    ;;
esac
 
exit 0

Next we need to make this file executable via :

$ chmod 755 ./node-server.sh

Now let’s tell the PI to execute this script and start the server on reboot:

$ update-rc.d node-server.sh defaults

If we ever want to disable this just type :

$ update-rc.d -f node-server.sh remove

And lastly you can now start and stop your node server in the background anytime via :

# start-server #
sudo /etc/init.d/node-server.sh start
# stop-server #
sudo /etc/init.d/node-server.sh stop

I personally added aliases to these two commands in my ~/.bashrc

# start / stop node server in background #
alias nodestart='sudo /etc/init.d/node-server.sh start'
alias nodestop='sudo /etc/init.d/node-server.sh stop'
alias nodetail='tail -n100 /var/log/node-server.log'

So I can start / stop the server and view the log file simply by typing the following :

$ nodestart
$ nodestop
$ nodelog

And that’s about it. Any problems or questions feel free to post in the comments below.

  • http://robdodson.me/ Rob Dodson

    Just a heads up. If you don’t mind running a version of node that’s a few patches behind, there are precompiled binaries for the Pi. Might save you an hour or two of headaches :)

    http://nodejs.org/dist/v0.10.7/node-v0.10.7-linux-arm-pi.tar.gz seems to be the latest.

    • braitsch

      Cool! although there’s something magical about watching all that beautiful output to the console when building node from source :)

      • http://robdodson.me/ Rob Dodson

        true. plus it makes you look super badass if you’re doing it at the office :)

        • http://robdodson.me/ Rob Dodson

          Spent all day messing with those binaries only to have them not work. I guess it has to do with whichever version of raspbian they were compiled against? Following your instructions and just compiling from source was the only thing that worked for me. Thanks dude 😀

  • Harminder Singh Deol

    Just got back from work all tired and stuff, yet exited to install node.js, cause of all the youtube vedio of people controlling GPIO over the web. I am beginner to pi & javascript.
    So i was installing node.js and after about 40 min my screen went to sleep so i pressed some keys on the keyboard and stuff got written into the terminal. Before i could delete it the set up went to the next line. I got a warning saying something…. don’t remember but whole lot of lines with warning in them.
    So i just press ctrl+c, exit out of the installation
    then deleted the node-v0.8.19 folder
    and nw installing it again.
    Should i be worried!!!!!!!

  • Harminder Singh Deol

    I found an error in the post , To run the hello-node.js file, the file name needed to be corrected on the post between hello and node there is a period (.) but there should have been a (-)

    Now start the server –
    $ node ./hello.node.js