1

I'm trying to implement Module Federation with Angular and NgRX, but i'm facing a problem and don't know how to fix it.

The problem is: when X application lazy loads a module from Y application that uses Firebase, angular does not recognize the fire auth provider.

I have 2 apps: Auth and Dashboard.

My Auth application uses firebase to do user login.

The firebase request login is made by a NgRX effect:

    import {AngularFireAuth} from '@angular/fire/auth';

    @Injectable()
    export class AuthEffects {
      
      userLogin$: Observable<Action> = createEffect(() => {
        /* effect implementation */
      });
      
      constructor(private fireAuth: AngularFireAuth){}
    }

The AuthModule imports:

imports: [
    /* ...other imports */
    StoreModule.forFeature('authState', authReducer),
    EffectsModule.forFeature([AuthEffects]),
    AngularFireModule.initializeApp(firebaseConfig),
    AngularFireAuthModule
]

The Dashboard AppRoutingModule imports:

const routes: Routes = [
  {
      path: '',
      pathMatch: 'full',
      loadChildren: () => import('auth/AuthModule').then(m => m.AuthModule)
  }
];

@NgModule({
  imports: [
    RouterModule.forRoot(routes)
  ],
  exports: [RouterModule]
})

When I start only the Auth application, everything works fine and I can do login.

But when I try to use the Auth application remotely, inside Dashboard application, I get this error:

NullInjectorError: StaticInjectorError(AppModule)[InjectionToken angularfire2.app.options]: 
  StaticInjectorError(Platform: core)[InjectionToken angularfire2.app.options]: 
    NullInjectorError: No provider for InjectionToken angularfire2.app.options!

Partial Auth - webpack.config.js

    plugins: [
        new ModuleFederationPlugin({
          name: 'auth',
          library: {type: 'var', name: 'auth'},
          filename: 'remoteEntry.js',
          exposes: {
            './AuthModule': './src/app/login/auth.module.ts',
            './AuthComponent': './src/app/login/auth.component.ts',
            './AuthActions': './src/app/store/actions/auth.actions.ts',
            './AuthReducer': './src/app/store/reducers/auth.reducer.ts',
            './AuthEffects': './src/app/store/effects/auth.effects'
          },
          shared: {
            ...dependencies,
            '@angular/core': {
              requiredVersion: dependencies['@angular/core'],
              singleton: true,
            },
            '@angular/common': {
              requiredVersion: dependencies['@angular/common'],
              singleton: true,
            },
            '@angular/router': {
              requiredVersion: dependencies['@angular/router'],
              singleton: true,
            }
          }
        })
    ]

Parial Dashboard - webpack.config.js

    plugins: [
        new ModuleFederationPlugin({
          name: 'dashboard',
          library: {type: 'var', name: 'dashboard'},
          filename: 'remoteEntry.js',
          remotes: {
            auth: 'auth',
          },
          shared: {
            ...dependencies,
            '@angular/core': {
              requiredVersion: dependencies['@angular/core'],
              singleton: true,
            },
            '@angular/common': {
              requiredVersion: dependencies['@angular/common'],
              singleton: true,
            },
            '@angular/router': {
              requiredVersion: dependencies['@angular/router'],
              singleton: true,
            }
          }
        })
      ]

I tried to resolve it by myself, but Module Federation is a new thing and we have little posts about.

Can someone help me? If you came until here, thank you very much! :D

1 Answers1

2

Solved!

Thanks Zack Jackson, I could solve the problem.

Solution:

https://github.com/module-federation/module-federation.github.io/issues/14#issuecomment-672647713

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Vimal Patel Jan 07 '22 at 13:25