Home > Back-end >  ERROR NullInjectorError: R3InjectorError(AppModule)[InjectionToken HTTP_INTERCEPTORS->[object Obj
ERROR NullInjectorError: R3InjectorError(AppModule)[InjectionToken HTTP_INTERCEPTORS->[object Obj

Time:08-09

Error Image:- Error Images I am facing this issue: ERROR NullInjectorError: R3InjectorError(AppModule)[InjectionToken HTTP_INTERCEPTORS -> [object Object] -> TransferState -> TransferState -> TransferState]: NullInjectorError: No provider for TransferState! ERROR Error: Cannot instantiate cyclic dependency! InjectionToken HTTP_INTERCEPTORS

Reference Link

I am using the following dependencies:

package.json

{
  "name": "felicity",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "device": "ng serve",
    "dev:ssr": "ng run Felicity:serve-ssr",
    "serve:ssr": "node dist/Felicity/server/main.js",
    "build:ssr": "ng build --prod && ng run Felicity:server:production",
    "prerender": "ng run Felicity:prerender"
  },
  "private": true,
  "dependencies": {
    "3": "^2.1.0",
    "-": "0.0.1",
    "@angular/animations": "~10.1.4",
    "@angular/cdk": "^10.2.7",
    "@angular/common": "~10.1.4",
    "@angular/compiler": "~10.1.4",
    "@angular/core": "~10.1.4",
    "@angular/fire": "6.0.3",
    "@angular/forms": "~10.1.4",
    "@angular/localize": "~10.1.4",
    "@angular/material": "^10.2.7",
    "@angular/platform-browser": "~10.1.4",
    "@angular/platform-browser-dynamic": "~10.1.4",
    "@angular/platform-server": "~10.1.4",
    "@angular/router": "~10.1.4",
    "@fortawesome/angular-fontawesome": "^0.8.2",
    "@fortawesome/fontawesome-free": "^5.15.4",
    "@fortawesome/fontawesome-svg-core": "^1.2.35",
    "@fortawesome/free-solid-svg-icons": "^5.15.3",
    "@fullcalendar/angular": "^5.6.0",
    "@fullcalendar/daygrid": "^5.6.0",
    "@fullcalendar/interaction": "^5.6.0",
    "@lottiefiles/lottie-player": "^1.5.6",
    "@ng-bootstrap/ng-bootstrap": "^8.0.4",
    "@ng-select/ng-option-highlight": "0.0.6",
    "@ng-select/ng-select": "^6.1.0",
    "@ng-toolkit/universal": "^8.0.1",
    "@nguniversal/express-engine": "^10.1.0",
    "@nguniversal/module-map-ngfactory-loader": "^8.2.6",
    "@teamhive/lottie-player": "^1.0.0",
    "@twilio/conversations": "^1.2.0",
    "@types/gapi": "0.0.39",
    "@types/gapi.auth2": "0.0.54",
    "@types/jquery": "^3.5.5",
    "@zoomus/websdk": "^2.5.0",
    "angular-animated-carousel": "0.0.4",
    "angular-google-charts": "^2.2.2",
    "angular-placeholder-shimmer": "^1.2.1",
    "angular-resize-event": "^3.0.2",
    "angularx-social-login": "^3.5.7",
    "animate.css": "^4.1.1",
    "animated-slider": "^1.1.6",
    "bootstrap": "^4.5.0",
    "clevertap-web-sdk": "^1.1.2",
    "cookie-parser": "^1.4.6",
    "cors": "^2.8.5",
    "detectrtc": "^1.4.1",
    "domino": "^2.1.6",
    "express": "^4.15.2",
    "firebase": "^7.24.0",
    "font-awesome": "^4.7.0",
    "fullcalendar": "^5.6.0",
    "hammerjs": "^2.0.8",
    "intl-tel-input": "^17.0.15",
    "jquery": "^1.12.4",
    "localstorage-polyfill": "^1.0.1",
    "lodash": "^4.17.21",
    "lottie-web": "^5.9.1",
    "magnific-popup": "^1.1.0",
    "moment": "^2.29.1",
    "moment-timezone": "^0.5.33",
    "ng-bootstrap-alert": "^2.0.0",
    "ng-image-fullscreen-view": "^2.0.0",
    "ng-lottie": "^0.3.2",
    "ng-speed-test": "^2.2.0",
    "ng2-tel-input": "^2.3.0",
    "ngx-bootstrap": "^6.2.0",
    "ngx-cookie-service": "^12.0.0",
    "ngx-device-detector": "^1.4.5",
    "ngx-editor": "^9.0.1",
    "ngx-international-phone-number": "^1.0.6",
    "ngx-lottie": "^7.0.4",
    "ngx-skeleton-loader": "^4.0.0",
    "ngx-slick-carousel": "^0.5.1",
    "popper.js": "^1.16.1",
    "rxjs": "~6.6.0",
    "scroll-out": "^2.2.12",
    "slick-carousel": "^1.8.1",
    "socket.io-client": "^4.0.1",
    "swiper": "^6.8.1",
    "tslib": "^2.0.0",
    "twilio-video": "^2.18.0",
    "upgrade-node-modules": "^1.3.5",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.1001.4",
    "@angular/cli": "^10.1.7",
    "@angular/compiler-cli": "~10.1.4",
    "@nguniversal/builders": "^10.1.0",
    "@types/express": "^4.17.0",
    "@types/jasmine": "~3.5.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/lodash": "^4.14.178",
    "@types/node": "^12.11.1",
    "codelyzer": "^6.0.0",
    "jasmine-core": "~3.6.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~5.0.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~3.0.2",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "protractor": "~7.0.0",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~4.0.2"
  }
}

http-transfer-state-interceptor.interceptor.ts

    import { Injectable, Inject, PLATFORM_ID } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent, HttpResponse } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { tap } from 'rxjs/operators';
import { TransferState, makeStateKey, StateKey } from '@angular/platform-browser';
import { isPlatformServer } from '@angular/common';

@Injectable({
  providedIn: 'root'
})
export class HttpTransferStateInterceptorService implements HttpInterceptor {
  debugger;
  constructor(private transferState: TransferState, @Inject(PLATFORM_ID) private platformId: any) { }

  public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

    const key: StateKey<string> = makeStateKey<string>(request.url);

    if (isPlatformServer(this.platformId)) {
      return next.handle(request).pipe(tap((event) => {
        this.transferState.set(key, (<HttpResponse<any>>event).body);
      }));
    } else {
      const storedResponse = this.transferState.get<any>(key, null);
      if (storedResponse) {
        const response = new HttpResponse({ body: storedResponse, status: 200 });
        this.transferState.remove(key);
        return of(response);
      } else {
        return next.handle(request);
      }
    }
  }
}

app.modules.ts

import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core";
import { RouterModule } from "@angular/router";
import { AppRoutingModule } from "./app-routing.module";
import { AppComponent } from "./app.component";
import { HeaderComponent } from "./header/header.component";
import { NgbModule } from "@ng-bootstrap/ng-bootstrap";
import { FontAwesomeModule } from "@fortawesome/angular-fontawesome";
import { UserLoginComponent } from "./auth/user-login/user-login.component";
import { UserSignupComponent } from "./auth/user-signup/user-signup.component";
import { ReactiveFormsModule } from "@angular/forms";
import { HttpClientModule, HTTP_INTERCEPTORS } from "@angular/common/http";
import { CommonModule } from "@angular/common";
import { AuthTokenInterceptor } from "./auth-token.interceptor";
import { ConfirmationDialogComponent } from "./confirmation-dialog/confirmation-dialog.component";
import { ErrorInterceptor } from "./error.interceptor";

import { SocialLoginModule, SocialAuthServiceConfig } from 'angularx-social-login';

import { NgxSkeletonLoaderModule } from 'ngx-skeleton-loader';
import { CookieService } from 'ngx-cookie-service';
import { HttpTransferStateInterceptorService } from "./http-transfer-state-interceptor.interceptor";
import { NgtUniversalModule } from '@ng-toolkit/universal';




@NgModule({
  declarations: [
    AppComponent,
    HeaderComponent,
    UserLoginComponent,
    UserSignupComponent,
   
  ],
  imports: [
    NgxSkeletonLoaderModule.forRoot({ animation: 'pulse' }),
    RouterModule,
    BrowserModule.withServerTransition({ appId: 'serverApp' }),
    AppRoutingModule, NgtUniversalModule,
    
    NgbModule,
    FontAwesomeModule,
    ReactiveFormsModule,
    HttpClientModule,
    CommonModule,
    SocialLoginModule,
   
  ],
  providers: [
    {
      provide: HTTP_INTERCEPTORS,
      useClass: AuthTokenInterceptor,
      multi: true,
    },
    {
      provide: HTTP_INTERCEPTORS,
      useClass: HttpTransferStateInterceptorService,
      multi: true,
    },
    {
      provide: HTTP_INTERCEPTORS,
      useClass: ErrorInterceptor,
      multi: true,
    }, CookieService
  ],
  bootstrap: [AppComponent],
  entryComponents: [ConfirmationDialogComponent],
})
export class AppModule { }

app.server.module.ts

import { NgModule } from '@angular/core';
import { ServerModule, ServerTransferStateModule } from '@angular/platform-server';

import { AppModule } from './app.module';
import { AppComponent } from './app.component';
import { ModuleMapLoaderModule } from '@nguniversal/module-map-ngfactory-loader';

@NgModule({
  imports: [
    AppModule, ServerTransferStateModule,
    ServerModule,
  ],
  bootstrap: [AppComponent],
})
export class AppServerModule { }

Can anyone help me to fix this issue?

CodePudding user response:

I can see that you missed importing BrowserTransferStateModule could that be the issue?

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule.withServerTransition({ appId: 'serverApp' }),
    BrowserTransferStateModule,
    CoreModule,
    // code omitted
  ],
  bootstrap: [AppComponent],
})
export class AppModule {}

reference

  • Related