🚩 TODO Explain some ES6 language features
🚩 TODO Explain some ES7 language features
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.
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.
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).
Add the babel presets to your package.json
:
"babel": {
"presets": ["es2015-node5", "stage-3"]
}
Alternatively, you can create a .babelrc
file.
Add a reuseable script to your package.json
:
"scripts": {
"build": "babel src --out-dir lib"
}
Run the script:
npm run build