Skip to content

Jesses Software Engineering Blog

Apr 19

Jesse

NodeJS ES6 with BabelJS

The JavaScript landscape has been one of the fastest growing programming niches I’ve had both the pleasure and angst of working with. While it’s exciting to watch and work with a rapidly growing technology, it can be extremely frustrating mastering a library or technique to have it quickly become obsolete.

Some the best and, in my opinion, longest lasting changes to JavaScript have come along in ES6. Since not all of ES6 (ES2015) is currently supported in NodeJS by default, it’s common to use a compiler such as BabelJS to leverage, or select, new functionality. Getting up and running with BabelJS allows access to all the newest features in NodeJS, both permanent and experimental.

package.json

{
  "scripts": {
    "start": "node -r babel-register -r babel-polyfill index.js"
  },
  "dependencies": {
    "babel-polyfill": "^6.7.4",
    "babel-preset-es2015": "^6.6.0",
    "babel-preset-stage-3": "^6.5.0",
    "babel-register": "^6.7.2"
  }
}

These are the typical required libraries needed to get Babel up and running. I’ve included both polyfill and stage-3 to get access to the async/await functionality. You will also need a .babelrc file to specify which presets to include:

.babelrc

{
  "presets": [
    "es2015",
    "stage-3"
  ]
}

index.js

let vart = "yasssss";

At this point any of the new ES6 functionality will be available along with Class keyword and async/await functionality, which are some of the best new features in JavaScript, without using strict mode.

Blog Powered By Wordpress