rollup-plugin-bundle-esm
raw JSON → 0.1.1 verified Mon Apr 27 auth: no javascript
A Rollup plugin that automatically bundles only ESM-only dependencies while leaving CommonJS-compatible packages external, optimizing bundle size and reducing edge cases. Current stable version is 0.1.1 (released 2022, no recent updates). Key differentiator: solves the dual-packaging problem for environments without ESM support (e.g., Electron) by specifically targeting ESM packages for inlining, unlike general-purpose resolve plugins that bundle everything. Compatible with Rollup 3+; requires @rollup/plugin-node-resolve and @rollup/plugin-commonjs as companion plugins.
Common errors
error Error: Cannot find module 'rollup-plugin-bundle-esm' ↓
cause Package not installed or incorrect import path.
fix
Run
npm install --save-dev rollup-plugin-bundle-esm and ensure import path is correct. error TypeError: bundleESM is not a function ↓
cause Importing named export instead of default export.
fix
Use
import bundleESM from 'rollup-plugin-bundle-esm' instead of import { bundleESM } from .... error Error: ESM package 'x' was treated as external but should be bundled ↓
cause Plugin incorrectly classifies package as CommonJS due to missing 'type' field.
fix
Manually specify 'forceBundle' for that package:
bundleESM({ forceBundle: ['x'] }). Warnings
gotcha Plugin may not correctly detect package format if package.json is malformed or missing 'type' field. Assumes no 'type' field means CommonJS. ↓
fix Ensure all dependencies have a 'type' field in their package.json; use 'forceBundle' option to override.
gotcha The plugin must be placed after nodeResolve() in the Rollup plugin list to work correctly. ↓
fix Order plugins: commonjs() first, then nodeResolve(), then bundleESM().
gotcha forceBundle option uses the same signature as rollup.external, which may cause confusion between including vs excluding. ↓
fix Use forceBundle to force bundling of specific packages; do not confuse with external option.
deprecated Package has not been updated since v0.1.1 (released 2022); may lack compatibility with latest Rollup versions. ↓
fix Consider alternative plugins or manual external configuration.
Install
npm install rollup-plugin-bundle-esm yarn add rollup-plugin-bundle-esm pnpm add rollup-plugin-bundle-esm Imports
- default wrong
import { bundleESM } from 'rollup-plugin-bundle-esm'correctimport bundleESM from 'rollup-plugin-bundle-esm' - default wrong
const { bundleESM } = require('rollup-plugin-bundle-esm')correctconst bundleESM = require('rollup-plugin-bundle-esm') - BundleESMOptions wrong
import { BundleESMOptions } from 'rollup-plugin-bundle-esm'correctimport type { BundleESMOptions } from 'rollup-plugin-bundle-esm'
Quickstart
import commonjs from '@rollup/plugin-commonjs';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import { defineConfig } from 'rollup';
import bundleESM from 'rollup-plugin-bundle-esm';
export default defineConfig({
output: {
format: 'commonjs',
dir: 'dist',
},
input: 'index.js',
plugins: [commonjs(), nodeResolve(), bundleESM()],
});