This post contains content about Now 1.0 – Learn about the latest version, Now 2.0.
Now 2.0 - Upgrade Available
Monday, June 6th 2016 (about 3 years ago)

Node v6

Tony Kovanen (@tonykovanen)

We're very happy to announce that all new deployments to Now will be served with the newly released Node.js *6.2.1* effective immediately.

Try it out with a package.json:

{
"name": "my-micro-service",
"dependencies": {
  "express": "4.13.4"
},
"scripts": {
  "start": "node index.js"
}
}

Your new ES6-powered index.js

const app = require('express')();
const { version } = process;

app.get('/', (req, res) => {
res.send(`Hello from ES6 + Node ${version}`);
});

app.listen(3000);

and then deploy!

▲ now .

What about older versions of Node? We will honor the engines field in your package.json.

"engines": {
"node": "5.x.x"
}

If you want to specify a special configuration for just the deployment, you can use now.engines:

"now": {
"engines": {
  "node": "5.x.x"
}
}

Starting with the newly released Now 0.17.0, you'll be notified of what version of Node is being used for your deployment. For example, if you choose not to specify engines:

 ▲  my-project (master) now
> Deploying "/Users/rase-/Projects/my-project"
> Using Node.js 6.2.1 (default)
> Ready! https://my-project-emflwcoqxv.now.sh (copied to clipboard) [296ms]
> […]

What's exciting about Node 6? Almost complete support for ES6 at 93%!

Let's go over some of the new things you can now do natively with Node, without invoking any build processes or transpilers.

Template literals

Concatating strings with + can be painful. Moving forward, you can use template literals:

const a = 11;
const b = 12;
const sum = a + b;
console.log(`${a} + ${b} = ${sum}`);

Classes

Explaining the ins and outs of Prototype in JS could get in the way of quickly picking up the language and become productive. While that knowledge is still fundamental, class offers some syntactic relief:

class Vehicle {
drive () {
  console.log('Driving vehicle');
}
}

class Car extends Vehicle {
drive () {
  super.drive();
  console.log('of type car');
}
}

const car = new Car();
car.drive();

Destructuring

Extracting data from objects is extremely common in JavaScript, specially when receiving and parsing JSON data from web services. Destructuring helps there:

function basicAuth ({ name, pass }) {
return 'Basic ' + new Buffer(`${name}:${pass}`).toString('base64');
}

const user = {
uid: 999999999,
name: 'hello',
email: 'hello@hello.hello',
pass: 'hello'
};

console.log('Authorization:', basicAuth(user));

Notice that the `basicAuth` function de-structures the received object parameter.
Destructuring also works with assignment:

const { user, name } = getProfile();

as well as Array:

const [first,,third] = ['First item', 'Second item', 'Third item'];

Default parameters

Setting up default values in JavaScript can be tricky and error-prone. Consider this example:

function getCount (count) {
return count || 'not provided';
}

If you pass a count of 0, that snippet will incorrectly think it was not provided because 0 evaluates to false. This is why it's smart to use the new ES6 defaults instead:

function getCount (count = 'not provided') {
return count;
}

For … of loops

Iterating over the values of a Set, or the entries in a Map is dramatically easier and more expressive thanks to for … of:

const members = new Set(['Guillermo', 'Tony', 'Naoyuki']);
for (const name of members) {
console.log(name);
}

To iterate a Map we can combine for … ofwith the array destructuring technique discussed above by invoking .entries():

const countries = new Map([
['us', 'United States'],
['fi', 'Finland'],
['jp', 'Japan']
]);
for (const [code, name] of countries.entries()) {
console.log(`${code}: ${name}`);
}

What's missing?

There are only a few features missing and some unresolved edge cases for Node to reach strict and complete spec compliance.

Two things to look out for are:

ES6 Modules

There's an ongoing debate over how ES6 modules will be incorporated into the platform for maximum inter-operability and backwards compatibility with require:

  • Through a new file extension .mjs
  • Or declared as part of a package.jsonand retaining .js

A great summary of the discussion and proposals can be found in the excellent es6modules-nodejs repo by Martin Heidegger.

Ideal performance

It's important to mention that there are some notable performance *penalties* with some of these new features and capabilities.

You can look at this table for very detailed benchmarks:

The V8 team is committed to improving the performance of thenewly introduced features. In the words of Seth Thompson, one of the Chrome project managers:

It's important to remember that implementation of ES2015 is merely the first pass; optimization comes soon after and will rapidly improve performance. General, absolute numbers (e.g. "ES2015 is X% slower than ES5") are usually either nonspecific or soon out-of-date. The best advice is to revise your performance assumptions frequently based on real code.

In closing, we are very excited to put the latest version of Node at your fingertips, while also offering you a simple way to pick and choose the version that suits you and your existing code the best.