What does ES6’s spread operator (…) do and what can you use it for?

Some language features are easy to guess what they do even if you’re unfamiliar with them, but it’s not immediately obvious what the ES6 spread operator ‘…’ does.

Here’s a great article that gives some practical examples of how you can use the spread operator, for example, to:

  • insert one array into another
  • copy the contents of an array
  • convert a String into an array of chars

Rendering arrays of Components with React

React doesn’t support the use of a for() loop in it’s render method. If you need to render a list of components based on data, one approach is to build a var representing the list of components in a helper function as described here.

Another approach is to use .map() to build an array of Components from an array of values, like this:

this.state.grid.map( (cell, index) => (
<CellComponent key={index} value={this.state.grid[index]}
onChange={this.handleChangeForArrayFields.bind(this, index)}/>
)
)}

In this example, this.state.grid is an array containing values to map to each rendered value attribute on my Component. Each rendered element needs to be unique, so we add the key attribute, and set it to the index from each element that comes out of the map function.

I’ll look at the onChange handler in a followon post.

Fixing npm global install permissions on MacOS

By default npm on MacOS tries to install global modules (npm install -g somemodule) to /usr/local/lib/node_modules and you get this error:

Error: EACCES: permission denied, access \'/usr/local/lib/node_modules\'

The npm docs here have a couple of steps to avoid this by telling npm to install to a location where you have have access to:

mkdir ~/.npm-global
npm config set prefix '~/.npm-global'

Edit your .profile or .bash_profile to add the new location to your PATH:

export PATH=~/.npm-global/bin:$PATH

Now you should be all set!

 

:

 

AWS IoT and Node.js on the Raspberry Pi

There are many approaches for installing node.js on the Raspberry Pi (Google and you’ll find lots of guides), presumably because for a while there didn’t seem to be any official binaries in the official apt repos so people were building and sharing their own.

I installed a version from somewhere (can’t actually remember where as it was a while back) and it doesn’t support ES6 class syntax used by some of the dependent libraries in the AWS IoT SDK:

$ node index.js
/home/pi/aws-iot-nodejs-pi-lights/node_modules/aws-iot-device-sdk/node_modules/mqtt/node_modules/websocket-stream/server.js:6
class Server extends WebSocketServer{
^^^^^
SyntaxError: Unexpected reserved word
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/home/pi/aws-iot-nodejs-pi-lights/node_modules/aws-iot-device-sdk/node_modules/mqtt/node_modules/websocket-stream/index.js:2:14)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)

The version I currently have installed is:

pi@raspberrypi:~ $ node -v
v0.10.29

Since I’m not sure where this version came from originally, (and apt-get upgrade is not finding any updates), I uninstalled:

sudo apt-get remove nodejs
sudo apt-get remove npm

Then I followed the steps in the AWS IoT SDK guide here to install using the version provided from Adafruit’s repo  (official node.js binaries for ARM are also available from nodejs.org here).

With version provided from Adafruit, this gives v 0.12.6 but unfortunately this still gives the same error with the ES6 class keyword.

$ node -v
v0.12.6

Next, lets try the ARM version from nodejs.org. There’s step by step instructions here showing how to download the tar, extract and copy to /usr/local/

Now we have:

$ node -v
v8.9.1

And now trying to run my AWS IoT node.js based app, success!