Babel Helper Module Transforms

raw JSON →
7.0.0-beta.1 verified Sat Apr 25 auth: no javascript

Internal Babel helper package providing utility functions for implementing ES6 module transformations, such as converting imports and exports to CommonJS, AMD, or UMD. This package is used by plugins like @babel/plugin-transform-modules-commonjs and @babel/plugin-transform-modules-amd. Version 7.0.0-beta.1 is an early beta; current stable releases follow Babel 7.x line (v7.29.2 as of March 2026) and Babel 8 development on v8.0.0-rc.3. Users should not depend on this package directly; use the respective transform plugins instead. Key differentiator: it centralizes module transformation logic shared across Babel module plugins.

error TypeError: rewriteModuleStatementsAndPrepareHeader is not a function
cause Importing as a named export instead of default export.
fix
Change to import rewriteModuleStatementsAndPrepareHeader from '@babel/helper-module-transforms'.
error Cannot find module '@babel/helper-module-transforms'
cause Package not installed; it's a Babel internal dependency.
fix
Ensure you have @babel/core installed, or explicitly install @babel/helper-module-transforms.
error Error: Plugin attempted to use a helper that is not available
cause Version mismatch between Babel packages.
fix
Update all @babel/* packages to the same version.
breaking Default export changed from a named export to the default export in v7.0.0-beta.1
fix Use import rewriteModuleStatementsAndPrepareHeader from '@babel/helper-module-transforms' instead of importing a named export.
gotcha The package is internal; direct imports may break without notice across minor releases.
fix Prefer using higher-level Babel plugins or @babel/core APIs instead of directly depending on this helper.
deprecated Some helper functions like getModuleName have been deprecated in favor of configuration via @babel/core.
fix Use the 'moduleId' or 'getModuleId' options in your Babel configuration instead.
gotcha Only CommonJS and AMD are supported; no native ESM output.
fix If you need ESM output, use @babel/preset-env with modules: false or use a bundler like Rollup.
npm install babel-helper-module-transforms
yarn add babel-helper-module-transforms
pnpm add babel-helper-module-transforms

Shows how to use the default export to rewrite module statements and extract metadata from an AST.

import rewriteModuleStatementsAndPrepareHeader from '@babel/helper-module-transforms';
import traverse from '@babel/traverse';
import { parse } from '@babel/parser';

const code = 'import { foo } from "bar"; export const baz = 1;';
const ast = parse(code, { sourceType: 'module' });
traverse(ast, {
  Program(path) {
    const { meta, headers } = rewriteModuleStatementsAndPrepareHeader(path, {
      exportName: 'module.exports',
      loose: false,
    });
    console.log('Module meta:', meta);
    console.log('Module headers:', headers);
  }
});