Angular - "has no exported member 'Observable'"

210,788

Solution 1

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

enter image description here

enter image description here

Solution 2

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';

Solution 3

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.

Solution 4

Just put:

import { Observable} from 'rxjs';

Just like that. Nothing more or less.

Solution 5

You are using RxJS 6. Just replace

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

by

import { Observable, of } from 'rxjs';
Share:
210,788

Related videos on Youtube

Thomas Lee
Author by

Thomas Lee

Updated on July 08, 2022

Comments

  • Thomas Lee
    Thomas Lee almost 2 years

    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

    • JB Nizet
      JB Nizet about 6 years
      The framework you're using is named Angular. AngularJS is a different framework.
    • JB Nizet
      JB Nizet about 6 years
      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.
    • Thomas Lee
      Thomas Lee about 6 years
      thx.I'm using Angular6.0 ~
    • JB Nizet
      JB Nizet about 6 years
      Here's the relevant documentation then: next.angular.io/guide/rx-library. Note that the imports are not the ones you're using.
  • David Dahan
    David Dahan almost 6 years
    I think this is the best thing to do as rxjs 6 will be supported from Angular 6 only.
  • Erty Seidohl
    Erty Seidohl almost 6 years
    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.
  • cjb110
    cjb110 almost 6 years
    how can you 'work this out?' from first hand? searching SO works, but if the library was less well used.
  • Prasanna
    Prasanna almost 6 years
    i'm also facing same issue, but what if i don't want to use backward compatibility package?
  • Zlatko
    Zlatko almost 6 years
    Well that was the error he began with, the export is not there.
  • Zlatko
    Zlatko almost 6 years
    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.
  • Levent Divilioglu
    Levent Divilioglu almost 6 years
    Using 'rxjs' instead of 'rxjs/Observable' just solved my problem. I think, owner of the question should accept your answer.
  • Enrico
    Enrico over 5 years
    What if we don't want to use rxjs-compat?
  • William Terrill
    William Terrill over 5 years
    This should be the correct answer. It's not the right answer moving forward... but it's definitely the "right now" answer.
  • Sandy
    Sandy over 5 years
    This will definitely work. But does it also support Tree Shaking?
  • EvAlex
    EvAlex over 5 years
    npm install rxjs-compat --save
  • PhpCoder
    PhpCoder about 4 years
    actually this solved the problem in angular 8 ionic 5
  • Jaseem Abbas
    Jaseem Abbas almost 4 years
    Confirmed! Worked in Angular 9 with Ionic 5.