esbuild-plugin-named-exports

raw JSON →
3.0.7 verified Fri May 01 auth: no javascript

An esbuild plugin (v3.0.7, infrequent releases) that rewrites CommonJS re-exports to preserve named exports when bundling CJS to ESM. It uses cjs-module-lexer to detect all named exports and emits explicit named export statements, preventing the common issue where only a default export appears. Unlike manual conversion or other CJS→ESM tools, this plugin automates the re-export of named exports for packages that re-export via module.exports = require(...).

error Error: The plugin "esbuild-plugin-named-exports" is not a function
cause Passed the plugin object directly instead of calling it as a function.
fix
plugins: [esbuldNamedExportsPlugin()]
error export default undefined
cause Plugin not applied; CJS re-exports lose named exports.
fix
Add the plugin to the esbuild build configuration.
error SyntaxError: Unexpected token 'export'
cause Using plugin with format 'cjs' or target too low.
fix
Ensure format is 'esm' and target is es2017 or higher.
gotcha Plugin must be called as a function: `esbuldNamedExportsPlugin()` not just the object.
fix Pass `esbuldNamedExportsPlugin()` in the plugins array.
gotcha Plugin only works when bundling CJS modules that re-export via `module.exports = require(...)`. It does not handle other CJS patterns.
fix Ensure source files use the exact pattern `module.exports = require('./other')`.
gotcha Plugin may incorrectly add named exports for dynamic require() calls or conditional exports.
fix Review generated bundle and manually fix if needed.
breaking In v3, the package switched to ESM-only, dropping CommonJS support.
fix Use dynamic import in CJS or switch to ESM.
npm install esbuild-plugin-named-exports
yarn add esbuild-plugin-named-exports
pnpm add esbuild-plugin-named-exports

Bundles a JavaScript file with esbuild from CJS to ESM, preserving named exports using the plugin.

import esbuild from 'esbuild';
import esbuldNamedExportsPlugin from 'esbuild-plugin-named-exports';

await esbuild.build({
  entryPoints: ['./input.js'],
  bundle: true,
  outfile: 'output.js',
  format: 'esm',
  target: 'es2017',
  plugins: [esbuldNamedExportsPlugin()],
});