Uncaught Error: Shared module is not available for eager consumption
Rany ElHousieny, PhD???
Senior Software Engineering Manager (EX-Microsoft) | Generative AI Leader @ Clearwater Analytics | Generative AI, Conversational AI Solutions Architect
If you are trying to share modules between Module Federation microfrontends, you might face the following strange error.
Uncaught Error: Shared module is not available for eager consumption
In this article, I will explain the reasons you might see this error and ways to resolve the issue
=======================
Reproduce the Error
First, let's reproduce the error to understand it better. Please, follow along with me.
Clone the project from the different Github repositories
Follow the following article to clone the three microfrontends from Github
You will have products and other microfrontends. Let's make sure the developer tools are running on the Products microfrontend
========
Sharing React
Let's add react to Products Microfrontend as follows
shard: ['react]
Now let's start the project again.
yarn run start
When you refresh https://localhost:8001, you will see the error in the development tools console as shown above. Also, you will notice that it failed to render the content.
==============
Solving the issue
If you look, again, into the network tab, you will notice that it is loading the dependency (React) Late. We need to delay this by moving the code into a bootstrap that we import in the index.js
The solution is to rename index.js to bootstrap.js and create a new file called index.js and put inside it
import('./bootstrap.js');
save both files and get back to the browser and refresh. You will see that it is rendering and the error disappeared.
This solution will allow webpack to load the dependencies before loading bootstrap.js
Full stack Engineer
1 年Hi, i have a q, what if one of project is a react ts project?
Web Developer (Javascript, Angular, Vue ,Laravel)
1 年You saved my time man ?? . Thanks a lot
SaaS Product Designer | Expert in User-Centric Designs, Prototyping, and Interaction Flows | Specializing in React, Redux & Frontend Optimization
1 年I am facing this similar issue for babel-polyfill. In my webpack.base.config file, I have- entry: { bundle: ['babel-polyfill', './main.js'], devHeader: ['./devHeader.js'], vendor: ['react', 'redux', 'react-redux', 'redux-thunk', 'react-bootstrap', 'react-router', 'react-intl'], }, How can I solve this error?
Vice President Software Engineer @ Citi | Ex-Apple | Ex-Amazon | Ex-Deloitte
1 年I wasted my whole day just trying to find the issue in my code. You saved my day. Thanks
Senior FullStack Developer @ GIS GmbH | Computer Software Engineering
2 年this works, as long as it is ok if you are returning a promise in the main/index.js file. I'm using SingleSpa and SingleSpa complains that it needs its own exports to be back in the main file and not a promise with the following link. https://single-spa.js.org/error/?code=37&arg=application&arg=@am/main&arg={ I'm checking on both sides how to solve the issue that while using single-spa to unify everything in one container-app we still can use something like module-federations to share components between these microfrontends