How to Fix Cannot find module ‘rxjs-compat/Observable’

Cannot find module ‘rxjs-compat/Observable’ error occurs when trying to “use an older version of RxJS code with a newer version of the library but without the rxjs-compat compatibility layer.”

RxJS (Reactive Extensions for JavaScript) underwent significant changes when transitioning from version 5 to version 6.

The code and import paths changed, but to help projects transition more smoothly, the rxjs-compat package was introduced. This package provides backward compatibility for projects using RxJS 5 code with RxJS 6.

How to fix the error

Here are the solutions that you can use to fix the error!

  1. Install the Compatibility Layer
  2. Updating Your Code
  3. Using Code Mod

Solution 1: Install the Compatibility Layer

If you want to continue using older RxJS code without making changes, you can install the compatibility layer:

npm install rxjs-compat

Import the Observable like this:

import { Observable } from 'rxjs'

Solution 2: Update Your Code

If you prefer not to add the compatibility layer, update your RxJS code to use the new version 6+ paths and methods. Import like this:

import { Observable } from 'rxjs';

import { map } from 'rxjs/operators';

Solution 3: Use Code Mod

RxJS provides a tool called rxjs-tslint that can automatically update your code to use the new version 6+ paths and methods. To use this:

First, install the required tools:

npm install -g rxjs-tslint 

npm install tslint -g

Then, run the automatic update:

rxjs-5-to-6-migrate -p [path/to/tsconfig.json]

Choose the option that best fits your project’s needs. If you’re starting a new project or already upgrading, it’s generally best to update your code to the latest version rather than rely on the compatibility layer.

That’s it!