babel-plugin-name-amd-modules
raw JSON → 2.32.2 verified Sat Apr 25 auth: no javascript
A Babel plugin that gives a name to AMD modules based on the module's path and package name. Version 2.32.2 is the current stable release. This plugin is maintained by Liferay and is part of the JS Toolkit ecosystem. It is specifically designed for AMD module systems, automatically generating named define() calls using package name and relative path. Unlike generic AMD plugins, it integrates with Liferay's build tooling and respects .npmbundlerrc configuration. Release cadence is irregular, with version bumps tied to the monorepo releases. Works with Babel 7+ and is intended for legacy AMD codebases or custom module systems.
Common errors
error Error: Cannot find module 'babel-plugin-name-amd-modules' ↓
cause Plugin not installed or not in node_modules.
fix
Run 'npm install --save-dev babel-plugin-name-amd-modules' to install the plugin.
error Plugin name-amd-modules not found ↓
cause Incorrect plugin name in Babel config (used full name 'babel-plugin-name-amd-modules' instead of short form).
fix
Use '"plugins": ["name-amd-modules"]' without the 'babel-plugin-' prefix.
error TypeError: Cannot read property 'relativePath' of undefined ↓
cause Plugin is processing a file outside the configured srcPrefixes or with missing package.json.
fix
Set srcPrefixes option to include the directory containing the file, or ensure package.json exists and is valid.
Warnings
gotcha Plugin only transforms AMD define() calls that match the exact pattern; anonymous define() without second argument (dependency array) may not be processed. ↓
fix Ensure define() calls always have at least two arguments: dependencies array and factory function.
deprecated The default packageName reads from package.json, which may be fragile in monorepo setups without proper source prefixes. ↓
fix Set packageName explicitly in plugin options to avoid relying on automatic detection.
gotcha The plugin expects AMD define to be a global function; if define is imported from a library (e.g., requirejs), the plugin may not recognize it. ↓
fix Ensure define is a global in the code being transformed; avoid importing define from another module.
breaking In version 2.x, source prefixes from .npmbundlerrc are automatically applied only when transforming files inside those directories; files outside may get incorrect relative paths. ↓
fix Explicitly configure srcPrefixes option to include all applicable source directories.
gotcha The plugin removes the .js extension from the module name; do NOT include .js in imports as it will cause mismatches. ↓
fix Use extensionless module paths in define() calls (e.g., define('pkg@1.0.0/index', ...)).
Install
npm install babel-plugin-name-amd-modules yarn add babel-plugin-name-amd-modules pnpm add babel-plugin-name-amd-modules Imports
- default wrong
import plugin from 'babel-plugin-name-amd-modules';correctmodule.exports = require('babel-plugin-name-amd-modules'); // in .babelrc plugin array - name-amd-modules wrong
{ "plugins": ["babel-plugin-name-amd-modules"] }correct// In .babelrc or babel.config.js: { "plugins": ["name-amd-modules"] } - require('babel-plugin-name-amd-modules') wrong
import plugin from 'babel-plugin-name-amd-modules';correctmodule.exports = function(api) { api.cache(true); const plugin = require('babel-plugin-name-amd-modules'); return { plugins: [plugin] }; };
Quickstart
// 1. Install
// npm install --save-dev babel-plugin-name-amd-modules
// 2. Configure .babelrc
{
"plugins": [
[
"name-amd-modules",
{
"packageName": "my-package",
"srcPrefixes": ["src"]
}
]
]
}
// 3. Input file (src/index.js):
define(['dep'], function(dep) {
// module code
});
// 4. Build output:
define('my-package@1.0.0/index', ['dep'], function(dep) {
// module code
});