Notes: Issues with the output should be reported on the babel issue tracker.
webpack 1.x | babel-loader <= 6.x
webpack 2.x | babel-loader >= 7.x (recommended) (^6.2.10 will also work, but with deprecation warnings)
webpack 3.x | babel-loader >= 7.1
yarn add babel-loader babel-core babel-preset-env webpack --dev
We recommend using yarn, but you can also still use npm:
npm install --save-dev babel-loader babel-core babel-preset-env webpack
Within your webpack configuration object, you'll need to add the babel-loader to the list of modules, like so:
module:rules:test: /\.js$/exclude: //use:loader: 'babel-loader'options:presets: 'env'
You can pass options to the loader by using the options property:
module:rules:test: /\.js$/exclude: //use:loader: 'babel-loader'options:presets: 'env'plugins:
This loader also supports the following loader-specific option:
false. When set, the given directory will be used to cache the results of the loader. Future webpack builds will attempt to read from the cache to avoid needing to run the potentially expensive Babel recompilation process on each run. If the value is blank (
loader: 'babel-loader?cacheDirectory') or
loader: babel-loader?cacheDirectory=true) the loader will use the default cache directory in
node_modules/.cache/babel-loaderor fallback to the default OS temporary file directory if no
node_modulesfolder could be found in any root directory.
cacheIdentifier: Default is a string composed by the babel-core's version, the babel-loader's version, the contents of .babelrc file if it exists and the value of the environment variable
BABEL_ENVwith a fallback to the
NODE_ENVenvironment variable. This can be set to a custom value to force cache busting if the identifier changes.
forceEnv: Default will resolve BABEL_ENV then NODE_ENV. Allow you to override BABEL_ENV/NODE_ENV at the loader level. Useful for isomorphic applications with different babel configuration for client and server.
sourceMap option is ignored, instead sourceMaps are automatically enabled when webpack is configured to use them (via the
devtool config option).
babel-loader is slow!
Make sure you are transforming as few files as possible. Because you are probably
/\.js$/, you might be transforming the
node_modules folder or other unwanted
node_modules, see the
exclude option in the
loaders config as documented above.
You can also speed up babel-loader by as much as 2x by using the
This will cache transformations to the filesystem.
babel is injecting helpers into each file and bloating my code!
babel uses very small helpers for common functions such as
_extend. By default
this will be added to every file that requires it.
You can instead require the babel runtime as a separate module to avoid the duplication.
The following configuration disables automatic per-file runtime injection in babel, instead
babel-plugin-transform-runtime and making all helper references use it.
See the docs for more information.
NOTE: You must run
npm install babel-plugin-transform-runtime --save-dev to include this in your project and
babel-runtime itself as a dependency with
npm install babel-runtime --save.
rules:// the 'transform-runtime' plugin tells babel to require the runtime// instead of inlining it.test: /\.js$/exclude: //use:loader: 'babel-loader'options:presets: 'env'plugins: 'transform-runtime'
NOTE: transform-runtime & custom polyfills (e.g. Promise library)
// ...'Promise': 'bluebird'// ...
The following approach will not work either:
default = ;var promise = ;
which outputs to (using
'use strict';var _Promise = 'default';'default' = ;var promise = ;
Promise library is referenced and used before it is overridden.
One approach is to have a "bootstrap" step in your application that would first override the default globals before your application:
// bootstrap.jsdefault = ;// ...;
The node API for
babel has been moved to
If you receive this message it means that you have the npm package
babel installed and use the short notation of the loader in the webpack config (which is not valid anymore as of webpack 2.x):
test: /\.js$/loader: 'babel'
Webpack then tries to load the
babel package instead of the
To fix this you should uninstall the npm package
babel as it is deprecated in babel v6. (instead install
In the case one of your dependencies is installing
babel and you cannot uninstall it yourself, use the complete name of the loader in the webpack config:
test: /\.js$/loader: 'babel-loader'