Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJs and Typescript. TS2307: Cannot find module '@reactivex/rxjs'

I got a problem with typescript and RxJs v5.

Please Look the UPDATE sections.

I did yarn add @reactivex/rxjs (also yarn add rxjs) and on my index.ts did import Rx from '@reactivex/rxjs'; and got this error: VSCode Error

Also, if I run node ./node_modules/.bin/tsc I got this error back too error TS2307: Cannot find module '@reactivex/rxjs'.

UPDATE

Also doing

import { Observable } from 'rxjs/Observable'

throws the same error enter image description here.

UPDATE 2

Well this seems to be more related to TS itself than to RxJS.

"compilerOptions": {
    "module": "commonjs",
    "allowJs": true,
    "outDir": "dist",
    "target": "es2015",
    "noImplicitAny": true,
    "noEmit": true,
    "strictNullChecks": true,
    "suppressExcessPropertyErrors": false,
    "suppressImplicitAnyIndexErrors": false,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "lib": [
        "es2015",
        "dom"
    ]
}

Having this ^ configuration seems to fix the VSCode issues, but running tsc against src/index.ts doesn't work

node_modules/rxjs/Observable.d.ts(69,60): error TS2693: 'Promise' only refers to a type, but is being used as a value here.
like image 763
Alejandro Nanez Avatar asked Mar 12 '17 16:03

Alejandro Nanez


4 Answers

Try setting the "moduleResolution": "node" inside your compileOptions to specify module resolution strategy for TS.

npm install rxjs --save

and in your Typescript

import { Observable } from 'rxjs/Rx';

That fixed it for me.

like image 80
Abhishek Chadha Avatar answered Nov 15 '22 01:11

Abhishek Chadha


For me I had the error below:
error TS2307: Cannot find module 'rxjs-compat/Observable'.

Solution: instead of importing:

import { Observable } from 'rxjs/Observable';

use:

import { Observable } from 'rxjs';

and you can put to the observable you want to import by separating all by comma like:

import { Observable, of } from 'rxjs';
like image 34
Aman Avatar answered Nov 15 '22 02:11

Aman


I had the same problem in a brand new 'Hello World' project in Visual Studio 2017, for which I had executed npm install rxjs.

Basically just had this in my server.ts file:

import { Observable } from 'rxjs/Observable'
import * as http from 'http';

var port = process.env.port || 1337
http.createServer(function (req, res) {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Hello World\n');
}).listen(port);

Depending on the values set for compile options module and lib, this generated the error:

server.ts(1,28): error TS2307: Build:Cannot find module 'rxjs/Observable'.

or:

node_modules\rxjs\Observable.d.ts(73,59): error TS2693: Build:'Promise' only refers to a type, but is being used as a value here.

The winning combination of compile options that worked for me were:

"compilerOptions": {
    "module": "commonjs",
    "lib": [ "es2015" ]
  }

This made both errors to go away.

like image 29
sboisse Avatar answered Nov 15 '22 02:11

sboisse


For RxJS 5 you're supposed to use:

import * as Rx from 'rxjs';

Or import only a specific part of the library:

import { Observable } from 'rxjs/Observable';

For more info see: https://github.com/ReactiveX/rxjs/#installation-and-usage

like image 2
martin Avatar answered Nov 15 '22 02:11

martin