Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module not found: Error: Can't resolve 'rxjs/add/observable/throw'

Tags:

angular

rxjs

I have the following imports in my "posts.service.ts"

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { NotFoundError } from '../commons/errors/not-found-error.errors';
import { AppErrors } from '../commons/errors/app-errors.errors';
import { Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';
import 'rxjs/add/observable/throw';

I even checked the node_modules folder for 'rxjs/add/observable/throw' and it is available there. But I get a strange error as shown below during project compilation phase.

ERROR in ./src/app/services/posts.service.ts Module not found: Error: Can't resolve 'rxjs/add/observable/throw' in '/Users/gauthampughaz/Development/angular/practice/src/app/services'

ℹ 「wdm」: Failed to compile.

like image 429
Gautham Pughazhendhi Avatar asked Jun 04 '18 11:06

Gautham Pughazhendhi


2 Answers

Since RxJS 6 you should import "creation" methods directly from 'rxjs':

import { throwError } from 'rxjs';

Just don't forget you need to have proper path maps set but if you're using angular-cli you don't need to worry about that. For more details see: https://github.com/ReactiveX/rxjs/blob/6.2.0/doc/pipeable-operators.md#build-and-treeshaking

like image 174
martin Avatar answered Sep 21 '22 02:09

martin


_throw is now exported as throwError.

You can do something like this if you dont want to replace every instance of _throw. (don't recommend).

import { throwError as _throw } from 'rxjs'; 

Or you can just change everywhere you use _throw to throwError

See other breaking changes here:

https://github.com/ReactiveX/rxjs/blob/master/CHANGELOG.md#breaking-changes-7

like image 33
DonDaniel Avatar answered Sep 23 '22 02:09

DonDaniel