@babel/preset-env is a smart preset that allows you to use the latest JavaScript without needing to micromanage which syntax transforms (and optionally, browser polyfills) are needed by your target environment(s). This both makes your life easier and JavaScript bundles smaller!

Install

With npm:

  1. npm install --save-dev @babel/preset-env

Or yarn:

  1. yarn add @babel/preset-env --dev

How Does it Work?

@babel/preset-env would not be possible if not for a number of awesome open-source projects, like browserslist, compat-table, and electron-to-chromium.

We leverage these data sources to maintain mappings of which version of our supported target environments gained support of a JavaScript syntax or browser feature, as well as a mapping of those syntaxes and features to Babel transform plugins and core-js polyfills.

It is important to note that @babel/preset-env does not support stage-x plugins.

@babel/preset-env takes any target environments you've specified and checks them against its mappings to compile a list of plugins and passes it to Babel.

Browserslist Integration

For browser- or Electron-based projects, we recommend using a .browserslistrc file to specify targets. You may already have this configuration file as it is used by many tools in the ecosystem, like autoprefixer, stylelint, eslint-plugin-compat and many others.

By default @babel/preset-env will use browserslist config sourcesunless either the targets or ignoreBrowserslistConfig options are set.

For example, to only include polyfills and code transforms needed for users whose browsers have >0.25% market share (ignoring browsers without security updates like IE 10 and BlackBerry):

Options

  1. {
  2. "presets": [
  3. [
  4. "@babel/preset-env",
  5. {
  6. "useBuiltIns": "entry"
  7. }
  8. ]
  9. ]
  10. }

browserslist

  1. > 0.25%
  2. not dead

or

package.json

  1. "browserslist": "> 0.25%, not dead"

Options

For more information on setting options for a preset, refer to the preset options documentation.

targets

string | Array<string> | { [string]: string }, defaults to {}.

Describes the environments you support/target for your project.

This can either be a browserslist-compatible query:

  1. {
  2. "targets": "> 0.25%, not dead"
  3. }

Or an object of minimum environment versions to support:

  1. {
  2. "targets": {
  3. "chrome": "58",
  4. "ie": "11"
  5. }
  6. }

Example environments: chrome, opera, edge, firefox, safari, ie, ios, android, node, electron.

Sidenote, if no targets are specified, @babel/preset-env will transform all ECMAScript 2015+ code by default.

We don't recommend using preset-env this way because it doesn't take advantage of its ability to target specific browsers.

  1. {
  2. "presets": ["@babel/preset-env"]
  3. }

targets.esmodules

boolean.

