How to Fix TypeError: Observable.of is not a function

TypeError: Observable.of is not a function error typically occurs when you’re trying to “use the of() method from the Observable class in the RxJS library, but the method is not identified.” 

The main reason for the error is “changes in the latest RxJS library versions.” Starting with RxJS version 6, the way you create and use observables, as well as the way you import operators, has changed.

In versions prior to RxJS 6, you can use:

import { Observable } from 'rxjs/Observable';

Observable.of(1, 2, 3);

But in RxJS version 6 and later, you must use:

import { of } from 'rxjs';

of(1, 2, 3);

If you are using RxJS 6 or later, you should update your imports and usage patterns accordingly. If you have a lot of existing code and don’t want to refactor everything immediately, you can also use rxjs-compat, a compatibility layer that allows older RxJS code to work with RxJS 6.

Conclusion

To fix the error:

  1. Ensure you have the correct version of RxJS installed.
  2. Update your code to use the new import and usage patterns.
  3. If needed, consider using rxjs-compat as a temporary solution.

That’s it!

Related posts

Cannot find module ‘rxjs-compat/Observable’