I'm trying to implement a federated module in Angular 15. I've tried multiple configurations but always fall into the same issue when trying to serve my remote:
main.ts:2 Error: Shared module is not available for eager consumption: 46269
at webpack_require.m.<computed> (consumes:145:1)
at webpack_require (bootstrap:19:1)
at 94666 (text-field.mjs:456:1)
at webpack_require (bootstrap:19:1)
at 34497 (animations.mjs:539:1)
at webpack_require (bootstrap:19:1)
at 25533 (vendor.module.ts:37:16)
at webpack_require (bootstrap:19:1)
at main.ts:1:1
at ZoneDelegate.invoke (zone.js:372:26)
Being that "46269" module @angular/common
.
This is my webpack.config.js
:
module.exports = withModuleFederationPlugin({
name: 'mfe1',
exposes: {
'./FederatedModule': './src/app/federation/federated.module.ts',
},
shared: share({
'@angular/core': { singleton: true, requiredVersion: '^15.0.0', strictVersion: true },
'@angular/common/': { singleton: true, requiredVersion: '^15.0.0', strictVersion: true },
'@angular/router': { singleton: true, requiredVersion: '^15.0.0', strictVersion: true },
'@angular/common/http': { singleton: true, requiredVersion: '^15.0.0', strictVersion: true },
}),
});
And yes, I have my main.ts
with:
import('./bootstrap')
.catch(err => console.error(err));
And my boostrap.ts
:
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
if (environment.production) {
enableProdMode();
}
platformBrowserDynamic()
.bootstrapModule(AppModule)
.catch((err) => console.error(err));
For reference, also my AppModule
:
@NgModule({
declarations: [AppComponent],
imports: [
HttpClientModule,
BrowserModule,
BrowserAnimationsModule,
RouterModule.forRoot(appRoutes, {}),
...
FederatedModule,
],
exports: [RouterModule],
providers: [
...
],
bootstrap: [AppComponent],
})
export class AppModule {}
And the FederatedModule
I'm trying to expose:
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { FederatedTestComponent } from './federated-test/federated-test.component';
const ROUTES: Routes = [{ path: 'federated', component: FederatedTestComponent }];
@NgModule({
declarations: [FederatedTestComponent],
imports: [CommonModule, RouterModule.forChild(ROUTES)],
})
export class FederatedModule {}
I've tried multiple configurations, removing the singleton: true
, setting the requiredVersion: auto
, etc. But always get the same issue. Also, trying to set eager: true
, results in the angular CLI not even compiling the project and giving me the error:
An unhandled exception occurred: Cannot read property 'import' of undefined
It's true that, if I remove the @angular/common
entry from my webpack.config.js
file, the remote is served correctly and I can even see load the Federated module there, but then the issue is in my host, where I get the error:
Error: inject() must be called from an injection context
Which as I found, it's due to multiple Angular versions being loaded as once, so I think the issue falls back to the remote problem, since I should be exporting @angular/common as I saw in many many examples. For reference, the webpack.config.js
of my host is the same as I'm showing above for the remote.
Also I read this post thoroughly: Shared module is not available for eager consumption - Angular 13 but none of the solutions seem to work for me.