Raspberry Pi + NodeJS
How to setup Node.js on a Raspberry Pi and run a node script on boot

How to set up a Node.js-environment on your Raspberry Pi and have it run on boot.

1. Set up your Raspberry Pi

Running a basic setup of Raspbian on a Raspberry Pi is very easy and clearly explained in their Getting Started Guide.

Download NOOBS, extract it on your SD, plug it in and turn on the Raspberry Pi. When prompted, choose to install Raspbian and let it run.

2. Install Node.js

Make sure you have internet connection and open up the terminal on the RPi.

Installing an ARM-version of Node has become very easy:

wget http://node-arm.herokuapp.com/node_latest_armhf.deb 
sudo dpkg -i node_latest_armhf.deb

That's it, basically. It shouldn't take too long to download and install.

To make sure it ran correctly, run node -v. It should return the current version.

Running npm -v or npm --version still gave me the known Illegal instruction error. However, installing modules with npm install works.

3. Write your Node.js magic

You can do anything here. I used it to take a picture every 3 minutes and have it sent to a remote server.

4. Test your script

Run node app.js and make sure it doesn't give any errors.

5. Make it run on boot

This is the hardest part if you don't really know what you're doing.

You can define things to run on boot in /etc/rc.local. In that shell script, you don't have the same path as when you log in, so just running node app.js won't do the trick.

I tried a lot of different things that all gave errors like Illegal instruction or Permission denied or File not found.

What does work, is running one command as the default pi user. Because that user does have node in his path, the command is known.

su pi -c 'node /home/pi/server.js < /dev/null &'

I suggest using an absolute path to your Node.js-file just to make sure.

Wrapping up

Running this will try to install Node.js and create an empty app.js file in your home directory that will run whenever you boot your Raspberry Pi.

wget http://node-arm.herokuapp.com/node_latest_armhf.deb 
sudo dpkg -i node_latest_armhf.deb
touch /home/pi/app.js
su pi -c 'node /home/pi/app.js < /dev/null &'

Written by Pieter Beulque