简体   繁体   English

React Module Federation - 未捕获错误:共享模块不可用于急切消费:webpack/sharing/consume/default/react/react

[英]React Module Federation - Uncaught Error: Shared module is not available for eager consumption: webpack/sharing/consume/default/react/react

I am trying react module federation.我正在尝试反应模块联合会。 I have created two react apps(modulefederation1, modulefederation2) using create-react-app command.我使用 create-react-app 命令创建了两个反应应用程序(modulefederation1,modulefederation2)。 I am getting 'Uncaught Error: Shared module is not available for eager consumption: webpack/sharing/consume/default/react/react' error when i run the application.我在运行应用程序时收到“未捕获错误:共享模块不可用于急切消费:webpack/sharing/consume/default/react/react”错误。

Node version: v16.14.2节点版本: v16.14.2

Below is my code.下面是我的代码。

modulefederation1:模块联盟 1:

Package.json: Package.json:

{
  "name": "modulefederation1",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.4",
    "@testing-library/react": "^13.2.0",
    "@testing-library/user-event": "^13.5.0",
    "@types/jest": "^27.5.1",
    "@types/node": "^16.11.36",
    "@types/react": "^18.0.9",
    "@types/react-dom": "^18.0.4",
    "html-webpack-plugin": "^5.5.0",
    "react": "^18.1.0",
    "react-dom": "^18.1.0",
    "react-scripts": "5.0.1",
    "serve": "^13.0.2",
    "ts-loader": "^9.3.0",
    "typescript": "^4.6.4",
    "web-vitals": "^2.1.4",
    "webpack": "^5.72.1",
    "webpack-cli": "^4.9.2",
    "webpack-dev-server": "^4.9.0"
  },
  "scripts": {
    "start": "webpack serve --open",
    "build": "webpack --config webpack.prod.js",
    "serve": "serve dist -p 3002",
    "clean": "rm -rf dist"
},
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

webpack.config.js: webpack.config.js:

const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require('webpack'); // only add this if you don't have yet
const { ModuleFederationPlugin } = webpack.container;
const deps = require('./package.json').dependencies;

module.exports = (env, argv) => {
  const isProduction = argv.mode === 'production';
  console.log({ isProduction });
  return {
    entry: './src/index.tsx',
    mode: process.env.NODE_ENV || 'development',
    devServer: {
      port: 3000,
      open: true,
    },
    resolve: {
      extensions: ['.ts', '.tsx', '.js'],
    },
    module: {
      rules: [
        {
          test: /\.(js|jsx|tsx|ts)$/,
          loader: 'ts-loader',
          exclude: /node_modules/,
        },
      ],
    },

    plugins: [
      new ModuleFederationPlugin({
        name: 'container',
        remotes: {
          app1: 'app1@http://localhost:3001/remoteEntry.js'
        },
        shared: {
          ...deps,
          react: { singleton: true, eager: true, requiredVersion: deps.react },
          'react-dom': {
            singleton: true,
            eager: true,
            requiredVersion: deps['react-dom'],
          },
        },
      }),
      new HtmlWebpackPlugin({
        template: './public/index.html',
      }),
    ],
  };
};

public/index.html:公共/index.html:

<html>
  <head>
    <title>CONTAINER</title>
  </head>
  <body>
    <div id="root"></div>
  </body>
</html>

src/index.tsx:源代码/index.tsx:

import './bootstrap';

src/bootstrap.tsx:源代码/bootstrap.tsx:

import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import reportWebVitals from './reportWebVitals';

const root = ReactDOM.createRoot(
  document.getElementById('root') as HTMLElement
);
root.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);

src/App.tsx:源代码/App.tsx:

import React from 'react';

const ModuleFederationTwo = React.lazy(() => import('app1/ModuleFederationTwo'));

function App() {
  return (
    <div >
     Container application
    </div>
  );
}

export default App;

modulefederation2:模块联合会2:

Pacakge.json file is same as modulefederation1 except "name": "modulefederation2" Pacakge.json文件与 modulefederation1 相同,除了“name”:“modulefederation2”

webpack.config.js: webpack.config.js:

