Transpile JavaScript with Babel

suggest change

Babel can transform modern JavaScript syntax for environments that do not support every feature used by an application. Choose target environments explicitly: transforming more syntax than necessary increases build time and output size.

Basic command-line setup

  1. Initialize a project and install Babel's command-line tools and environment preset:
npm init -y
npm install --save-dev @babel/core @babel/cli @babel/preset-env
  1. Create babel.config.json:
{
  "presets": [
    ["@babel/preset-env", {
      "targets": "> 0.25%, not dead"
    }]
  ]
}

Production projects should define targets through their supported-runtime policy, often with a Browserslist configuration, instead of copying this illustrative query unchanged.

  1. Add a build script to package.json:
{
  "scripts": {
    "build": "babel src --out-dir dist"
  }
}
  1. Put source files in src, then run:
npm run build

Babel transforms syntax; it does not automatically provide every missing runtime API. Depending on the targets and features used, an application may also need carefully selected polyfills. Keep the Babel packages, target configuration, and runtime support policy under version control.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents