While loading the host application I’m facing issue with the webpack module federation with the error “Initialization of sharing external failed: ScriptExternalLoadError: Loading script failed.”.
Here is my web pack configuration.
Host
//host-app/webpack.config.js
const HtmlWebpackPlugin = require("html-webpack-plugin");
const ModuleFederationPlugin = require("webpack/lib/container/ModuleFederationPlugin");
const { dependencies } = require("./package.json");
module.exports = {
entry: "./src/index",
mode: "development",
optimization: {
// runtimeChunk: 'single',
splitChunks: {
chunks: 'all',
cacheGroups: {
vendor: {
test: /[\/]node_modules[\/]/,
name: module => (module.context.match(/[\/]node_modules[\/](.*?)([\/]|$)/) || [])[1]
}
}
}
},
devServer: {
port: 3000,
open: true,
hot: true,
historyApiFallback: true,
headers: {
"Access-Control-Allow-Origin": "*",
},
},
module: {
rules: [
{
test: /.(js|jsx)?$/,
exclude: /node_modules/,
use: [
{
loader: "babel-loader",
options: {
presets: ["@babel/preset-env", "@babel/preset-react"],
},
},
],
},
{
test: /.css$/i,
use: ["style-loader", "css-loader"],
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: "./public/index.html",
favicon: "./public/favicon.ico",
manifest: "./public/manifest.json",
}),
new ModuleFederationPlugin({
name: "Remote CoreShell Client",
remotes: {
// TODO replace the url with base url
Remote: `Remote@http://localhost:3001/moduleEntry.js`,
},
shared: {
...dependencies,
react: {
singleton: true,
eager: true,
requiredVersion: dependencies["react"],
},
"react-dom": {
singleton: true,
eager: true,
requiredVersion: dependencies["react-dom"],
},
},
}),
],
resolve: {
extensions: [".js", ".jsx"],
},
target: "web",
}
Remote
const HtmlWebpackPlugin = require("html-webpack-plugin");
const path = require("path");
const { ModuleFederationPlugin } = require('webpack').container;
const TerserPlugin = require('terser-webpack-plugin');
const { dependencies } = require("./package.json");
module.exports = {
entry: "./src/index.js",
mode: "production",
devServer: {
static: {
directory: path.join(__dirname, "dist"),
},
port: 3001,
historyApiFallback: true,
// watchFiles: ["src/**/*"],
client: {
webSocketURL: 'ws://localhost:3000/ws',
},
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'dist'),
publicPath: 'http://localhost:3001/'
},
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
terserOptions: {
compress: {
drop_console: true, // Optional: Drop console statements
},
},
}),
],
splitChunks: {
chunks: 'all',
cacheGroups: {
vendor: {
test: /[\/]node_modules[\/]/,
name: 'vendor', // Fixed name for the vendor bundle
chunks: 'all',
},
},
}
},
module: {
rules: [
{
test: /.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react']
}
}
}, {
test: /.css$/,
use: ['style-loader', 'css-loader'],
},{
test: /.json$/,
loader: 'json-loader', // Add this rule for JSON files
type: 'javascript/auto',
},
{
test: /.(png|svg|jpg|gif)$/,
use: [
'file-loader'
]
},
{
test: /.(woff|woff2|eot|ttf|otf)$/,
use: [
'file-loader'
]
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: path.join(__dirname, "public", "index.html"),
publicPath: "http://localhost:3001/", // Add this to resolve PUBLIC_URL issues
inject: true,
}),
new ModuleFederationPlugin({
name: "Remote",
filename: "moduleEntry.js",
exposes: {
"./Dashboard": "./src/App",
},
shared: {
...dependencies,
react: {
singleton: true,
requiredVersion: dependencies["react"],
eager: true
},
"react-dom": {
singleton: true,
requiredVersion: dependencies["react-dom"],
eager: true
},
},
}),
],
resolve: {
extensions: [".js", ".jsx"],
},
target: "web",
};
The dependencies versions that I’m using for this application are .
“react”: “^18.3.1”,
“react-dom”: “^18.3.1”,
“webpack”: “^5.96.1”
I can see the module http://localhost:3001/moduleEntry.js loading in the remote app but having this issue.
Initialization of sharing external failed: ScriptExternalLoadError: Loading script failed.