The `liferay-npm-bundler-plugin-replace-browser-modules` is a specialized plugin designed for the `liferay-npm-bundler`. Its core function is to facilitate the adaptation of Node.js modules for browser environments by processing the `browser` field (or other configured `aliasFields`) in `package.json` files. This allows developers to conditionally swap out or empty modules that are not browser-compatible, ensuring that frontend bundles are optimized for web usage within the Liferay DXP platform. The current stable version is 2.32.2. Updates for this plugin are typically aligned with the release cycle of the broader `liferay-frontend-projects` monorepo, which often sees multiple releases per quarter across its various sub-packages. It works in conjunction with `babel-plugin-alias-modules` to fully implement module aliasing, handling the external redirection part while the Babel plugin manages local requires. Its primary differentiator is its deep integration within the Liferay build toolchain, which is currently in a deprecated state, planned for future removal in Liferay 2024.Q4/Portal GA129.
npm install liferay-npm-bundler-plugin-replace-browser-modulesVerified import paths — ran on the pinned version, not inferred.
This `.npmbundlerrc` snippet adds `replace-browser-modules` as a plugin for all packages and explicitly configures it to use the `browser` field for aliasing. This is typically placed in the root of your Liferay widget project.
Explicitly configure `resolve.aliasFields` in your `.npmbundlerrc` if you need to alias fields other than the default `['browser']`. Ensure your `package.json` uses the `browser` field for browser-specific module replacements.
Ensure both `liferay-npm-bundler-plugin-replace-browser-modules` and `babel-plugin-alias-modules` are correctly configured in your Liferay build process to achieve full module aliasing.
For new projects or ongoing maintenance, consider migrating away from `liferay-npm-bundler` and its ecosystem to modern build tools as suggested by Liferay documentation. Refer to Liferay's migration guides for transitioning existing projects.
Verify the `browser` field in the module's `package.json` points to an existing file. Check your `.npmbundlerrc` to ensure `replace-browser-modules` is listed under `plugins` and that `resolve.aliasFields` is correctly configured.
This is a known limitation of `liferay-npm-bundler` v2. Workarounds include transpiling modules to CommonJS using Babel before the bundler runs, or considering a migration to modern bundlers like Webpack or esbuild that natively support ES modules.
Ensure your `.npmbundlerrc` has a valid `resolve` object with an `aliasFields` array, for example: `"config": { "resolve": { "aliasFields": ["browser"] } }` or within the plugin-specific configuration.