- Install
babel-cliand 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
envpreset is the most recent version.
npm i --save-dev babel-preset-env
- Create a
.babelrcfile in the root of your project. The.babelrcfile 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
buildscript to yourpackage.jsonto transpile your JavaScript usingbabel. This example transpiles all.jsfiles inside thesrc/directory and outputs tolib/.
"scripts": {
"build": "babel src -d lib",
},