API Reference

Babel

JavaScript compiler that transforms modern syntax and TypeScript into backward-compatible code through presets and plugins.

Official Documentation ↗
CompatibilityThis documentation covers version v8.1.0. Check the official docs for the latest API surface.

Installation

npmnpm install --save-dev @babel/core @babel/preset-env
pnpmpnpm add -D @babel/core @babel/preset-env

Methods

Overview

Babel is the long-standing JavaScript compiler that turns modern syntax and TypeScript into code older environments can run using presets and plugins. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.

Core APIs

  • transformAsync — Asynchronously compiles the given source and resolves with code, map, and metadata.
  • transformSync — Synchronous compilation entry recommended for build tools that cannot await.
  • parse — Parses source into a Babel AST for custom analysis or codemods.

Example

const babel = require('@babel/core');

const result = await babel.transformAsync('const x = () => 1;', {
  presets: ['@babel/preset-env'],
});
console.log(result.code);

Notes

Babel only transforms syntax by default; runtime APIs still need polyfills via core-js. Use preset-env targets and cacheDirectory to balance compatibility and build speed.

Verification

Compile an arrow function with preset-env targeting an old browser and confirm the output uses a regular function expression.

API reference evidence

Fetched from official sources on 2026-11-08T19:20:00+08:00 via WebFetch https://babeljs.io/docs/babel-core.

  • transformAsync → signature from https://babeljs.io/docs/babel-core; official text: “babel.transformAsync(code: string, [options?]: Object) babel.transformAsync(code, options); // => Promise<{ code, map, ast }>”
  • transformSync → signature from https://babeljs.io/docs/babel-core; official text: “babel.transformSync(code: string, [options?]: Object) babel.transformSync(code, options); // => { code, map, ast }”
  • parse → signature from https://babeljs.io/docs/babel-core; official text: “babel.parse(code: string, [options?]: Object, callback: Function) Given some code, parse it using Babel’s standard behavior.”

FAQ

What does @babel/preset-env do?
It selects the syntax transforms and polyfill imports needed for the browsers listed in your targets, so you do not hand-pick plugins.
Do I still need core-js for polyfills?
Yes for runtime APIs. Configure useBuiltIns: "usage" with corejs: 3 in preset-env so only used polyfills are injected.
How do I enable caching for faster rebuilds?
Set the cacheDirectory option to true or a path; Babel stores results keyed by file content and options.