DaveSemm
Posts: 156
Joined: Thu Jun 21, 2012 1:16 pm

Which Node.js?

Wed Mar 04, 2015 6:36 pm

I recently discovered that node.js might be perfect for what I am trying to do (after struggling with python for a while), and decided to try it out. The problem is which one? Adafruit says this is how you install it:

Code: Select all

wget http://node-arm.herokuapp.com/node_latest_armhf.deb
nodewiz says this is how:

Code: Select all

sudo apt-get install nodejs npm
Their sample apps are different.

I'm assuming these are 2 different implementations. Which should I be using? I am setting up web services and web sites on several Pi's that let me control stuff.

Heater
Posts: 8135
Joined: Tue Jul 17, 2012 3:02 pm

Re: Which Node.js?

Wed Mar 04, 2015 6:44 pm

In my experience the version you get from "apt-get install ...." is way old. node has been changing very fast. Example codes may be different because the application interface to node has changed on occasion.

Certainly you no longer have to install npm separately, it come built in with node.

Luckily the node guys have been providing more recent versions for the Pi. Like node-v0.11.11-linux-arm-pi.tar.gz available here: http://nodejs.org/dist/v0.11.11/

If you poke around in those dist directories you may find a newer version.

Should be good to go, works for me.

User avatar
DougieLawson
Posts: 30475
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Which Node.js?

Wed Mar 04, 2015 6:50 pm

On a Raspberry Pi 2B use
curl -sL https://deb.nodesource.com/setup | sudo bash -
sudo apt-get install -y build-essential python-dev python-rpi.gpio nodejs

On a Raspberry Pi A/B/B+/A+ use
wget http://node-arm.herokuapp.com/node_0.10.36_armhf.deb
sudo dpkg -i node_0.10.36_armhf.deb
sudo apt-get install build-essential python-dev python-rpi.gpio
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

DaveSemm
Posts: 156
Joined: Thu Jun 21, 2012 1:16 pm

Re: Which Node.js?

Thu Mar 05, 2015 5:03 am

Thanks! I hope to be less of a node.js newbie by the weekend.Thanks to the adafruit article, I started flashing LEDs in no time. I hope to remote control the whole house by the time I'm done.

I see now there is an application framework called express that I'll also look into.

Also, there are apparently a few forks of node.js (io.js?). Are there any problems with node.js that I should know about?

Heater
Posts: 8135
Joined: Tue Jul 17, 2012 3:02 pm

Re: Which Node.js?

Thu Mar 05, 2015 4:52 pm

You don't need to worry about io.js.

Some guys forked node.js into io.js so they could move development along more quickly. Hopefully with time those developments will merge back into node.js.

If you want a stable system go with node.js.

rnbwd
Posts: 6
Joined: Fri Mar 20, 2015 8:15 pm

Re: Which Node.js?

Fri Mar 20, 2015 8:49 pm

Actually, iojs is currently the best option for raspberry pi - https://iojs.org. They run tests every release for armv6-raspbian-wheezy - https://jenkins-iojs.nodesource.com/job ... multi/349/ - The most recent version that works with armv6 is (v1.5.1) https://iojs.org/dist/v1.5.1/ (I'm not sure why the current build is failing on armv6) - but at least you know what works.

There's a disscustion here about using iojs on rpi2 - https://github.com/iojs/io.js/issues/1105

Also check out the roadmap: http://roadmap.iojs.org/

User avatar
r3d4
Posts: 912
Joined: Sat Jul 30, 2011 8:21 am
Location: ./

Re: Which Node.js?

Sat Mar 21, 2015 8:32 am

Take a look at ./n ...

#https://github.com/tj/n/blob/master/bin/n - Simple flavour of node/iojs binary management.

xinad
Posts: 1
Joined: Wed Apr 01, 2015 8:29 pm

Re: Which Node.js?

Wed Apr 01, 2015 9:36 pm

