babel-preset-env-modules
raw JSON → 1.0.1 verified Sat Apr 25 auth: no javascript
A Babel preset that wraps @babel/preset-env and babel-polyfill-corejs3 to produce minimal compiled output for environments that support ESM, with improved polyfill handling. Current version 1.0.1, released as a single stable version. It solves the common issue where preset-env conflates helpers and polyfills, leading to unnecessary code. Key differentiators: automatically prevents inclusion of supported builtins, uses finer-grained polyfills via babel-polyfills, excludes niche polyfills (e.g., Symbol.species) by default, and coordinates options between preset-env and polyfill plugin to avoid conflicts. Suitable for library authors targeting modern browsers or Node.js with ESM.
Common errors
error Error: [BABEL] unknown preset 'babel-preset-env-modules' ↓
cause Preset not installed
fix
npm install babel-preset-env-modules --save-dev
error TypeError: Cannot read property 'preset-env' of undefined ↓
cause Missing @babel/preset-env peer dependency
fix
npm install @babel/preset-env --save-dev
Warnings
gotcha The polyfill option automatically sets corejs and useBuiltIns, overriding any manual settings passed to preset-env. ↓
fix Use polyfill option instead of manually setting corejs/useBuiltIns in preset-env options.
deprecated The package is essentially a wrapper that may become obsolete as @babel/preset-env and babel-polyfills improve. ↓
fix Consider migrating to direct use of @babel/preset-env with useBuiltIns: 'usage' and corejs option.
gotcha Setting modules to false (preserve ESM) may conflict with polyfill plugin if not careful; polyfill may still inject non-ESM code. ↓
fix Ensure polyfill option is configured to respect modules setting.
Install
npm install babel-preset-env-modules yarn add babel-preset-env-modules pnpm add babel-preset-env-modules Imports
- babel-preset-env-modules wrong
import preset from 'babel-preset-env-modules'correctmodule.exports = { presets: [['babel-preset-env-modules', options]] } - DEFAULT wrong
module.exports = { presets: [require('babel-preset-env-modules')] }correctmodule.exports = { presets: ['babel-preset-env-modules'] }
Quickstart
// babel.config.js
module.exports = {
presets: [
[
'babel-preset-env-modules',
{
targets: { esmodules: true },
loose: true,
bugfixes: true,
modules: false, // preserve ESM
polyfill: {
// see babel-polyfill-corejs3 options
corejs: 3,
proposals: true,
},
},
],
],
};