babel-plugin-transform-export-default-name
raw JSON → 2.1.0 verified Sat Apr 25 auth: no javascript maintenance
Babel plugin that transforms export default of anonymous functions, arrow functions, and anonymous classes to named exports using the filename as the variable name. Version 2.1.0 is current. Released sporadically; last update was several years ago. Key differentiator: solves stack trace readability by assigning a derived name from the file (e.g., foo-bar.js becomes fooBar), improving debugging. Unlike other plugins that only handle function declarations, this targets anonymous/arrow function and class expressions. Works with Babel 6 and 7 (likely).
Common errors
error Error: Cannot find module 'babel-plugin-transform-export-default-name' ↓
cause Plugin not installed or missing from node_modules.
fix
npm install --save-dev babel-plugin-transform-export-default-name
error Error: Plugin/Preset files are not allowed to export objects, only functions. ↓
cause Using a very old Babel version that expects different plugin format (Babel 6 vs 7).
fix
Install @babel/core@^7.0.0 and ensure compatibility; consider alternative plugin.
error TypeError: Cannot assign to read only property 'name' of function ↓
cause Plugin incorrectly tries to assign name to a function that is already named; edge case with named default exports.
fix
Ensure the default export is anonymous; remove any explicit name declaration.
error Unexpected token, expected "export" (e.g., after transformation, syntax error) ↓
cause Plugin corrupts export due to misordered plugins or conflicting transforms.
fix
Ensure this plugin runs first in the plugin list before other export transforms.
Warnings
breaking Plugin may conflict with other Babel plugins that also transform export default (e.g., @babel/plugin-proposal-export-default-from). Order matters: run this plugin before others that re-export. ↓
fix Ensure this plugin is listed before any plugin that handles export-all or re-exporting.
deprecated Package has not been updated in years; may not support Babel 7 clearify. It is not officially maintained. ↓
fix Consider using @babel/plugin-proposal-export-default-from or monkey-patch Babel transform output for named functions.
gotcha Uses lodash.camelCase; filenames with unusual characters may produce unexpected variable names (e.g., numbers, underscores). ↓
fix Check transformed output for valid identifiers; avoid file names starting with digits or containing special chars.
gotcha Only transforms anonymous functions and arrow functions; named exports or non-function/class default exports remain unchanged. ↓
fix Use only for anonymous default exports; for named exports, use other tools.
gotcha Does not transform export default of anonymous class if the class is named in the same file (e.g., export default class Foo {} -> stays as Foo). Only unnamed classes are affected. ↓
fix Ensure the default export is truly anonymous; otherwise, no transformation occurs.
Install
npm install babel-plugin-transform-export-default-name yarn add babel-plugin-transform-export-default-name pnpm add babel-plugin-transform-export-default-name Imports
- plugin (default) wrong
module.exports = { plugins: ['babel-plugin-transform-export-default-name'] }correctmodule.exports = { plugins: ['transform-export-default-name'] } - plugin (programmatic API) wrong
require('babel-plugin-transform-export-default-name').defaultcorrectrequire('babel-plugin-transform-export-default-name') - ESM import wrong
import { plugin } from 'babel-plugin-transform-export-default-name'correctimport plugin from 'babel-plugin-transform-export-default-name'
Quickstart
// .babelrc
{
"plugins": ["transform-export-default-name"]
}
// foo.js
// Input: export default () => {};
// Output: let foo = () => {}; export default foo;