I use io.js 1.6.2 actually on Minibian.
Node modules which I use:
onoff, express, dot, async, body-parser, js-yaml, morgan, mocha, nodeunit

Code: Select all

root@rpi:~# node -v
v1.6.2
root@rpi:~# iojs -v
v1.6.2
root@rpi:~# npm -v
2.7.1

root@rpi:~# cat /proc/version
Linux version 3.18.10-v7+ (dc4@dc4-XPS13-9333) (gcc version 4.8.3 20140303 (prerelease) (crosstool-NG linaro-1.13.1+bzr2650 - Linaro GCC 2014.03) ) #774 SMP PREEMPT Wed Mar 25 14:10:30 GMT 2015

colinmower
Posts: 12
Joined: Thu Aug 14, 2014 8:38 pm

Re: Which Node.js?

Sun May 31, 2015 7:24 pm

Hi all

I've been following the instructions to install Node-RED on my RPi B+, but am failing during install. I have gone back to the beginning and think it could be down to some missing Node.js files...see error below. This is the final command as per Dougie's instructions in the install process - the other two worked successfully.

Does anyone know if I should be using a different command or if the files have moved somewhere else please?

Thanks
Colin

*************** Output from command *******************
pi@raspberrypi ~ $ sudo apt-get install build-essential python-dev python-rpi.gpio
Reading package lists... Done
Building dependency tree
Reading state information... Done
build-essential is already the newest version.
python-rpi.gpio is already the newest version.
The following package was automatically installed and is no longer required:
libgail-3-0
Use 'apt-get autoremove' to remove it.
The following extra packages will be installed:
libexpat1-dev libssl-dev libssl-doc python2.7-dev
The following NEW packages will be installed:
libexpat1-dev libssl-dev libssl-doc python-dev python2.7-dev
0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
Need to get 2,709 kB/31.6 MB of archives.
After this operation, 42.2 MB of additional disk space will be used.
Do you want to continue [Y/n]? Y
Err http://mirrordirector.raspbian.org/raspbian/ wheezy/main libssl-dev armhf 1.0.1e-2+rvt+deb7u13
404 Not Found
Err http://mirrordirector.raspbian.org/raspbian/ wheezy/main libssl-doc all 1.0.1e-2+rvt+deb7u13
404 Not Found
Failed to fetch http://mirrordirector.raspbian.org/rasp ... _armhf.deb 404 Not Found
Failed to fetch http://mirrordirector.raspbian.org/rasp ... 13_all.deb 404 Not Found
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?

Heater
Posts: 8135
Joined: Tue Jul 17, 2012 3:02 pm

Re: Which Node.js?

Sun May 31, 2015 9:15 pm

rnbwd,

Wow, you are right. io.js is taking good care of the raspi and racing ahead of developments.

I was nervous though that it may be so far ahead that lot's of useful modules are broken for it. Basically just worrying about advising anything other than the stability of node.js itself.

seulater
Posts: 76
Joined: Wed Jul 16, 2014 7:03 pm

Re: Which Node.js?

Mon Dec 07, 2015 1:10 pm

DougieLawson wrote:On a Raspberry Pi 2B use
curl -sL https://deb.nodesource.com/setup | sudo bash -
sudo apt-get install -y build-essential python-dev python-rpi.gpio nodejs

On a Raspberry Pi A/B/B+/A+ use
wget http://node-arm.herokuapp.com/node_0.10.36_armhf.deb
sudo dpkg -i node_0.10.36_armhf.deb
sudo apt-get install build-essential python-dev python-rpi.gpio
Thanks for the instructions, but might you have the time to clarify some things for me.
I am trying this on a Pi Zero. I am confused on a couple things

#1) I see on Node.js site the latest is 5.1.1. Why is it suggested to get it from an alternate place such as wget http://node-arm.herokuapp.com/node_0.10.36_armhf.deb ?

#2) Cannot we just grab the source from https://nodejs.org/dist/latest/ and use that ?

Thank you

User avatar
DougieLawson
Posts: 30475
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Which Node.js?

