184

code

error info

Typescript code:

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import { Hero } from './hero';
import { HEROES } from './mock-heroes';

@Injectable({
  providedIn: 'root'
})
export class HeroService {

  constructor() { }

  getHeroes(): Observable<Hero[]> {
    return of(HEROES);
  }

}

error info:

error TS2307: Cannot find module 'rxjs-compat/Observable'. node_modules/rxjs/observable/of.d.ts(1,15): error TS2307: Cannot find module 'rxjs-compat/observable/of'. src/app/hero.service.ts(2,10): error TS2305: Module '"F:/angular-tour-of-heroes/node_modules/rxjs/Observable"' has no exported member 'Observable'. src/app/hero.service.ts(15,12): error TS2304: Cannot find name 'of'.

package.json file with Angular version:

version

veben
  • 19,637
  • 14
  • 60
  • 80
Thomas Lee
  • 2,757
  • 2
  • 11
  • 13
  • 3
    The framework you're using is named Angular. AngularJS is a different framework. – JB Nizet Apr 15 '18 at 08:51
  • 9
    It seems you're using RxJS 6. The imports need to be changed when using that version (see the release notes). If you're not using Angular 6, then you should stick with RxJS 5. – JB Nizet Apr 15 '18 at 08:54
  • thx.I'm using Angular6.0 ~ – Thomas Lee Apr 15 '18 at 09:04
  • 5
    Here's the relevant documentation then: https://next.angular.io/guide/rx-library. Note that the imports are not the ones you're using. – JB Nizet Apr 15 '18 at 09:07

16 Answers16

225

This might be helpful in Angular 6 for more info refer this Document

  1. rxjs: Creation methods, types, schedulers and utilities
import { Observable, Subject, asapScheduler, pipe, of, from, interval, merge, fromEvent } from 'rxjs';
  1. rxjs/operators: All pipeable operators:
import { map, filter, scan } from 'rxjs/operators';
  1. rxjs/webSocket: The web socket subject implementation
import { webSocket } from 'rxjs/webSocket';
  1. rxjs/ajax: The Rx ajax implementation
import { ajax } from 'rxjs/ajax';
  1. rxjs/testing: The testing utilities
import { TestScheduler } from 'rxjs/testing';
Sanjeet kumar
  • 3,333
  • 3
  • 17
  • 26
135

Apparently (as you point in the error log), after updating to Angular 6.0.0 rxjs-compat is missing.

Run npm install rxjs-compat --save to install. Should fix it.

pau
  • 1,493
  • 1
  • 10
  • 9
123

Just put:

import { Observable} from 'rxjs';

Just like that. Nothing more or less.

craigcaulfield
  • 3,381
  • 10
  • 32
  • 40
Marius Mielcarek
  • 1,239
  • 1
  • 6
  • 2
64

I replaced the original code with import { Observable, of } from 'rxjs', and the problem is solved.

enter image description here

enter image description here

Thomas Lee
  • 2,757
  • 2
  • 11
  • 13
36

You are using RxJS 6. Just replace

import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';

by

import { Observable, of } from 'rxjs';
veben
  • 19,637
  • 14
  • 60
  • 80
36

Try this:

npm install rxjs-compat --save
Zoe
  • 27,060
  • 21
  • 118
  • 148
Yogesh Kumar
  • 1,016
  • 11
  • 10
13

What helped me is:

  1. Get rid of all old import paths and replace them with new ones like this:

    import { Observable , BehaviorSubject } from 'rxjs';)

  2. Delete node_modules folder

  3. npm cache verify
  4. npm install
Vega
  • 27,856
  • 27
  • 95
  • 103
Jackie
  • 327
  • 2
  • 13
7

I had a similar issue. Back-revving RXJS from 6.x to the latest 5.x release fixed it for Angular 5.2.x.

Open package.json.

Change "rxjs": "^6.0.0", to "rxjs": "^5.5.10",

run npm update

Matthew Smith
  • 1,287
  • 1
  • 9
  • 19
4

Just remove /Observable from 'rxjs/Observable';

If you then get Cannot find module 'rxjs-compat/Observable' just put below line to thr terminal and press enter.

npm install --save rxjs-compat
Blasanka
  • 21,001
  • 12
  • 102
  • 104
2

My resolution was adding the following import: import { of } from 'rxjs/observable/of';

So the overall code of hero.service.ts after the change is:

import { Injectable } from '@angular/core';
import { Hero } from './hero';
import { HEROES } from './mock-heroes';
import { of } from 'rxjs/observable/of';
import {Observable} from 'rxjs/Observable';

@Injectable()
export class HeroService {

  constructor() { }

  getHeroes(): Observable<Hero[]> {
    return of(HEROES);
  }
}
2

The angular-split component is not supported in Angular 6, so to make it compatible with Angular 6 install following dependency in your application

To get this working until it's updated use:

"dependencies": {
"angular-split": "1.0.0-rc.3",
"rxjs": "^6.2.2",
    "rxjs-compat": "^6.2.2",
}
dustinroepsch
  • 1,122
  • 1
  • 7
  • 24
Manoj Gupta
  • 456
  • 4
  • 9
1

You have not installed rxjs library please install

npm install rxjs-compat --save

Vinayak Shedgeri
  • 2,222
  • 1
  • 21
  • 24
0

In my case this error was happening because I had an old version of ng cli in my computer.

The problem was solved after running:

ng update

ng update @angular/cli

sebbab
  • 847
  • 8
  • 13
0

Update angular-in-memory-web-api version. The default angular-in-memory-web-api version installed during the tutorial angular-tour-of-heroes was 0.4. It worked like a charm in my case. (Using Angular 7 with RxJS 6)

npm i angular-in-memory-web-api@0.8.0
rtrigo
  • 519
  • 1
  • 6
  • 16
0

Just Run

npm i rxjs-compat

this works for me.

hash
  • 5,336
  • 7
  • 36
  • 59
-3

use return Observable.of(HEROES);

Neuron
  • 5,141
  • 5
  • 38
  • 59
  • 1
    While this answer is probably correct and useful, it is preferred if you include some explanation along with it to explain how it helps to solve the problem. This becomes especially useful in the future, if there is a change (possibly unrelated) that causes it to stop working and users need to understand how it once worked. – Erty Seidohl May 25 '18 at 16:09
  • If you read the question carefully, you'll see mentions of Angular **6** and rxjs **6**, not 5. This problem is specific for rxjs v6+, while the solution you posted is related to rxjs (and Angular) v5. – Zlatko Jun 26 '18 at 09:17