babel-plugin-transform-modules-requirejs-babel
raw JSON → 0.2.0 verified Sat Apr 25 auth: no javascript
A Babel plugin for transforming ESM modules to AMD modules, designed to work with RequireJS and the requirejs-babel7 plugin. Current stable version 0.2.0 (May 2025). It replaces the combination of @babel/plugin-transform-modules-amd, babel-plugin-amd-checker, babel-plugin-amd-default-export, and babel-plugin-module-resolver-standalone, solving limitations around mixing ESM and AMD modules at any dependency level and handling default exports without wrapping. Supports customising module path resolution via a resolvePath function. Infrequent releases; maintained by a single author.
Common errors
error Error: Module 'babel-plugin-transform-modules-requirejs-babel' not found ↓
cause The plugin is not installed or is missing from node_modules.
fix
Run
npm install -D babel-plugin-transform-modules-requirejs-babel to install it as a dev dependency. error TypeError: resolvePath is not a function ↓
cause The resolvePath option was provided as a string instead of a function.
fix
Provide a function:
resolvePath(sourcePath, currentFile) { ... } not a string like 'es6!source'. error SyntaxError: Unexpected token 'export' ↓
cause Babel is not configured to transpile ES modules; the plugin may not be applied.
fix
Ensure the plugin is listed in your Babel config under 'plugins' (not 'presets') and that Babel is running on the file. Example:
plugins: ['transform-modules-requirejs-babel']. error ReferenceError: define is not defined ↓
cause The AMD module output expects RequireJS to be loaded in the browser, but RequireJS is not present.
fix
Include RequireJS script before your compiled modules. Example:
<script src='require.js' data-main='main'></script>. Warnings
gotcha The plugin assumes that AMD modules without dependencies (empty define) are not mixed with ESM; if you have AMD modules with no deps, they may be transformed incorrectly. ↓
fix Upgrade to >=0.0.4 which fixes recognition of AMD modules without dependencies.
gotcha ResolvePath function must handle the three pseudo-modules 'require', 'module', 'exports' explicitly to avoid prefixing them. ↓
fix Always check sourcePath against 'require', 'module', 'exports' in resolvePath, as shown in the example.
deprecated This plugin replaces a combination of four other plugins; if you are using those, migrate to this single plugin to avoid conflicts and missing features. ↓
fix Remove '@babel/plugin-transform-modules-amd', 'babel-plugin-amd-checker', 'babel-plugin-amd-default-export', 'babel-plugin-module-resolver-standalone' and use this plugin instead.
breaking The plugin now transforms default exports without wrapping in { default: ... } to keep compatibility with RequireJS. This may break code expecting the default property wrapper. ↓
fix Update consuming AMD modules to treat direct exports as the default value; if you need the wrapper, set the addDefaultProperty option (not yet implemented) or use babel-plugin-amd-default-export with addDefaultProperty: true before this plugin.
Install
npm install babel-plugin-transform-modules-requirejs-babel yarn add babel-plugin-transform-modules-requirejs-babel pnpm add babel-plugin-transform-modules-requirejs-babel Imports
- default wrong
import transformModulesRequirejsBabel from 'babel-plugin-transform-modules-requirejs-babel'correctmodule.exports = { plugins: ['transform-modules-requirejs-babel'] } - resolvePath wrong
plugins: [['babel-plugin-transform-modules-requirejs-babel', { resolvePath: 'es6!' }]]correct['transform-modules-requirejs-babel', { resolvePath: (source, currentFile) => 'es6!' + source }] - Plugin as CommonJS require wrong
const plugin = require('babel-plugin-transform-modules-requirejs-babel').defaultcorrectconst plugin = require('babel-plugin-transform-modules-requirejs-babel'); module.exports = { plugins: [plugin] }
Quickstart
// Install: npm i -D babel-plugin-transform-modules-requirejs-babel
// .babelrc or babel.config.js
module.exports = {
presets: [],
plugins: [
// Basic usage: no options
'transform-modules-requirejs-babel'
]
};
// Optionally with custom resolvePath:
// plugins: [
// [
// 'transform-modules-requirejs-babel',
// {
// resolvePath(sourcePath, currentFile) {
// if (sourcePath.indexOf('!') < 0 && sourcePath !== 'require' &&
// sourcePath !== 'module' && sourcePath !== 'exports') {
// return 'es6!' + sourcePath;
// }
// }
// }
// ]
// ]
// Example ESM input 'src/main.js':
// import { helper } from './helper';
// export default function main() { helper(); }
// After Babel transformation (AMD output):
// define('src/main', ['./helper'], function (_helper) {
// 'use strict';
// function main() { (0, _helper.helper)(); }
// return main;
// });