Node.js – Unable to update nodejs and npm on Raspbian-Raspberry Pi 3

node.jsnpmraspbian

My raspbian system is currently running Node.js v4.8.2 and am trying to install its latest version but in vain.I have tried the following solution from this post npm command not working on ubuntu (i.e uninstalling the old packages and installing new ones).When using this command sudo apt-get install -y nodejs I get this message:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
You might want to run 'apt --fix-broken install' to correct these.
The following packages have unmet dependencies:
 libiw-dev : Depends: libiw30 (= 30~pre9-8) but 30~pre9-12 is to be installed
 nodejs : Conflicts: nodejs-legacy
E: Unmet dependencies. Try 'apt --fix-broken install' with no packages (or specify a solution).

I also tried installing NPM directly using this command: sudo apt-get install npm but I get this message:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
You might want to run 'apt --fix-broken install' to correct these.
The following packages have unmet dependencies:
 libiw-dev : Depends: libiw30 (= 30~pre9-8) but 30~pre9-12 is to be installed
 npm : Depends: node-abbrev (>= 1.0.4) but it is not going to be installed
       Depends: node-ansi (>= 0.3.0-2) but it is not going to be installed
       Depends: node-ansi-color-table but it is not going to be installed
       Depends: node-archy but it is not going to be installed
       Depends: node-block-stream but it is not going to be installed
       Depends: node-fstream (>= 0.1.22) but it is not going to be installed
       Depends: node-fstream-ignore but it is not going to be installed
       Depends: node-github-url-from-git but it is not going to be installed
       Depends: node-glob (>= 3.1.21) but it is not going to be installed
       Depends: node-graceful-fs (>= 2.0.0) but it is not going to be installed
       Depends: node-inherits but it is not going to be installed
       Depends: node-ini (>= 1.1.0) but it is not going to be installed
       Depends: node-lockfile but it is not going to be installed
       Depends: node-lru-cache (>= 2.3.0) but it is not going to be installed
       Depends: node-minimatch (>= 0.2.11) but it is not going to be installed
       Depends: node-mkdirp (>= 0.3.3) but it is not going to be installed
       Depends: node-gyp (>= 0.10.9) but it is not going to be installed
       Depends: node-nopt (>= 3.0.1) but it is not going to be installed
       Depends: node-npmlog but it is not going to be installed
       Depends: node-once but it is not going to be installed
       Depends: node-osenv but it is not going to be installed
       Depends: node-read but it is not going to be installed
       Depends: node-read-package-json (>= 1.1.0) but it is not going to be installed
       Depends: node-request (>= 2.25.0) but it is not going to be installed
       Depends: node-retry but it is not going to be installed
       Depends: node-rimraf (>= 2.2.2) but it is not going to be installed
       Depends: node-semver (>= 2.1.0) but it is not going to be installed
       Depends: node-sha but it is not going to be installed
       Depends: node-slide but it is not going to be installed
       Depends: node-tar (>= 0.1.18) but it is not going to be installed
       Depends: node-underscore but it is not going to be installed
       Depends: node-which but it is not going to be installed
E: Unmet dependencies. Try 'apt --fix-broken install' with no packages (or specify a solution).

Any help would be appreciated.Thanks

Best Answer

The problem comes from this line:

nodejs : Conflicts: nodejs-legacy

You should remove all versions of node first.

(Following commands can be referenced at raspberrypi.org's community page):

pi@raspberrypi:~ $  sudo su -
root@raspberrypi:~ # apt-get remove nodered -y
root@raspberrypi:~ # apt-get remove nodejs nodejs-legacy -y
root@raspberrypi:~ # apt-get remove npm  -y # if you installed npm

Make sure you are pointed to correct source.

root@raspberrypi:~ # curl -sL https://deb.nodesource.com/setup_5.x | sudo bash -

Install, and check versions:

root@raspberrypi:~ # apt-get install nodejs -y
root@raspberrypi:~ # node -v
root@raspberrypi:~ # npm -v