const HtmlWebpackPlugin = require('html-webpack-plugin');
const { ModuleFederationPlugin } = require('webpack').container;
const path = require('path');
const deps = require('./package.json').dependencies;

module.exports = {
  entry: './src/index.tsx',
  mode: 'development',
  devServer: {
    port: 3001,
    open: true,
  },
  resolve: {
    extensions: ['.ts', '.tsx', '.js'],
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx|tsx|ts)$/,
        loader: 'ts-loader',
        exclude: /node_modules/,
      },
    ],
  },
  plugins: [
    new ModuleFederationPlugin({
      name: 'app1',
      filename: 'remoteEntry.js',
      exposes: {
        // expose each component
        './ModuleFederationTwo': './src/App',
      },
      shared: {
        ...deps,
        react: { singleton: true, eager: true, requiredVersion: deps.react },
        'react-dom': {
          singleton: true,
          eager: true,
          requiredVersion: deps['react-dom'],
        },
      },
    }),
    new HtmlWebpackPlugin({
      template: './public/index.html',
    }),
  ],
};

src/index.tsx and src/bootstrap.tsx file is same as modulefederation1 src/index.tsx 和 src/bootstrap.tsx文件与 modulefederation1 相同

src/App.tsx:源代码/App.tsx:

import React from 'react';
import './App.css';

function App() {
  return (
    <div className="App">
      MFE application One
    </div>
  );
}

export default App;

I run both modulefederation1, modulefederation2 using yarn start .我使用yarn start运行 modulefederation1、modulefederation2。

I get main.js:1312 Uncaught Error: Shared module is not available for eager consumption: webpack/sharing/consume/default/react/react error when i run modulefederation1 application.当我运行 modulefederation1 应用程序时,出现main.js:1312 Uncaught Error: Shared module is not available for eager consumption: webpack/sharing/consume/default/react/react错误。

When i browsed everyone say that index code should be moved to bootstrap which i have done already.当我浏览时,每个人都说应该将索引代码移至我已经完成的引导程序。 Do i have any other solution?我还有其他解决方案吗?

Network:网络:

网络日志

I was able to make above code work by changing the below code.通过更改下面的代码,我能够使上面的代码工作。

modulefederation1:模块联盟1:

public/index.html:- add script tag <script src="http://localhost:3001/remoteEntry.js"></script> public/index.html:-添加脚本标签<script src="http://localhost:3001/remoteEntry.js"></script>

<html>
  <head>
    <script src="http://localhost:3001/remoteEntry.js"></script>
    <title>CONTAINER</title>
  </head>
  <body>
    <div id="root"></div>
  </body>
</html>

webpack.config.js: Change remotes app1 value webpack.config.js:更改遥控器 app1 的值

remotes: {
          app1: 'app1'
        },

Add export {} inside src/index.tsx after importing the bootstrap导入 bootstrap 后在 src/index.tsx 中添加export {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的错误:共享模块不可用于热切使用 - Uncaught Error: Shared module is not available for eager consumption Webpack 模块联合反应 17.0.2 - Webpack Module Federation react 17.0.2 Webpack 模块联合不适用于急切的共享库 - Webpack module federation is not working with eager shared libs Webpack Module Federation React 动态导入 - Webpack Module Federation React dynamic import NextJS 中的 Webpack 模块联合 React 版本问题 - Webpack Module Federation React Version issue in NextJS Webpack 模块联合和 react-router-dom - Webpack module federation and react-router-dom 使用模块联合反应微前端 - React microfrontend with Module Federation Webpack5 模块联合:“未捕获的错误:找不到 react-redux 上下文值; 请确保组件被包裹在一个<provider> ”</provider> - Webpack5 Module Federation: “Uncaught Error: could not find react-redux context value; please ensure the component is wrapped in a <Provider>” 未捕获的类型错误:__WEBPACK_IMPORTED_MODULE_0_react___default.a.createContext 不是 function - Uncaught TypeError: __WEBPACK_IMPORTED_MODULE_0_react___default.a.createContext is not a function Gatsby Module Federation CORS 错误和急切消费问题 - Gatsby Module Federation CORS error and eager consumption issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM