Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error TS2307: Cannot find module '@angular/common/http' for HttpClientModule

Tags:

import

angular

I am trying to import HttpClientModule using the following line in my app.module.ts:

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

and

imports: [
    ...
    HttpClientModule,
    ...
]

However I keep getting the same error:

src/app/app.module.ts(4,38): error TS2307: Cannot find module '@angular/common/http'.

Despite those exact lines being included in these tutorials:

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

  • https://medium.com/codingthesmartway-com-blog/angular-4-3-httpclient-accessing-rest-web-services-with-angular-2305b8fd654b

  • http://blog.ninja-squad.com/2017/07/17/http-client-module/

How can I fix this and use HttpClient ?

like image 434
user96649 Avatar asked Aug 15 '17 07:08

user96649


People also ask

What is difference between HTTP and HttpClient in angular?

The HttpClient is used to perform HTTP requests and it imported form @angular/common/http. The HttpClient is more modern and easy to use the alternative of HTTP. HttpClient is an improved replacement for Http.

Which declares HttpClient has not been processed correctly by Ngcc?

This likely means that the library (@angular/common/http) which declares HttpClientModule 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.


1 Answers

The new http client was only added in the 4.3 version. You're probably using older angular version. You need to update the project. Go into package.json and modify all @angular/... entries to include ^4.3.4 version.

Also, if you're using SystemJS you need to add two entries to the map:

'tslib': 'npm:tslib/tslib.js',
'@angular/common/http': 'npm:@angular/common/bundles/common-http.umd.js',

Also see Difference between HTTP and HTTPClient in angular 4?

like image 64
Max Koretskyi Avatar answered Oct 20 '22 06:10

Max Koretskyi