6

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:

How can I fix this and use HttpClient ?

eko
  • 39,722
  • 10
  • 72
  • 98
user96649
  • 471
  • 1
  • 5
  • 22

1 Answers1

14

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?

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
  • 1
    This remove the error in the log, but now I get a XHR Error from the app: `Error loading http://localhost:3000/node_modules/@angular/common/bundles/common.umd.js/http as "@angular/common/http" from http://localhost:3000/app/app.module.js` – user96649 Aug 15 '17 at 08:05
  • @user96649, do you use `systemjs`? – Max Koretskyi Aug 15 '17 at 08:07
  • No, I installed by git cloning, and using npm install. – user96649 Aug 15 '17 at 08:33
  • look for `systemjs.config.js` file and add `'@angular/common/http': 'npm:@angular/common/bundles/common-http.umd.js',` – Max Koretskyi Aug 15 '17 at 08:37
  • Still no success: `Error loading http://localhost:3000/tslib as "tslib" from http://localhost:3000/node_modules/@angular/common/bundles/common-http.umd.js` – user96649 Aug 15 '17 at 09:51
  • you have to install it `npm i tslib` and update `systemjs.config.js` with `'tslib': 'npm:tslib/tslib.js'` – Max Koretskyi Aug 15 '17 at 09:56
  • This worked, thanks a lot ! You should probably add these details to the main answer. Why there is no information on any tutorial that it's needed to install with npm or update systemjs, that is a mystery. – user96649 Aug 15 '17 at 10:50