menfon
menfon

Reputation: 1817

"Invalid provider for the NgModule" when using AoT

Without AoT everything works fine. But after switching loaders, I am getting this error and have no clue how to fix it, or what exactly is wrong.

Invalid provider for the NgModule 'AppModule in /xxx/src/app/app.module.ts' - only instances of Provider and Type are allowed, got: [LoggerService in /xxx/src/app/services/logger-service.service.ts, LocalStorageService in /xxx/node_modules/angular-2-local-storage/dist/local-storage.service.d.ts, WindowService in /xxx/src/app/services/window.service.ts, ?null?, ...]

Related code from AppModule:

import CustomHttp from './services/custom-http.service';

...

@NgModule({

...

  providers: [
    LocalStorageService,
    WindowService,
    AuthService,
    LoggerService,
    CustomHttp,
    AuthTokenStore,
    AuthService,
    SchemaValidator,
    AuthInterceptor,
    DataService,
    ErrorHelper,
    FileUpload,
    {provide: ErrorHandler, useClass: LoggingErrorHandler},
    NonAngularDialogsHelper,
    ConfirmationService,
    SearchHelper,
    FormHelper,
    DebugHelper,
    PuxApplication,
    StoreUtils,
    TranslationHelper,
    MessagesService,
    CustomValidatorsService,
    GeolocationService,
    SavedSearchesService,
    LoggedInfoService,
    BeTranslate,
    CountryHelper,
    SuggestionGenerators,
    PrimeNgHelper,
    UrlHelper,
    DocumentClickService,
    NavigationHelper,
    BeErrorsInterceptor,
    DocumentService,
    ScrollHelper,
    LinkDialogHelper,
    HtmlUtilsService,
    RouterHelperService,
    StripeService,
    VatExamplesService,
    ContactInfoHelper,
    WizardHelper,
    PasswordChangingPagesHelper,
    LandingPageHelper,
    TrackingEventsHelper,
    RfoHelper,
    ReactiveFormsHelper,
    LiveChatService,

    CounterActions
  ]

...

Snippets from CustomHttp:

...

@Injectable()
export default class CustomHttp {

...

  constructor(private http: Http,
              loggerService: LoggerService) {
    this.logger = loggerService.createLogger('CustomHttp');
  }

...

Edit 1: Added whole providers array as requested.

Upvotes: 1

Views: 2209

Answers (2)

Shakoor Hussain Attari
Shakoor Hussain Attari

Reputation: 1821

For me, I have included duplicate export statements for some services in service's index.ts file. Finally it works after removing one.

(you can use Excel to find out duplicates. :-) )

got from this link

.....
export * from './custom-message.service';
export * from './department.service';
export * from './custom-message.service'; // duplicated, remove this
.......

Upvotes: 0

menfon
menfon

Reputation: 1817

AoT compiler is not very clever. Turns out that it can't handle default imports for some reason...

Not working:

import CustomHttp from './services/custom-http.service';

Working:

import { CustomHttp } from './services/custom-http.service';

Upvotes: 6

Related Questions