Observable.of is not a function

AngularRxjs

Angular Problem Overview


I am having issue with importing Observable.of function in my project. My Intellij sees everything. In my code I have:

import {Observable} from 'rxjs/Observable';

and in my code I use it like that:

return Observable.of(res);

Any ideas?

Angular Solutions


Solution 1 - Angular

Actually I have imports messed up. In latest version of RxJS we can import it like that:

import 'rxjs/add/observable/of';

Solution 2 - Angular

If anybody is having this problem while using Angular >= 6 and rxjs version 6 or greater, see the answers here: https://stackoverflow.com/questions/50220854/could-not-use-observable-of-in-rxjs-6-and-angular-6

In short, you need to import it like this:

import { of } from 'rxjs';

And then instead of calling

Observable.of(res);

just use

of(res);

Solution 3 - Angular

Although it sounds absolutely strange, with me it mattered to capitalize the 'O' in the import path of import {Observable} from 'rxjs/Observable. The error message with observable_1.Observable.of is not a function stays present if I import the Observable from rxjs/observable. Strange but I hope it helps others.

Solution 4 - Angular

My silly mistake was that I forgot to add /add when requiring the observable.

Was:

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

Which visually looks OK becasue rxjs/observable/of file, in fact, exists.

Should be:

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

Solution 5 - Angular

Patching wasn't working for me, for whatever reason, so I had to resort to this method:

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

// ...

return of(res)

Solution 6 - Angular

Just to add,

if you're using many of them then you can import all using

import 'rxjs/Rx'; 

as mentioned by @Thierry Templier. But I think If you are using limited operator then you should import individual operator like

import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/mergeMap';
import 'rxjs/add/observable/of';

as mentioned by @uksz.

Because 'rxjs/Rx' will import all the Rx components which definitely cost performance.

Comparison

Solution 7 - Angular

You could also import all operators this way:

import {Observable} from 'rxjs/Rx';

Solution 8 - Angular

I am using Angular 5.2 and RxJS 5.5.6

This code did not work:

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

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

Below code worked:

    import { Observable } from 'rxjs/Observable';
    import { Subscriber } from 'rxjs/Subscriber';
    
     getHeroes(): Observable<Hero[]> 
     {
          return Observable.create((observer: Subscriber<any>) => {
              observer.next(HEROES);
              observer.complete();
          });

      }
    

Calling method:

this.heroService.getHeroes()
      .subscribe(heroes => this.heroes = heroes);

I think they might moved/changed of() functionality in RxJS 5.5.2

Solution 9 - Angular

This should work properly just try it.

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

Solution 10 - Angular

// "rxjs": "^5.5.10"
import { of } from 'rxjs/observable/of';

.... 
return of(res)

Solution 11 - Angular

Upgraded from Angular 5 / Rxjs 5 to Angular 6 / Rxjs 6?

You must change your imports and your instantiation. Check out Damien's blog post

Tl;dr:

import { Observable, fromEvent, of } from 'rxjs';

const yourResult = Observable
    .create(of(yourObservable))
    .startWith(null)
    .map(x => x.someStringProperty.toLowerCase());

//subscribe to keyup event on input element
Observable
    .create(fromEvent(yourInputElement, 'keyup'))
    .debounceTime(5000)
    .distinctUntilChanged()
    .subscribe((event) => {
        yourEventHandler(event);
    });

Solution 12 - Angular

RxJS 6

When upgrading to version 6 of the RxJS library and not using the rxjs-compat package the following code

import 'rxjs/add/observable/of';   
  // ...
  return Observable.of(res);

has to be changed into

import { of } from 'rxjs';
  // ...
  return of(res);

Solution 13 - Angular

For me (Angular 5 & RxJS 5) the autocomplete import suggested:

import { Observable } from '../../../../../node_modules/rxjs/Observable';

while to should be (with all static operators from, of, e.c.t working fine:

import { Observable } from 'rxjs/Observable';

Solution 14 - Angular

I had this problem today. I'm using systemjs to load the dependencies.

I was loading the Rxjs like this:

...
    paths: {
        "rxjs/*": "node_modules/rxjs/bundles/Rx.umd.min.js"
    },
...

Instead of use paths use this:

var map = {
...
'rxjs':                       'node_modules/rxjs',
...
}

var packages = {
...
'rxjs':	{ main: 'bundles/Rx.umd.min.js', defaultExtension: 'js' }
...
}

This little change in the way systemjs loads the library fixed my problem.

Solution 15 - Angular

For Angular 5+ :

import { Observable } from 'rxjs/Observable';should work. The observer package should match the import as well import { Observer } from 'rxjs/Observer'; if you're using observers that is

import {<something>} from 'rxjs'; does a huge import so it's better to avoid it.

Solution 16 - Angular

import 'rxjs/add/observable/of';

shows a requirement of rxjs-compat

require("rxjs-compat/add/observable/of");

I did not have this installed. Installed by

npm install rxjs-compat --save-dev

and rerunning fixed my issue.

Solution 17 - Angular

In rxjs v6, of operator should be imported as import { of } from 'rxjs';

Solution 18 - Angular

Somehow even Webstorm made it like this import {of} from 'rxjs/observable/of'; and everything started to work

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionukszView Question on Stackoverflow
Solution 1 - AngularukszView Answer on Stackoverflow
Solution 2 - AngularjgosarView Answer on Stackoverflow
Solution 3 - AngularMark LangerView Answer on Stackoverflow
Solution 4 - AngularEyeView Answer on Stackoverflow
Solution 5 - AngularShaun GradyView Answer on Stackoverflow
Solution 6 - Angulardharmesh kaklotarView Answer on Stackoverflow
Solution 7 - AngularThierry TemplierView Answer on Stackoverflow
Solution 8 - Angularkarunakar bhogyariView Answer on Stackoverflow
Solution 9 - Angularalok singhView Answer on Stackoverflow
Solution 10 - AngularletanthangView Answer on Stackoverflow
Solution 11 - AngularKVarmarkView Answer on Stackoverflow
Solution 12 - AngularzgueView Answer on Stackoverflow
Solution 13 - AngularTomasView Answer on Stackoverflow
Solution 14 - AngularEliView Answer on Stackoverflow
Solution 15 - AngularSS-View Answer on Stackoverflow
Solution 16 - AngularEsaithView Answer on Stackoverflow
Solution 17 - AngularLasantha BasnayakeView Answer on Stackoverflow
Solution 18 - AngularYevheniy PotupaView Answer on Stackoverflow