84

I am starting a new angular project with the CLI and am running into a no provider for HttpClient error.

I have added HttpClientModule to my module imports which seems to be the usual culprit in this scenario. Not finding a lot online other than that so I am not sure what the issue may be.

my app.module.ts

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

and my service

@Injectable()
export class FlexSearchService {


    constructor(private http: HttpClient) {}

    getSavedSearchResult(index: string, queryName: string, query: string ): Observable<any> {
      const url = `${environment.flexUrl}/${index}/search`;
      const item = new SearchQuery({queryName: queryName, variables: {q: query}});
      return this.http.post(url, item);
    }
}

and ng version gives the following output

@angular/cli: 1.4.2
node: 6.9.4
os: darwin x64
@angular/animations: 4.4.4
@angular/common: 4.4.4
@angular/compiler: 4.4.4
@angular/core: 4.4.4
@angular/forms: 4.4.4
@angular/http: 4.4.4
@angular/platform-browser: 4.4.4
@angular/platform-browser-dynamic: 4.4.4
@angular/router: 4.4.4
@angular/cli: 1.4.2
@angular/compiler-cli: 4.4.4
@angular/language-service: 4.4.4
typescript: 2.3.4

my tsconfig

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}

My test

import { TestBed, inject } from '@angular/core/testing';
import { HttpClientModule } from '@angular/common/http';
import { FlexSearchService } from './flex-search.service';

describe('FlexSearchService', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [FlexSearchService, HttpClientModule]
    });
  });
  it('should be created', inject([FlexSearchService], (service: FlexSearchService) => {
    expect(service).toBeTruthy();
  }));

Any help is GREATLY appreciated!

9
  • Can you post the exact error message? Commented Oct 4, 2017 at 16:06
  • 1
    Where did you provide FlexSearchService? Commented Oct 4, 2017 at 16:07
  • 3
    the error is being thrown with my tests. Why did not you say it in your question??? You should import HttpClientModule to your TestBed.configureTestingModule Commented Oct 4, 2017 at 16:19
  • 5
    You provided the module in providers, not in imports. Commented Oct 4, 2017 at 16:49
  • 1
    For me, it was forgetting to import HttpClientModule also in my service.spec.ts. Commented Nov 14, 2017 at 3:15

6 Answers 6

141

In your test

TestBed.configureTestingModule({
      providers: [FlexSearchService, HttpClientModule]
    });

It should be

TestBed.configureTestingModule({
      imports: [HttpClientModule],
      providers: [FlexSearchService]
    });

or even better (if you want to mock request):

TestBed.configureTestingModule({
      imports: [HttpClientTestingModule],
      providers: [FlexSearchService]
    });
Sign up to request clarification or add additional context in comments.

Comments

34

Import HttpClientTestingModule.

In your test:

import { HttpClientTestingModule } from '@angular/common/http/testing';

and in the configureTestingModule of your test, do the following:

TestBed.configureTestingModule({
    imports: [ HttpClientTestingModule ],
})
.compileComponents();

2 Comments

Yes, the line imports: [HttpClientTestingModule] is crucial.
this should be an accepted answer
12

An easier way is to provide it globally....Import the following into app.module.ts like so:

import { HttpModule } from '@angular/http'
import { HttpClient, HttpClientModule } from '@angular/common/http';

and declare it in imports:

  imports: [
    HttpModule,
    HttpClientModule, ...
]

Comments

9

For this you have to import the following:

import { HttpClientTestingModule } from '@angular/common/http/testing';

And for the spec file in the configureTestingModule of your test, do the following:

TestBed.configureTestingModule({
    imports: [ HttpClientTestingModule ],
})
.compileComponents();

Comments

7

For anyone come here after upgrading version of Angular and found out that HttpClientTestingModule is deprecated, you can use provideHttpClient() and provideHttpClientTesting() instead.

        providers: [
            provideHttpClient(),
            provideHttpClientTesting(),
   
        ]

Comments

1

I've noticed this issue when I was trying to link my angular library locally within my project using the npm link.

Downloading the library from the repository solved my issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.