Mon Dec 07, 2015 4:44 pm

I got those instructions from the NodeRED website.

NodeRED is now included with the very latest 2015-11-21 Jessie so there MUST be a version of Node.JS already installed.

Get new Jessie and do nothing more.

Code: Select all

root@falcon ~ # which node
/usr/bin/node
root@falcon ~ # node -v
v0.10.29
root@falcon ~ #
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

Heater
Posts: 8135
Joined: Tue Jul 17, 2012 3:02 pm

Re: Which Node.js?

Mon Dec 07, 2015 7:25 pm

seulater,

I am currently using a package from https://nodejs.org/dist/....

It's version 5 point something. Whatever was latest at the time. Works very well.

You might have to be careful to get the arm6 or arm7 package depending on what PI you have.

If Raspbian Jessie is installing node version 0.10.29 then that is well out of date and only of historical interest.

By the way, that is not the source you get from https://nodejs.org/dist/ but the runnable binary.

User avatar
r3d4
Posts: 912
Joined: Sat Jul 30, 2011 8:21 am
Location: ./

Re: Which Node.js?

Mon Dec 07, 2015 9:53 pm

DougieLawson wrote:NodeRED is now included with the very latest 2015-11-21 Jessie so there MUST be a version of Node.JS already installed.
Get new Jessie and do nothing more.
Unless
you expect to use anything that might depend on a "native add-on" ...........
fivdi wrote: The variant of Node.js v0.10.29 that comes preinstalled with Raspbian Jessie 2015-11-21 is broken. It's not possible to install Node.js native add-ons with this variant of Node.js.
viewtopic.php?f=66&t=127939
Last edited by r3d4 on Mon Dec 07, 2015 10:05 pm, edited 1 time in total.

User avatar
joan
Posts: 12819
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Which Node.js?

Mon Dec 07, 2015 10:04 pm

r3d4 wrote:
DougieLawson wrote:NodeRED is now included with the very latest 2015-11-21 Jessie so there MUST be a version of Node.JS already installed.
Get new Jessie and do nothing more.
Unless
you expect to use " native add-ons" ...........
fivdi wrote: The variant of Node.js v0.10.29 that comes preinstalled with Raspbian Jessie 2015-11-21 is broken. It's not possible to install Node.js native add-ons with this variant of Node.js.
viewtopic.php?f=66&t=127939
If you want to stick to the standard repositories you would need to update to Raspbian testing. That has nodejs at version 4.2.2 which supports native add-ons. Of course moving to Raspbian testing is not something to be undertaken lightly.

seulater
Posts: 76
Joined: Wed Jul 16, 2014 7:03 pm

Re: Which Node.js?

Mon Dec 07, 2015 10:33 pm

Thanks guys. What I wound up doing was this, and so far all works well on the Zero.


Code: Select all

wget https://nodejs.org/dist/latest/node-v5.1.1-linux-armv6l.tar.gz
tar -xvf https://nodejs.org/dist/latest/node-v5.1.1-linux-armv6l.tar.gz
cd node-v5.1.1-linux-armv6l/
sudo cp -R * /usr/local/

callahanp
Posts: 5
Joined: Wed Apr 13, 2016 2:49 pm

Re: Which Node.js?

Tue May 17, 2016 10:02 pm

I tried 5.1.1, 6.1 and 0.10.29 and got errors trying the example code for rpi-gpio.

sudo node example.js
/home/pi/node_modules/rpi-gpio/rpi-gpio.js:329
return currentPins[channel] + '';
^

TypeError: Cannot read property '11' of undefined
at getPinRpi (/home/pi/node_modules/rpi-gpio/rpi-gpio.js:329:27)
at Gpio.write.output (/home/pi/node_modules/rpi-gpio/rpi-gpio.js:222:19)
at write (/home/pi/experimental/node-example/example.js:11:10)
at Object.<anonymous> (/home/pi/experimental/node-example/example.js:17:1)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:148:18)