You may also target browsers supporting ES Modules (https://www.ecma-international.org/ecma-262/6.0/#sec-modules). When specifying this option, the browsers field will be ignored. You can use this approach in combination with <script type="module"></script> to conditionally serve smaller scripts to users (https://jakearchibald.com/2017/es-modules-in-browsers/#nomodule-for-backwards-compatibility).

Please note: when specifying the esmodules target, browsers targets will be ignored.

  1. {
  2. "presets": [
  3. [
  4. "@babel/preset-env",
  5. {
  6. "targets": {
  7. "esmodules": true
  8. }
  9. }
  10. ]
  11. ]
  12. }

targets.node

string | "current" | true.

If you want to compile against the current node version, you can specify "node": true or "node": "current", which would be the same as "node": process.versions.node.

targets.safari

string | "tp".

If you want to compile against the technology preview version of Safari, you can specify "safari": "tp".

targets.browsers

string | Array<string>.

A query to select browsers (ex: last 2 versions, > 5%, safari tp) using browserslist.

Note, browsers' results are overridden by explicit items from targets.

Note: this will be removed in later version in favor of just setting "targets" to a query directly.

spec

boolean, defaults to false.

Enable more spec compliant, but potentially slower, transformations for any plugins in this preset that support them.

loose

boolean, defaults to false.

Enable "loose" transformations for any plugins in this preset that allow them.

modules

"amd" | "umd" | "systemjs" | "commonjs" | "cjs" | "auto" | false, defaults to "auto".

Enable transformation of ES6 module syntax to another module type.

Setting this to false will not transform modules.

Also note that cjs is just an alias for commonjs.

debug

boolean, defaults to false.

Outputs the targets/plugins used and the version specified in plugin data version to console.log.

include

Array<string|RegExp>, defaults to [].

An array of plugins to always include.

Valid options include any:

  • Babel plugins - both with (@babel/plugin-transform-spread) and without prefix (plugin-transform-spread) are supported.

  • Built-ins (both for core-js@2 and core-js@3, such as es.map, es.set, or es.object.assign.

Plugin names can be fully or partially specified (or using RegExp).

Acceptable inputs:

  • Full name (string): "es.math.sign"
  • Partial name (string): "es.math.*" (resolves to all plugins with es.math prefix)
  • RegExp Object: /^transform-.$/ or new RegExp("^transform-modules-.")

Note that the above . is the RegExp equivalent to match any character, and not the actual '.' character. Also note that to match any character . is used in RegExp as opposed to in glob format.

This option is useful if there is a bug in a native implementation, or a combination of a non-supported feature + a supported one doesn't work.

For example, Node 4 supports native classes but not spread. If super is used with a spread argument, then the @babel/plugin-transform-classes transform needs to be included, as it is not possible to transpile a spread with super otherwise.

NOTE: The include and exclude options only work with the plugins included with this preset; so, for example, including @babel/plugin-proposal-do-expressions or excluding @babel/plugin-proposal-function-bind will throw errors. To use a plugin not included with this preset, add them to your "plugins" directly.

exclude

Array<string|RegExp>, defaults to [].

An array of plugins to always exclude/remove.

The possible options are the same as the include option.

This option is useful for "blacklisting" a transform like @babel/plugin-transform-regenerator if you don't use generators and don't want to include regeneratorRuntime (when using useBuiltIns) or for using another plugin like fast-async instead of Babel's async-to-gen.

useBuiltIns

"usage" | "entry" | false, defaults to false.

This option configures how @babel/preset-env handles polyfills.

When either the usage or entry options are used, @babel-preset-env will add direct references to core-js modules as bare imports (or requires). This means core-js will be resolved relative to the file itself and needs to be accessible.

Since @babel/polyfill was deprecated in 7.4.0, we recommend directly adding core-js and setting the version via the corejs option.

  1. npm install core-js@3 --save
  2. # or
  3. npm install core-js@2 --save

useBuiltIns: 'entry'

NOTE: Only use import "core-js"; and import "regenerator-runtime/runtime"; once in your whole app.If you are using @babel/polyfill, it already includes both core-js and regenerator-runtime: importing it twice will throw an error.Multiple imports or requires of those packages might cause global collisions and other issues that are hard to trace.We recommend creating a single entry file that only contains the import statements.

This option enables a new plugin that replaces the import "core-js/stable"; and import "regenerator-runtime/runtime" statements (or require("corejs") and require("regenerator-runtime/runtime")) with individual requires to different core-js entry points based on environment.

In

  1. import "core-js";

Out (different based on environment)

  1. import "core-js/modules/es.string.pad-start";
  2. import "core-js/modules/es.string.pad-end";

Importing "core-js" loads polyfills for every possible ECMAScript feature: what if you know that you only need some of them? When using core-js@3, @babel/preset-env is able to optimize every single core-js entrypoint and their combinations. For example, you might want to only polyfill array methods and new Math proposals:

In

  1. import "core-js/es/array";
  2. import "core-js/proposals/math-extensions";

Out (different based on environment)

  1. import "core-js/modules/es.array.unscopables.flat";
  2. import "core-js/modules/es.array.unscopables.flat-map";
  3. import "core-js/modules/esnext.math.clamp";
  4. import "core-js/modules/esnext.math.deg-per-rad";
  5. import "core-js/modules/esnext.math.degrees";
  6. import "core-js/modules/esnext.math.fscale";
  7. import "core-js/modules/esnext.math.rad-per-deg";
  8. import "core-js/modules/esnext.math.radians";
  9. import "core-js/modules/esnext.math.scale";

You can read core-js's documentation for more information about the different entry points.

NOTE: When using core-js@2 (either explicitly using the corejs: 2 option or implicitly), @babel/preset-env will also imports and requires of @babel/polyfill.This behavior is deprecated because it isn't possible to use @babel/polyfill with different core-js versions.

useBuiltIns: 'usage'

Adds specific imports for polyfills when they are used in each file. We take advantage of the fact that a bundler will load the same polyfill only once.

In

a.js

  1. var a = new Promise();

b.js

  1. var b = new Map();

Out (if environment doesn't support it)

  1. import "core-js/modules/es.promise";
  2. var a = new Promise();
  1. import "core-js/modules/es.map";
  2. var b = new Map();

Out (if environment supports it)

  1. var a = new Promise();
  1. var b = new Map();

useBuiltIns: false

Don't add polyfills automatically per file, and don't transform import "core-js" or import "@babel/polyfill" to individual polyfills.

corejs

2, 3 or { version: 2 | 3, proposals: boolean }, defaults to 2.

This option only has an effect when used alongside useBuiltIns: usage or useBuiltIns: entry, and ensures @babel/preset-env injects the correct imports for your core-js version.

By default, only polyfills for stable ECMAScript features are injected: if you want to polyfill them, you have three different options:

  • when using useBuiltIns: "entry", you can directly import a proposal polyfill: import "core-js/proposals/string-replace-all".
  • when using useBuiltIns: "usage" you have two different alternatives:
    • set the shippedProposals option to true. This will enable polyfills and transforms for proposal which have already been shipped in browsers for a while.
    • use corejs: { version: 3, proposals: true }. This will enable polyfilling of every proposal supported by core-js.

forceAllTransforms

boolean, defaults to false.

Example

With Babel 7's JavaScript config file support, you can force all transforms to be run if env is set to production.

  1. module.exports = function(api) {
  2. return {
  3. presets: [
  4. [
  5. "@babel/preset-env",
  6. {
  7. targets: {
  8. chrome: 59,
  9. edge: 13,
  10. firefox: 50,
  11. },
  12. // for uglifyjs...
  13. forceAllTransforms: api.env("production"),
  14. },
  15. ],
  16. ],
  17. };
  18. };

NOTE: targets.uglify is deprecated and will be removed in the next major infavor of this.

By default, this preset will run all the transforms needed for the targetedenvironment(s). Enable this option if you want to force running _all_transforms, which is useful if the output will be run through UglifyJS or anenvironment that only supports ES5.

NOTE: Uglify has a work-in-progress "Harmony" branch to address the lack ofES6 support, but it is not yet stable. You can follow its progress inUglifyJS2 issue #448. If yourequire an alternative minifier which does support ES6 syntax, we recommendusing babel-minify.

configPath

string, defaults to process.cwd()

The starting point where the config search for browserslist will start, and ascend to the system root until found.

ignoreBrowserslistConfig

boolean, defaults to false

Toggles whether or not browserslist config sources are used, which includes searching for any browserslist files or referencing the browserslist key inside package.json. This is useful for projects that use a browserslist config for files that won't be compiled with Babel.

shippedProposals

boolean, defaults to false

Toggles enabling support for builtin/feature proposals that have shipped in browsers. If your target environments have native support for a feature proposal, its matching parser syntax plugin is enabled instead of performing any transform. Note that this does not enable the same transformations as @babel/preset-stage-3, since proposals can continue to change before landing in browsers.

The following are currently supported:

Builtins injected when using useBuiltIns: "usage"

Features

  • None

You can read more about configuring preset options here