- Install
babel-cli
and save it to your developer dependencies
npm i --save-dev babel-cli
- Install a preset of your choice. Presets indicate which version of EcmaScript to use. The
env
preset is the most recent version.
npm i --save-dev babel-preset-env
- Create a
.babelrc
file in the root of your project. The.babelrc
file specifies which preset to use, along with any plugins or other configuration options.
A minimal .babelrc
file looks like this:
{
"preset": ["env"]
}
- Install any plugins you wish to use. Plugins enable specific syntactic or language features. For example, to enable object spread, the following steps should be performed:
npm i --save-dev babel-plugin-transform-object-rest-spread
{
"plugins": ["transform-object-rest-spread"],
"presets": ["env"]
}
- Add a
build
script to yourpackage.json
to transpile your JavaScript usingbabel
. This example transpiles all.js
files inside thesrc/
directory and outputs tolib/
.
"scripts": {
"build": "babel src -d lib",
},