asandford
Posts: 1787
Joined: Mon Dec 31, 2012 12:54 pm
Location: Ealing

Re: Which Node.js?

Tue May 17, 2016 10:08 pm

callahanp wrote:I tried 5.1.1, 6.1 and 0.10.29 and got errors trying the example code for rpi-gpio.

sudo node example.js
/home/pi/node_modules/rpi-gpio/rpi-gpio.js:329
return currentPins[channel] + '';
^

TypeError: Cannot read property '11' of undefined
at getPinRpi (/home/pi/node_modules/rpi-gpio/rpi-gpio.js:329:27)
at Gpio.write.output (/home/pi/node_modules/rpi-gpio/rpi-gpio.js:222:19)
at write (/home/pi/experimental/node-example/example.js:11:10)
at Object.<anonymous> (/home/pi/experimental/node-example/example.js:17:1)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:148:18)
You may need to provide a bit more information as to what you want to achieve

Heater
Posts: 8135
Joined: Tue Jul 17, 2012 3:02 pm

Re: Which Node.js?

Wed May 18, 2016 11:50 am

callahanp,

What did you actually do? Show us the code in your example.js.

All seems to work fine here.

I like to live dangerously so I just grab the latest node version from node.js.org See here:
https://nodejs.org/en/download/releases/

So for example I do this:

Code: Select all

$ cd /usr/local/
$ sudo wget https://nodejs.org/download/release/v6.2.0/node-v6.2.0-linux-armv7l.tar.gz
$ sudo tar --strip-components=1 -xvf node-v6.2.0-linux-armv7l.tar.gz
$ node -v
v6.2.0
$
Now let's try rpi-gpio...

Code: Select all

$ cd
$ mkdir temp 
$ cd temp/
$ npm install rpi-gpio
Create a file containing the first two examples from the rpi-gpio pages like so:

Code: Select all

'use strict';

var gpio = require('rpi-gpio');
 
gpio.setup(7, gpio.DIR_IN, readInput);
 
function readInput() {
    gpio.read(7, function(err, value) {
        console.log('The value is ' + value);
    });
}
 
gpio.setup(8, gpio.DIR_OUT, write);
 
function write() {
    gpio.write(8, true, function(err) {
        if (err) throw err;
        console.log('Written to pin');
    });
}
And run it:
$ sudo node temp.js
Written to pin
The value is true
Seems to work just fine.

Personally I prefer pigpio https://www.npmjs.com/package/pigpio

I would not use the nodejs that is packaged in Raspbian, it's ancient.

joerg.hampel
Posts: 2
Joined: Mon Sep 19, 2016 12:56 pm

Re: Which Node.js?

Mon Sep 19, 2016 1:06 pm

I'm experiencing the same problems when trying to call a function containing the gpio.write() function from setInterval. Somehow, the scope is messed up so that I end up with:

Code: Select all

webic-root-0 TypeError: Cannot read property '16' of undefined
webic-root-0     at getPinRpi (/var/local/webic/node_modules/rpi-gpio/rpi-gpio.js:329:27)
webic-root-0     at Gpio.write.output (/var/local/webic/node_modules/rpi-gpio/rpi-gpio.js:222:19)
webic-root-0     at setLed (/var/local/webic/webic-root/webic-root.js:198:8)
webic-root-0     at blinkYellowLed [as _repeat] (/var/local/webic/webic-root/webic-root.js:207:3)
webic-root-0     at wrapper [as _onTimeout] (timers.js:275:11)
webic-root-0     at Timer.listOnTimeout (timers.js:92:15)
When calling the very same function setLed() directly or from a socket event everything works as expected. This is the (I think) relevant part of my code:

Code: Select all

var ledState;

function getPinNumber(name) {
  switch(name){
    case "yellow":
      return configuration.yellowPin;
    case "red":
      return configuration.redPin;
  }
  return 0;
}

function setLed(data) {
  var pinNumber = getPinNumber(data.Color);
  if(pinNumber != 0) {
    gpio.write(pinNumber, data.State, function(err) {
      if (err) throw err;
      console.log('Set!');
    });
  }
}

