218

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?

Ajit Medhekar
  • 1,018
  • 1
  • 10
  • 39
uksz
  • 18,239
  • 30
  • 94
  • 161
  • 7
    Checkout the latest docs if you're using rxjs6 on the correct import and usage `import { of } from 'rxjs'; return of(res);` https://github.com/ReactiveX/rxjs/blob/master/MIGRATION.md#import-paths & https://github.com/ReactiveX/rxjs/blob/master/MIGRATION.md#howto-convert-to-pipe-syntax – fidev May 17 '18 at 09:58

18 Answers18

239

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

import 'rxjs/add/observable/of';
uksz
  • 18,239
  • 30
  • 94
  • 161
  • 21
    After my 192nd double-take, I noticed that in my code I was importing from **`operator/`** - `rxjs/add/operator/of` - instead of `observable/`. D'oh. – EricRobertBrewer Dec 06 '16 at 00:51
  • I found I don't need to use this statement in one of angular projects. But in the other one, I have to import it. I don't understand the differences. Do you know the reasons? – niaomingjian Sep 14 '17 at 01:43
  • 1
    Angular version, perhaps?! I didn't have to do this for 4.3.2 but I did for 5.0.0. – Draghon Dec 22 '17 at 17:27
  • @Draghon: Exactly the same with me. I didn't have to do it for 4.4, I do now for 5.2. Even more weird, I only have to include it in one file and all other .ts-files simply pick it up and are good to go. – J.P. Jan 26 '18 at 13:08
  • 2
    What if I get Cannot find module "rxjs/add/observable/of" ? – Enrico Aug 14 '18 at 09:11
216

If anybody is having this problem while using Angular >= 6 and rxjs version 6 or greater, see the answers here: 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);
Kingston Fortune
  • 905
  • 8
  • 17
jgosar
  • 2,469
  • 1
  • 16
  • 14
  • 3
    Thank you! Figuring out imports in Rx is always such a huge source of frustration for me because of the api volatility. – DomenicDatti Nov 06 '18 at 13:46
  • Finally. Spent an hour going through different permutations until this answer finally solved this issue. Why did they make this so hard? – Eternal21 Jul 20 '21 at 03:27
44

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.

Mark Langer
  • 1,064
  • 1
  • 9
  • 14
30

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';
esengineer
  • 9,514
  • 7
  • 45
  • 69
22

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)
Shaun Grady
  • 221
  • 2
  • 3
  • This isn't a work around, this is the syntax for Angular >= 6.0.0. import { of } from 'rxjs' was fine for me. See https://stackoverflow.com/questions/38067580/property-of-does-not-exist-on-type-typeof-observable – mark_h Mar 21 '19 at 07:37
18

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

  • 1
    I found I don't need to use this statement in one of angular projects. But in the other one, I have to import it. I don't understand the differences. Do you know the reasons? – niaomingjian Sep 14 '17 at 01:44
16

You could also import all operators this way:

import {Observable} from 'rxjs/Rx';
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
  • 7
    I would not recommend importing this way, as this is a quite big library and "of" is a very small part of it. – kodeaben Mar 09 '17 at 11:53
  • 2
    @methgaard `import { Observable } from 'rxjs/Observable' import 'rxjs/add/observable/of';` doesn't work. Only `import {Observable} from 'rxjs/Rx';` works. The version is 5.4.2 – niaomingjian Sep 13 '17 at 08:08
  • @methgaard I'm sorry. I made a mistake. The fact is I got `Observable_1.Observable.of(...).delay(...).timeout is not a function`. I didn't use `import 'rxjs/add/operator/timeout'` – niaomingjian Sep 13 '17 at 09:44
  • It also increases the bundle size – Amirhossein Mehrvarzi Sep 23 '18 at 16:05
5

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

Peter Wippermann
  • 4,125
  • 5
  • 35
  • 48
karunakar bhogyari
  • 622
  • 1
  • 8
  • 16
4

This should work properly just try it.

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
Levon Petrosyan
  • 8,815
  • 8
  • 54
  • 65
alok singh
  • 41
  • 3
4
// "rxjs": "^5.5.10"
import { of } from 'rxjs/observable/of';

.... 
return of(res)
letanthang
  • 390
  • 5
  • 7
  • This is a code-only answer. Would you please explain what you're trying to suggest? – Peter Wippermann Jul 19 '18 at 12:10
  • 1
    Hi there, we should only import the operator that we need, not the whole "Observable" because of performance issue. In the the new version (^5.5.10) the right way to import "of" operator is: import { of } from 'rxjs/observable/of' ... It works for my case. I will edit my resolution. Thanks Peter. – letanthang Jul 23 '18 at 03:29
4

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);
    });
KVarmark
  • 226
  • 3
  • 4
  • 1
    Using 6.0, attempting to subscribe to Observable.create(of(val)) resulted in "this._subscribe is not a function". Instead, I successfully created an observable by just calling "of(val)". – Jim Norman Jun 02 '18 at 20:51
3

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);
zgue
  • 3,793
  • 9
  • 34
  • 39
3

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';
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
Tomas
  • 3,269
  • 3
  • 29
  • 48
2

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.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Eli
  • 21
  • 4
  • The question applies to Angular 2. [It doesn't perform well with RxJS UMD module](https://github.com/angular/angular/issues/9359). – Estus Flask Aug 25 '16 at 14:58
2

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.

Stabak
  • 36
  • 4
1
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.

Esaith
  • 706
  • 9
  • 12
1

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

0

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

Yevheniy Potupa
  • 537
  • 5
  • 12