Getting Error with HttpClient Module in angular
Asked Answered
G

10

13

ERROR in node_modules/@angular/common/http/http.d.ts:81:22 - error NG6002: Appears in the NgModule.imports of AppModule, but could not be resolved to an NgModule class.

This likely means that the library (@angular/common/http) which declares HttpClient has not been processed correctly by ngcc, or is not compatible with Angular Ivy. Check if a newer version of the library is available, and update if so. Also consider checking with the library's authors to see if the library is expected to be compatible with Ivy.

81 export declare class HttpClient {

Gershom answered 8/4, 2020 at 5:35 Comment(1)
{ "scripts": { "postinstall": "ngcc" } }Observable
W
31

Maybe you have imported HttpClient instead ofHttpClientModule. Check imports in app.module.ts

imports: [  
    BrowserModule,
    AppRoutingModule,
    BrowserAnimationsModule,
    MatToolbarModule,
    FlexLayoutModule,
    HttpClientModule
  ],
Weigle answered 1/5, 2020 at 16:13 Comment(0)
L
6

That precisely is, when it comes to importing it should be HttpClientModule and when it comes to usage it should be HttpClient

Reference: https://angular.io/guide/http

Lenticel answered 23/5, 2020 at 15:24 Comment(0)
B
2
  1. npm cache verify

  2. npm cache clean --force

Beker answered 14/12, 2020 at 9:22 Comment(0)
B
1

I faced Same Issue in AngularJs Version 13:

Error: node_modules/@angular/common/http/http.d.ts:91:22 - error NG6002: Appears in the NgModule.imports of AppModule, but could not be resolved to an NgModule class

This likely means that the library (@angular/common/http) which declares HttpClient has not been processed correctly by ngcc, or is not compatible with Angular Ivy. Check if a newer version of the library is available, and update if so. Also consider checking with the library's authors to see if the library is expected to be compatible with Ivy.

Solution : That Error is Our HttpClientModule is Not Import In our

app.module.ts file

Then You Can Add HttpClientModule to imports Area. *

@NgModule({
  declarations: [AppComponent],
  // After import here Above Import API
  imports: [
    BrowserModule,
    AppRoutingModule,
    BrowserAnimationsModule,
    HttpClientModule,
  ],
  providers: [],
  bootstrap: [AppComponent],
})
export class AppModule {}
Blab answered 27/5, 2022 at 1:6 Comment(0)
M
0

I had the same problem. It's working to me.

Finish serve project to : Crtl+c, npm install and build again ng serve --open

Martita answered 2/11, 2020 at 18:14 Comment(0)
O
0

I had the same problem, I just ran the following command and it worked for me.

npm cache verify

If it doesn't work, manually delete node_module and reinstall it.

rm -rf node_modules
npm install

And also you need to add HttpClientModule just after BrowserModule. Otherwise, it won't work. See below.

imports: [  
   BrowserModule,
   HttpClientModule,
   AppRoutingModule,
   BrowserAnimationsModule,
   MatToolbarModule,
   FlexLayoutModule
],
Often answered 10/1, 2021 at 5:28 Comment(0)
F
0

In app.module.ts => import { HttpClientModule } from '@angular/common/http';

In the service => import { HttpClient } from '@angular/common/http';

Forthcoming answered 24/2, 2021 at 14:50 Comment(0)
I
0

The HttpClientModule needs to be imported under @NgModule section. So make sure it is under "imports" as opposed to the "declaration" section. This should resolve the problem as in: imports: [ BrowserModule, HttpClientModule, FormsModule, AppRoutingModule, ],

Incommodious answered 13/1, 2023 at 18:34 Comment(0)
F
0

I made it work!

  1. At app.module.ts, add HttpClientModule import, not HttpClient

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

  1. At app.module.ts, add HttpClientModule in imports section

`

imports: [  
BrowserModule,
AppRoutingModule,
FormsModule,
HttpClientModule
  ],

`

  1. At the **service.ts, do not import HttpClientModule. add HttpClient import instead.

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

I hope this is clear now!

Fauces answered 20/6, 2023 at 10:44 Comment(1)
Welcome to SO and thanks for sharing. This answer seems to be the same as the other existing answers here. Going forward, try to help improve existing answers instead of creating duplicates.Photomural
C
0

Just try to run the following command

npm install
Camara answered 6/7, 2023 at 8:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.