function blinkYellowLed() {
  ledState = !ledState;
  setLed({ "Color": "yellow", "State": ledState });
}
var ledIntval = setInterval(blinkYellowLed, 500);
I understand that I'm having trouble with the scope, but I cannot work out how to fix this.

Thanks in advance for any help!

Heater
Posts: 8135
Joined: Tue Jul 17, 2012 3:02 pm

Re: Which Node.js?

Mon Sep 19, 2016 5:12 pm

joerg,

Your scope is just fine.

The problem is you don't setup the pin for output. When I add that your code runs just fine. As shown below.

See rpi-gpio instructions "Setup and read the value of a pin" here: https://www.npmjs.com/package/rpi-gpio

I have a feeling pigpio is easier to use https://www.npmjs.com/package/pigpio

Code: Select all

'use strict';

let gpio = require('rpi-gpio');

let ledState = false;

//  Set up pin and write intial value
gpio.setup(16, gpio.DIR_OUT, () => {
    gpio.write(16, ledState, (err) => {
        if (err) throw err;
        console.log(`Pin 16 initialize to ${ledState}`);
    });
});

let configuration = {
    yellowPin: 16,
    redPin: 17
};

function getPinNumber(name) {
    switch (name) {
    case 'yellow':
        return configuration.yellowPin;
    case 'red':
        return configuration.redPin;
    }
    return 0;
}

function setLed(data) {
    var pinNumber = getPinNumber(data.Color);
    if (pinNumber !== 0) {
        gpio.write(pinNumber, data.State, function(err) {
            if (err) throw err;
                console.log('Set!', pinNumber, data.State);
        });
    }
}

function blinkYellowLed() {
    ledState = !ledState;
    setLed({'Color': 'yellow', 'State': ledState});
}

var ledIntval = setInterval(blinkYellowLed, 500);

Code: Select all

pi@raspberrypi3:~$ node -v
v6.3.1
pi@raspberrypi3:~$ sudo node index.js
Pin 16 initialize to false
Set! 16 true
Set! 16 false
Set! 16 true
Set! 16 false
Set! 16 true
Set! 16 false
Set! 16 true

joerg.hampel
Posts: 2
Joined: Mon Sep 19, 2016 12:56 pm

Re: Which Node.js?

Thu Sep 22, 2016 1:36 pm

@heater, you are a beauty!

I had left out the setup in the "(I think) relevant part of my code". So, I had already implemented the setup function. I think that it was the 'use strict' together with 'let' that made the difference.

One thing that cost me some time: I was so into testing that I forgot to run node with root rights (otherwise the GPIOs cannot be accessed). Only when I threw the error not only when writing, but also in the setup function did I see what was going wrong:

Code: Select all

gpio.setup(16, gpio.DIR_OUT, (err) => {
  if (err) throw err;
    gpio.write(16, false, (err) => {
        if (err) throw err;
        // do something project-specific here...
    });
});
However, it works no. Thanks a lot!

pi2r2
Posts: 8
Joined: Wed Jan 01, 2014 5:45 am

Re: Which Node.js?

Thu Sep 22, 2016 2:54 pm

From experience, this is most reliable way to install nodejs: https://github.com/creationix/nvm

Heater
Posts: 8135
Joined: Tue Jul 17, 2012 3:02 pm

Re: Which Node.js?

Thu Sep 22, 2016 4:48 pm

Yes, nvm is a wonderful thing.

Heater
Posts: 8135
Joined: Tue Jul 17, 2012 3:02 pm

Re: Which Node.js?

Mon Oct 30, 2017 7:05 pm

I already reported that last post as SPAM.

But really, when we hire developers for JS we expect that they actually have some idea of computer science or at least programming and have more on their CV than just experience the latest flavor of the month JS library or framework.

Return to “Other languages”

Who is online

Users browsing this forum: No registered users and 2 guests