Raspberry Pi - Upgrading Node.js, npm and installing latest Node-Red v0.16.2

I've posted this more for myself really as a reference point to come back to as I keep forgetting how to do it, but if anyone else finds this useful then even better.

As of posting this, the latest version of raspbian was released in January 2017 still doesn't come with the latest versions of npm and node.js leaving you with the rather outdated version node 0.10.29 and npm 1.4.21

I'm not sure why they pack these images with the outdated versions, it's really annoying to have to do it yourself, and then having to scour the internet all the time, especially if you're still a noob at this like I am.

Upgrading to latest Nodejs

Firstly we need to check our current version by running the following command from the terminal

$ node -v

Next we need to run the update command, this doesn't actually update anything, it just downloads (or is supposed to) the latest package lists from the repositories.

$ sudo apt update

To find the latest version we need to run this

$ apt list nodejs

The output of which should look like this

Listing... Done

According to the result on the terminal though there was no upgrade available, even though I knew there is a Nodejs v7.5 out there. 

Running "$ sudo apt install nodejs" just told me that the latest version was already installed, which of course it wasn't. Running node -v still produced v0.10.29 as an output.

So let's get the latest version which we know to be v7.5

$ curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -

This doesn't actually install anything, it just pulls everything down from the repository, what we need to do now is to install it.

$ sudo apt install nodejs

Now this is when the latest version gets installed, afterwards we run the command again

$ node -v

Nodejs has now been updated to the latest version, and npm (although I didn't physically install it) is now npm v4.1.2 which you can find by using the command npm -v

On checking this now though I found that my already preinstalled version of Node-Red no longer worked and it no longer existed in the start menu either... So time for a reinstall.

As of writing the latest version of Node-Red is v0.16.2, use the following command in the terminal to install Node-Red.

sudo npm install -g node-red

Unlike the preinstall version of Node-Red, the new version doesn't work in the same way as before using the command node-red-start, instead you have to use the command "node-red" to get it started

The new version of Node-Red comes with a new interface which is really cool (I might cover this another time) and unlike the previous version where you had to stop and restart Node-Red you can now search for and install nodes from the GUI, and for the most part you don't have to do a restart, pretty cool. The downside I found though is that there is no option to get Node-Red to start up at boot time, so if you're like me where I use it for home automation, if there is a system crash or power failure which causes a reboot you'll find that Node-Red isn't running. Even an attempt at getting it to run via pm2 didn't work. ...but there is hope.

This next bit of wizardry comes from GitHub, just copy and paste the following lines (one at a time) into the terminal, these run as sudo so it's up to you how you want to do things.

sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/nodered.service -O /lib/systemd/system/nodered.service
sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-start -O /usr/bin/node-red-start
sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-stop -O /usr/bin/node-red-stop
sudo chmod +x /usr/bin/node-red-st*
sudo systemctl daemon-reload

These commands update the system service starter with the start and stop commands (node-red-start and node-red-stop) which are then made executable and then the final command refreshes the systemd config.

If you run the following command this will enable Node-Red to run as a service at start up as it used to when you previously had the preinstalled version, before you upgraded. 

sudo systemctl enable nodered.service

Hope this helps some of you out there, if it does then please feel free to comment below.

No comments:

Post a Comment