Build with AOT: No provider for CoreAppProvider


#1

Hi,

I have an app that works fine in dev mode. Now I’m building it in prod mode because the app is almost ready. The app compiles fine, but when I open it I get this error:

Uncaught Error: StaticInjectorError(AppModule)[AddonNotificationsModule -> CoreAppProvider]: 
  StaticInjectorError(Platform: core)[AddonNotificationsModule -> CoreAppProvider]: 
    NullInjectorError: No provider for CoreAppProvider!
    at _NullInjector.get (core.js:1003)
    at resolveToken (core.js:1301)
    at tryResolveToken (core.js:1243)
    at StaticInjector.get (core.js:1111)
    at resolveToken (core.js:1301)
    at tryResolveToken (core.js:1243)
    at StaticInjector.get (core.js:1111)
    at resolveNgModuleDep (core.js:10896)
    at _callFactory (core.js:10960)
    at _createProviderInstance$1 (core.js:10910)

I don’t understand why is this happening, this provider is declared in my app.module.ts:

import { CoreAppProvider } from '@providers/app';

...

    providers: [
        CoreAppProvider,
        ...
    ]

@providers is an alias that we defined in our own webpack.config.js.

AddonNotificationsModule is a separate module that uses CoreAppProvider in its constructor. This module is imported in app.module.ts:

import { AddonNotificationsModule } from '@addon/notifications/notifications.module';

...

    imports: [
        ...
        AddonNotificationsModule,
        ...

Any idea of what could be going on? How can I debug this error?

Thank you!


#2

try to build your application with --preserve-symlinks


#3

Thanks, I didn’t try preserve-symlinks because I was able to fix it. I was doing this because I’m using CORE_PROVIDERS elsewhere:

export const CORE_PROVIDERS: any[] = [
    CoreAppProvider,
    ...
];

    providers: CORE_PROVIDERS.concat([
         {
            provide: HTTP_INTERCEPTORS,
            useClass: CoreInterceptor,
            multi: true,
        },
    ])

It seems AOT doesn’t like this way of declaring providers. I just copied all my providers to the providers declaration and now it works:

    providers: [
         CoreAppProvider,
         ...
         {
            provide: HTTP_INTERCEPTORS,
            useClass: CoreInterceptor,
            multi: true,
        },
    ]