4

I found a similar question but it was for angular2 & the commands in the answers seems angular2 specific or telling to run the same cmd I am running, so I am asking this question here.

I am following the Angular 5 tour of heroes tutorial & in the section of HTTP, I got the error below:

Failed to compile.

src/app/app.module.ts(13,48): error TS2307: Cannot find module '@angular-in-memory-web-api'.

After running the cmd, I got some warnings but it confirmed installation:

λ npm install angular-in-memory-web-api --save
npm WARN ajv-keywords@3.1.0 requires a peer of ajv@^6.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.1.3 (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.1.3: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

+ angular-in-memory-web-api@0.5.3
added 1 package in 25.102s

My package.json file is showing the dependency correctly:

    {
  "name": "hello-world",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build --prod",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^5.2.0",
    "@angular/common": "^5.2.0",
    "@angular/compiler": "^5.2.0",
    "@angular/core": "^5.2.0",
    "@angular/forms": "^5.2.0",
    "@angular/http": "^5.2.0",
    "@angular/platform-browser": "^5.2.0",
    "@angular/platform-browser-dynamic": "^5.2.0",
    "@angular/router": "^5.2.0",
    "angular-in-memory-web-api": "^0.5.3",
    "core-js": "^2.4.1",
    "rxjs": "^5.5.6",
    "zone.js": "^0.8.19"
  },
  "devDependencies": {
    "@angular/cli": "~1.7.0",
    "@angular/compiler-cli": "^5.2.0",
    "@angular/language-service": "^5.2.0",
    "@types/jasmine": "~2.8.3",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "^4.0.1",
    "jasmine-core": "~2.8.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~2.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~4.1.0",
    "tslint": "~5.9.1",
    "typescript": "~2.5.3"
  }
}

app.module.ts file:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms'; // <-- NgModel lives here
import { AppComponent } from './app.component';
import { HeroesComponent } from './heroes/heroes.component';
import { HeroDetailComponent } from './hero-detail/hero-detail.component';
import { HeroService } from './hero.service';
import { MessagesComponent } from './messages/messages.component';
import { MessageService } from './message.service';
import { AppRoutingModule } from './app-routing.module';
import { DashboardComponent } from './dashboard/dashboard.component';
import { HttpClientModule } from '@angular/common/http';
import { HttpClientInMemoryWebApiModule } from '@angular-in-memory-web-api';
import { InMemoryDataService } from './in-memory-data.service';


@NgModule({
  declarations: [
    AppComponent,
    HeroesComponent,
    HeroDetailComponent,
    MessagesComponent,
    DashboardComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    AppRoutingModule,
    HttpClientModule,

// The HttpClientInMemoryWebApiModule module intercepts HTTP requests
// and returns simulated server responses.
// Remove it when a real server is ready to receive requests.
HttpClientInMemoryWebApiModule.forRoot(
  InMemoryDataService, { dataEncapsulation: false }
)
  ],
  providers: [HeroService, MessageService],
  bootstrap: [AppComponent]
})
export class AppModule { }

I tried restarting VS code & re-running the cmd above but it didn't help.

Community
  • 1
  • 1
Ahmed Elkoussy
  • 8,162
  • 10
  • 60
  • 85

3 Answers3

3

The import is wrong. The @ is not required. Replace with this instead

import { HttpClientInMemoryWebApiModule } from 'angular-in-memory-web-api';
GSSwain
  • 5,787
  • 2
  • 19
  • 24
  • Thanks Girija , that solved the issue. it seems I mistakenly typed the @ as I was rewriting whatever written in the tutorial to get a feeling of how to use angular – Ahmed Elkoussy Feb 27 '18 at 11:54
2

If you are following Angular 5 tour tutorial and the errors are those

angular-in-memory-web-api@0.6.0 requires a peer of @angular/common@^6.0.0-rc.0 but none is installed. You must install peer dependencies yourself

angular-in-memory-web-api@0.6.0 requires a peer of @angular/core@^6.0.0-rc.0 but none is installed. You must install peer dependencies yourself

angular-in-memory-web-api@0.6.0 requires a peer of @angular/http@^6.0.0-rc.0 but none is installed. You must install peer dependencies yourself

angular-in-memory-web-api@0.6.0 requires a peer of rxjs@^6.0.0-beta.1

You should be modify de angular in memory web api version, try to do installing

npm install angular-in-memory-web-api@0.5.4 --save

Lrodriguez84
  • 714
  • 7
  • 8
0

I solved this problem by adding below code instead of HTTP CLIENT

import { InMemoryWebApiModule } from 'angular-in-memory-web-api/in-memory-web-api.module';

and in NGModule imports

import[ 
    InMemoryWebApiModule.forRoot(
      InMemoryDataService, { dataEncapsulation: false }
    )]

and it is working fine for me