babel-plugin-amd-to-esm

raw JSON →
2.0.4 verified Sat Apr 25 auth: no javascript

A Babel plugin that converts AMD modules (using define()) to ES modules (import/export). Version 2.0.4 includes features like arrow function support, handling ES module dependencies (since v2.0.0), and options for rootDir and alias. It is actively maintained with bug fixes and documentation updates. Key differentiator: it specifically targets AMD-to-ESM conversion for legacy codebases, with support for handling mixed AMD/ESM dependency graphs, unlike generic AMD transform tools.

error Error: [BABEL] unknown: Plugin babel-plugin-amd-to-esm is not a valid plugin
cause Package not installed, or Babel version incompatibility (requires @babel/core 7.x).
fix
npm install --save-dev @babel/core babel-plugin-amd-to-esm
error Error: Cannot find module 'babel-plugin-amd-to-esm'
cause Plugin not installed in project's node_modules.
fix
npm install --save-dev babel-plugin-amd-to-esm
error TypeError: Cannot destructure property 'default' of 'undefined' or null.
cause AMD dependency resolution failed because handleESMDependencies is enabled but the dependency is not found or rootDir/alias misconfigured.
fix
Ensure rootDir and alias point to existing directories and dependency paths are correct.
gotcha Options handleESMDependencies, rootDir, and alias are only effective in v2.0.0+ and require handleESMDependencies: true to activate.
fix Upgrade to >=2.0.0 and set handleESMDependencies: true to use these options.
deprecated Using require() instead of import in Babel config is deprecated in Babel 7.8+; prefer using JS config files with module.exports or .mjs.
fix Use .babelrc.js with module.exports = { ... } or .babelrc.mjs for ESM.
gotcha The plugin does not support dynamic AMD require() calls; only static define() is converted.
fix Manually refactor dynamic requires or use a different tool for dynamic imports.
breaking In v2.0.0, default import of dependencies changed: previously a default import was always generated, now it generates named imports if possible.
fix Check generated imports and update code relying on default import behavior; migrate to v2.0.0.
npm install babel-plugin-amd-to-esm
yarn add babel-plugin-amd-to-esm
pnpm add babel-plugin-amd-to-esm

Configures babel-plugin-amd-to-esm in .babelrc.js with all options: handleESMDependencies, rootDir, and alias.

module.exports = {
  plugins: [
    ['babel-plugin-amd-to-esm', {
      handleESMDependencies: true,
      rootDir: path.resolve(__dirname, 'src'),
      alias: { '@': path.resolve(__dirname, 'src') }
    }]
  ]
};