Transpile JavaScript with Babel
suggest changeBabel 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
- 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
- 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.
- Add a build script to
package.json:
{
"scripts": {
"build": "babel src --out-dir dist"
}
}
- 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.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents