babel-plugin-namespace-modules
raw JSON → 2.32.2 verified Sat Apr 25 auth: no javascript
A Babel plugin that prepends the root project's package name and version to AMD module names in define() and require() calls, preventing module name clashes across projects. This is particularly useful for sandboxing dependencies in Liferay environments, ensuring runtime dependencies match development dependencies. Version 2.32.2 is the latest stable release, maintained as part of the liferay-frontend-projects monorepo. It integrates with liferay-npm-bundler for consistent namespace handling.
Common errors
error Module not found: Can't resolve 'babel-plugin-namespace-modules' ↓
cause Missing npm install or incorrect plugin name in Babel config.
fix
Run: npm install --save-dev babel-plugin-namespace-modules
error Error: Plugin . is not a valid plugin, or is already loaded ↓
cause Using full package name 'babel-plugin-namespace-modules' instead of short name 'namespace-modules' in .babelrc.
fix
Use: { "plugins": ["namespace-modules"] }
error TypeError: Cannot read property 'someModule' of undefined ↓
cause Module named with a scoped package '@scope/pkg' is referenced without the plugin's transformation, causing alias mismatch.
fix
Ensure the plugin is applied and all AMD define/require calls use the namespaced name.
Warnings
gotcha Plugin only works with AMD-style define() and require() calls (e.g., from RequireJS). CommonJS or ES modules are not affected. ↓
fix Ensure your code uses AMD syntax or use a different plugin for CommonJS/ESM.
gotcha Relative module paths (starting with './') are not namespaced, which may cause unexpected sharing of local modules. ↓
fix Use absolute-looking module names if you want them namespaced, or configure a different plugin.
breaking Scoped package names are modified: '@scope/pkg' becomes '@my-project$scope/pkg' (prefix without '@'). This changes the module name significantly. ↓
fix Update any hardcoded references to scoped modules to match the new pattern.
deprecated The plugin is primarily designed for Liferay ecosystem and may not receive updates for non-Liferay setups. ↓
fix Consider using general-purpose module naming plugins if outside Liferay.
Install
npm install babel-plugin-namespace-modules yarn add babel-plugin-namespace-modules pnpm add babel-plugin-namespace-modules Imports
- default wrong
import plugin from 'babel-plugin-namespace-modules'correctmodule.exports = require('babel-plugin-namespace-modules') - BabelPluginNamespaceModules wrong
const { BabelPluginNamespaceModules } = require('babel-plugin-namespace-modules')correctconst plugin = require('babel-plugin-namespace-modules') - plugin as .babelrc wrong
{ "plugins": ["babel-plugin-namespace-modules"] }correct{ "plugins": ["namespace-modules"] }
Quickstart
// Install and configure in .babelrc to namespace AMD modules
// Ensure @babel/core is installed: npm install --save-dev @babel/core
// .babelrc
{
"plugins": ["namespace-modules"]
}
// Example code: define('my-module', ['dep'], function(dep) { return dep; });
// After transpilation: define('my-project$my-module', ['my-project$dep'], function(dep) { return dep; });