Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 Error: No provider for HttpClient

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!

like image 826
mrpotocnik Avatar asked Oct 04 '17 16:10

mrpotocnik


People also ask

How do I fix Nullinjectorror no provider for Httpclient?

Resolution. The issue is more due to not registering the required services i.e HttpClientModule in the root module ie. NgModule. As per Angular Design and Architecture every service (internal or external service) is required to be registered with root NgModule as required.

What is null injector error?

in Angular on May 16, 2020. The error occurs because of missing providers in app.module.ts. The error mostly occurs when we try to change one of the services and class and forget to update the providers in the app.module.

What is HTTP client module?

The HttpClientModule is a service module provided by Angular that allows us to perform HTTP requests and easily manipulate those requests and their responses. It is called a service module because it only instantiates services and does not export any components, directives or pipes.


1 Answers

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]     }); 
like image 50
Kevin Doyon Avatar answered Oct 01 '22 17:10

Kevin Doyon