Using ES6 in Node.js

My team recently upgraded our Node version from 0.10 to 4.0 for a big app we are working on. Lots of improvements came in the upgrade, but by far, my favorite is all of the ES6 features now available natively in Node.

Just to be clear, Node 4.0 does not exhaustively support ES6. It implements a lot of the biggest new features, all incorporated from the upstream V8 engine. Getting all of these features–especially without having to set up up a build process with Babel or another compiler–has been a huge win for our team. We’ve been able to write cleaner, better JavaScript, and we’ve been gradually refactoring our app to use the new ES6 features.

I thought I would go over a few of my favorite features,  focusing on what is available and why I like it, rather than the specifics of each feature. There are a huge number of resources like MDN which can explain the specifics better than I can!

let and const

If I’m being honest, var isn’t that bad. Functional scoping is admittedly weird, but I don’t know that I’ve ever encountered an actual bug from it. let just makes more sense though; it works like every other programming language.

On the other hand, const is excellent. It actually enforces the constrain that JavaScript programmers have had wrapped up IN_CONVENTION for so long. Node 4.0 supports both let and const.

Classes

Dealing with prototype chains and OO programming in JavaScript has always been a pain, but with ES6 classes, that has finally been fixed. The great thing about the class syntax is that it is just sugar over the existing JavaScript OO patterns, not a complete divergence. It makes defining classes, constructors, and inheritance much more intuitive, though.

Arrow Functions

This is my absolute favorite ES6 feature. Arrow functions solve the issue of anonymous functions failing to bind the this variable correctly. As a bonus, they also have a much more succinct syntax. They play really well with classes and libraries like lodash.

Template Strings

No more endless string concatenation! Dynamic strings have always been painful in JavaScript, but no more. Template strings let you interpolate variables and expressions directly into your JavaScript.

Native Promises

Promises are fantastic, and now you no longer need a separate library for them. ES6 native promises follow the same Promises/A+ standard as all the most popular libraries, so they can interop seamlessly with them.

Things You Don’t Get

Node, at least in version 4.0, does not support all of ES6. Also, most of the features are only available in strict mode, but you’re already using that, right? Some of the great ES6 features you don’t get include:

  • Default function parameters
  • Array and object destructuring

For an exhaustive list of what is and isn’t supported, check out this table.

But honestly, the things you do get are a big win, as is not having to run your code through a compilation step to get it. If you aren’t using ES6 in your Node.js apps, now is the time to start!

Conversation
  • john says:

    Not to mentioned generator functions! Generator functions wrapped with the co library can really cleanup callback and promise syntax.

  • Philip Damra says:

    I’ve really been enjoying using some of the new es2015 features, too. I’ve started transpiling my server code with babel on my most recent project, so as to better support isomorphic React apps. It’s nice because it avoids that “big bang” loading effect of SPAs where the app is initially displayed in a neutral state, then makes multiple API calls to load data.

    It looks like they’re dropping future plans for array comprehensions. They’re one of my favorite features of python.

  • Woohoo node.js 6.0.0 released this week, with 96% native ES6 coverage!

  • Comments are closed.