What’s new in io.js 2.0.0

in Development

This week io.js, a community fork of Node.js, released version 2.0.0. This latest version comes with many bug fixes and some very cool new features. So today I want to briefly cover 6 of these features, some of which are from the upcoming version of JavaScript (ES6 or ES2015) and are no longer behind a staging feature flag.

1. Better way to inspect Promise, Map, and Set objects

In previous versions of io.js, the output of printing Promise, Map, or Set objects to the console wasn’t very helpful. The following example using the REPL from io.js version 1.8.1 illustrates this:

> var prom = new Promise(function(){});
undefined
> prom
{}

Now let’s look at the same code run with the new version, 2.0.0:

$ node
> var prom = new Promise(function(){});
undefined
> prom
Promise { <pending> }

Now we can see that prom is a Promise object on the pending state. Here’s what it looks like for the resolved and rejected states:

$ node
> var prom2 = new Promise(function(resolve, reject){ resolve("OK"); });
undefined
> prom2
Promise { 'OK' }
> var prom3 = new Promise(function(resolve, reject){ reject("Not OK"); });
undefined
> prom3
Promise { <rejected> 'Not OK' }

While this might not seem like lot of information, it’s a huge help for developing and debugging.

2. REPL history can be persisted across sessions

The REPL now has the ability to store command history across different sessions. To enable this, we must set the NODE_REPL_HISTORY_FILE environment variable with a path to an existing user-accessible file. For example:

NODE_REPL_HISTORY_FILE='/Users/caike/.iojs_repl_history'

Now all commands typed in the REPL will be stored in /Users/caike/.iojs_repl_history as an array of strings. This means the next time we open a REPL, we’ll be able to navigate through previous commands using the arrow up and arrow down keys.

Pro Tip: Export NODE_REPL_HISTORY_FILE from your ~/.bash_profile to avoid having to type this every. single. time. you use the REPL.

3. REPL magic mode

The REPL now has support for 3 different modes:

  1. Sloppy
  2. Strict
  3. Magic

The first mode, sloppy, sets the REPL to non-strict mode; strict sets the REPL to strict mode – I won’t go over what strict mode is, but if you’d like to read more about it I suggest this MDN entry. Finally, the new magic mode automatically enables strict mode when needed.

Let’s look at an example using the new let keyword, currently supported exclusively in strict mode.

In previous versions of io.js, if we tried using a strict mode feature out of the box, this is the error we’d see:

$ node
> let name = 'Annie';
SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode

In order to use let, we had to explicitly opt-in to strict mode using the --use_strict flag:

$ node --use_strict
> let name = 'Annie';
undefined
> name
'Annie'

In version 2.0.0, the REPL defaults to magic mode, which automatically runs strict mode statements:

$ node
> let name = 'Annie';
undefined
> name
'Annie'

If, for some reason, we need to prevent automatic strict mode, we must manually set the NODE_REPL_MODE environment variable to sloppy.

$ NODE_REPL_MODE='sloppy' node
> let name = 'Annie';
SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode

4. Classes have moved out of staging

The new JavaScript class syntax is out of staging and now available behind the --use_strict flag (hint: remember, the magic mode we just talked about only works for the REPL). Previous versions of io.js also required the use of the --harmony_classes flag, in addition to --use_strict.

The following code illustrates a simple example when using the new class syntax:

class Person {
  constructor(name){
    this.name = name;
  }

  greet(){
    return `Hello I am ${this.name}`;
  }
}

var person = new Person("Sam");
console.log(person.greet());

To run this code in io.js 2.0.0, the only flag we need is --use_strict:

$ node --use_strict classes.js
Hello I am Sam

5. Rest parameters on staging

Another ES6 feature, rest parameters, was added to staging behind the --harmony-rest-parameters flag. Rest parameters allow function signatures to declare an indefinite number of arguments as an array. Let’s look at an example:

function listNames(...names){
  for(var name of names){
    console.log(name);
  }
}

listNames("Annie", "Camille", "Julie");

And now we can run this in the latest version of io.js using the --harmony-rest-parameters flag:

$ node --harmony-rest-parameters parameters.js
Annie
Camille
Julie

6. Computed property names on staging

Computed properties, also from ES6, are now available behind the --harmony-computed-property-names flag. This feature allows property key values to be determined by evaluating an expression. The following example illustrates a computed property for the stats object:

var ages = {
  "Kara": 25,
  "Jackie": 31
};

var firstPlace = "Kara",
  secondPlace = "Jackie";

var stats = {
  [firstPlace + "Age"]: ages[firstPlace],
  [secondPlace + "Age"]: ages[secondPlace],
};

console.log(stats);

Notice we are combining variables (firstPlace and secondPlace) with a string suffix (“Age”) to determine the key values. Running this code requires the --harmony-computed-property-names flag:

$ node --harmony-computed-property-names iojs-computed.js 
{ KaraAge: 25, JackieAge: 31 }

Of course these were just some of the changes added to version 2.0.0 of io.js. Want to see the complete list? Check out the CHANGELOG. And if you’re looking to learn more about writing server-side JavaScript applications with io.js or Node.js, I recommend playing through our Node.js tutorial, Real-time Web with Node.js, and our ExpressJS course, Building Blocks of ExpressJS. Let me know what you think about these updates and which is your favorite in the comments section below!

Code School

Code School teaches web technologies in the comfort of your browser with video lessons, coding challenges, and screencasts. We strive to help you learn by doing.

Visit codeschool.com

About the Author

Carlos Souza

Developer, Instructor and Metal aficionado.

Might We Suggest