{"id":19204,"library":"babel-plugin-transform-modules-requirejs-babel","title":"babel-plugin-transform-modules-requirejs-babel","description":"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.","status":"active","version":"0.2.0","language":"javascript","source_language":"en","source_url":"https://github.com/prantlf/babel-plugin-transform-modules-requirejs-babel","tags":["javascript","babel-plugin","module","transform","babel","plugin","requirejs","requirejs-babel","requirejs-babel7"],"install":[{"cmd":"npm install babel-plugin-transform-modules-requirejs-babel","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-transform-modules-requirejs-babel","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-transform-modules-requirejs-babel","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Babel plugin requires @babel/core to function; peer dependency.","package":"@babel/core","optional":false},{"reason":"Runtime dependency for loading AMD modules in the browser; required for execution.","package":"requirejs","optional":true}],"imports":[{"note":"The plugin is used as a string in Babel config, not imported directly. Use the plugin name in .babelrc or babel.config.js.","wrong":"import transformModulesRequirejsBabel from 'babel-plugin-transform-modules-requirejs-babel'","symbol":"default","correct":"module.exports = { plugins: ['transform-modules-requirejs-babel'] }"},{"note":"resolvePath must be a function, not a string. It receives sourcePath and currentFile arguments.","wrong":"plugins: [['babel-plugin-transform-modules-requirejs-babel', { resolvePath: 'es6!' }]]","symbol":"resolvePath","correct":"['transform-modules-requirejs-babel', { resolvePath: (source, currentFile) => 'es6!' + source }]"},{"note":"This plugin is CJS-compatible. Using .default would be incorrect as it's not an ESM default export.","wrong":"const plugin = require('babel-plugin-transform-modules-requirejs-babel').default","symbol":"Plugin as CommonJS require","correct":"const plugin = require('babel-plugin-transform-modules-requirejs-babel'); module.exports = { plugins: [plugin] }"}],"quickstart":{"code":"// Install: npm i -D babel-plugin-transform-modules-requirejs-babel\n// .babelrc or babel.config.js\nmodule.exports = {\n  presets: [],\n  plugins: [\n    // Basic usage: no options\n    'transform-modules-requirejs-babel'\n  ]\n};\n\n// Optionally with custom resolvePath:\n// plugins: [\n//   [\n//     'transform-modules-requirejs-babel',\n//     {\n//       resolvePath(sourcePath, currentFile) {\n//         if (sourcePath.indexOf('!') < 0 && sourcePath !== 'require' &&\n//             sourcePath !== 'module' && sourcePath !== 'exports') {\n//           return 'es6!' + sourcePath;\n//         }\n//       }\n//     }\n//   ]\n// ]\n\n// Example ESM input 'src/main.js':\n// import { helper } from './helper';\n// export default function main() { helper(); }\n\n// After Babel transformation (AMD output):\n// define('src/main', ['./helper'], function (_helper) {\n//   'use strict';\n//   function main() { (0, _helper.helper)(); }\n//   return main;\n// });","lang":"javascript","description":"Shows basic Babel configuration with the plugin, optional resolvePath customization, and expected AMD output from an ESM module."},"warnings":[{"fix":"Upgrade to >=0.0.4 which fixes recognition of AMD modules without dependencies.","message":"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.","severity":"gotcha","affected_versions":"<0.0.4"},{"fix":"Always check sourcePath against 'require', 'module', 'exports' in resolvePath, as shown in the example.","message":"ResolvePath function must handle the three pseudo-modules 'require', 'module', 'exports' explicitly to avoid prefixing them.","severity":"gotcha","affected_versions":">=0.0.0"},{"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.","message":"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.","severity":"deprecated","affected_versions":">=0.1.0"},{"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.","message":"The plugin now transforms default exports without wrapping in { default: ... } to keep compatibility with RequireJS. This may break code expecting the default property wrapper.","severity":"breaking","affected_versions":">=0.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Run `npm install -D babel-plugin-transform-modules-requirejs-babel` to install it as a dev dependency.","cause":"The plugin is not installed or is missing from node_modules.","error":"Error: Module 'babel-plugin-transform-modules-requirejs-babel' not found"},{"fix":"Provide a function: `resolvePath(sourcePath, currentFile) { ... }` not a string like 'es6!source'.","cause":"The resolvePath option was provided as a string instead of a function.","error":"TypeError: resolvePath is not a function"},{"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']`.","cause":"Babel is not configured to transpile ES modules; the plugin may not be applied.","error":"SyntaxError: Unexpected token 'export'"},{"fix":"Include RequireJS script before your compiled modules. Example: `<script src='require.js' data-main='main'></script>`.","cause":"The AMD module output expects RequireJS to be loaded in the browser, but RequireJS is not present.","error":"ReferenceError: define is not defined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}