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 ?

7
  • 3
    you need to use version above 4.3 Commented Aug 15, 2017 at 7:44
  • 1
    @echonax, let's see if it fixes the error) Commented Aug 15, 2017 at 7:54
  • is that someone me ? :P just kidding Commented Aug 15, 2017 at 7:55
  • 1
    @echonax, yeah, I will Commented Aug 15, 2017 at 8:00
  • 1
    @user96649 Yes, change all the @angular-lines to this version: "@angular/common": "^4.3.4", then run npm install Commented Aug 15, 2017 at 8:00

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

7 Comments

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, do you use systemjs?
No, I installed by git cloning, and using npm install.
look for systemjs.config.js file and add '@angular/common/http': 'npm:@angular/common/bundles/common-http.umd.js',
Still no success: Error loading http://localhost:3000/tslib as "tslib" from http://localhost:3000/node_modules/@angular/common/bundles/common-http.umd.js
|

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.