Uncaught Error: Shared module is not available for eager consumption

No alt text provided for this image

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

No alt text provided for this image
No alt text provided for this image


========

Sharing React


Let's add react to Products Microfrontend as follows

  1. Stop the project with ctrl + c
  2. Add react as a shared module in shared/Products/webpacl.config.js

No alt text provided for this image
shard: ['react]        

Now let's start the project again.

yarn run start        
No alt text provided for this image

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

No alt text provided for this image

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');        
No alt text provided for this image
No alt text provided for this image


save both files and get back to the browser and refresh. You will see that it is rendering and the error disappeared.

No alt text provided for this image

This solution will allow webpack to load the dependencies before loading bootstrap.js





Vlad Kosmach

Full stack Engineer

1 年

Hi, i have a q, what if one of project is a react ts project?

回复
Chayan Roy

Web Developer (Javascript, Angular, Vue ,Laravel)

1 年

You saved my time man ?? . Thanks a lot

Murali Rander

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?

  • 该图片无替代文字
回复
Sarvesh Kesharwani

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

Farhad Nowzari

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

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了