Lesson 4 / 30

Installing and Configuring Babel

Get Babel running with core packages and a .babelrc configuration.

npm install @babel/core and @babel/cli

Every Babel project starts with the core library and optional CLI for command-line transpilation.

npm install --save-dev @babel/core @babel/cli

.babelrc: the configuration file

Create a .babelrc file in your project root to tell Babel which presets and plugins to use.

{
  "presets": ["@babel/preset-env"],
  "plugins": []
}

Running Babel from the CLI

Use npx babel to transpile files or directories. Babel reads .babelrc automatically.

npx babel src/index.js
npx babel src -d dist  # transpile directory

Output:

let x = [1, 2, 3].map(n => n * 2);
// becomes:
var x = [1, 2, 3].map(function(n) {
  return n * 2;
});