I got a problem with typescript and RxJs v5.
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:
Also, if I run node ./node_modules/.bin/tsc
I got this error back too error TS2307: Cannot find module '@reactivex/rxjs'.
Also doing
import { Observable } from 'rxjs/Observable'
throws the same error .
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.
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.
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';
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With