nodejs · raspberry pi

Installing NodeJS on a Raspberry PI

screen-shot-2017-03-04-at-6-15-17-am

There are two ways…

The easiest is to install using the package manager.  Recently though I ran into an issue where the folks providing the package manager version forgot to include the ARMv6 build. If that’s the case while you attempt, move onto the tarball method and you’ll be up and running in no time.

With package manager…

First, install NodeJS.  At the time of this writing, I prefer NodeJS 6.  The best way to install it is through the package manager.  This site contains all the details:

https://github.com/nodesource/distributions/blob/master/README.md

The important bit is this (Note: this can take a little while):

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - &&\
sudo apt-get install -y nodejs

When package manager fails!  The tarball method…

Sometimes the folks supporting the builds have a glitch, and the ArmV6 version of the build doesn’t get published…. so it will complain with this message:

“## You appear to be running on ARMv6 hardware. Unfortunately this is not currently supported by the NodeSource Linux distributions. Please use the ‘linux-armv6l’ binary tarballs available directly from nodejs.org for Node.js v4 and later.”

Which is bunk, but here is the work around during these dark times:

wget https://nodejs.org/dist/v10.15.3/node-v10.15.3-linux-armv6l.tar.xz
tar xvf node-v10.15.3-linux-armv6l.tar.xz
cd node-v10.15.3-linux-armv6l
sudo cp -R bin/* /usr/bin/
sudo cp -R lib/* /usr/lib/
sudo apt-get update && sudo apt-get upgrade
sudo apt-get install build-essential

Ok… now to test it…

First, check to make sure you have it installed:

node --version

It should come back with the version you chose to install.

Lets go a little further…

Create a directory for a simple project.

mkdir test
cd test

Now tell npm to “initialize” that folder for you. This creates your package.json file and lets you start recording what packages you’d like to install. We’ll make a quick express server… `cause that is fun.

npm init

The defaults should all be fine… since this isn’t a long term project. This though, allows you to start recording and installing some packages your node code will need.

npm install --save express

Now… as seen on: https://expressjs.com/en/starter/hello-world.html

Create a file called ‘server.js’ like this:

touch server.js

And edit it with VIM. If you aren’t using vim yet… you should. Its the only real editor Raspberry PI and Linux folks should be using… if you aren’t one of those folks. Use nano. 😉

Add this to the file:

const express = require(‘express’)
const app = express()
const port = 3000

app.get(‘/’, (req, res) => {
res.send(‘Hello World!’)
})

app.listen(port, () => {
console.log(Example app listening on port ${port})
})

Now you can run this little bit of code like this:

node server.js

… note you’ll be stuck there waiting for that little guy to return… and it never will – you just started a server!

In a NEW terminal run this:

curl localhost:3000

And you’ll have your very own server response: “Hello World!”

Which means you’ve installed node… and taken a small step toward being a web-o-naut.

Stable diffusion’s version of a web-o-naut penguin… interesting.