Skip to content

Instantly share code, notes, and snippets.

@MarkTiedemann
Created November 23, 2015 19:04
Show Gist options
  • Save MarkTiedemann/69bb6d7a7930074943f3 to your computer and use it in GitHub Desktop.
Save MarkTiedemann/69bb6d7a7930074943f3 to your computer and use it in GitHub Desktop.
Using Next Generation Node

Using Next Generation Node

Features

ES6 (ES2015)

🚩 TODO Explain some ES6 language features

ES7 (ES2016)

🚩 TODO Explain some ES7 language features

Transpiling

For transpiling Node, Babel has become the de-facto standard.

Out of the box, however, Babel doesn't do anything. For transpiling your code, you need to enable plugins. To make it easier to share such configurations, multiple plugins form presets.

The following steps explain how to Install Babel, Install Babel Presets, Configure Babel To Use Presets and, finally, how to Transpile With Babel.

They assume that you want to transpile ES6 and ES7 files from a src directory to a lib directory in such a way that they are compatible with Node 5.

Step 1: Install Babel

To get started with Babel, you can use babel-cli. It's a tool for transpiling files via the command line.

You can install babel-cli via npm:

npm install babel-cli --save-dev

If you want to make babel-cli available globally, you can install it with the --global flag instead.

Step 2: Install Babel Presets

Since Node 5 already supports some ES6 features, not all of them are needed. The babel-preset-es2015-node5 encapsulates those missing features.

npm install babel-preset-es2015-node5 --save-dev

To be able to use ES7 language features, you can add a stage preset. If you use babel-preset-stage-3, for example, you can use async/await and the exponentiation operator.

npm install babel-preset-stage-3 --save-dev

💡 The default Babel stage presets contain all higher stages (e.g. stage 1 does also contain stage 2 and stage 3).

Step 3: Configure Babel To Use Presets

Add the babel presets to your package.json:

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

Alternatively, you can create a .babelrc file.

Step 4: Transpile With Babel

Add a reuseable script to your package.json:

"scripts": {
  "build": "babel src --out-dir lib"
}

Run the script:

npm run build
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment