typescript RxJs 和打字稿。TS2307:找不到模块“@reactivex/rxjs”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42750266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RxJs and Typescript. TS2307: Cannot find module '@reactivex/rxjs'
提问by Alejandro Nanez
I got a problem with typescript and RxJs v5.
我在打字稿和 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:
我做了yarn add @reactivex/rxjs
(也yarn add rxjs
)并且在我的 index.ts 上做了import Rx from '@reactivex/rxjs';
并得到了这个错误:
Also, if I run node ./node_modules/.bin/tsc
I got this error back too error TS2307: Cannot find module '@reactivex/rxjs'.
另外,如果我运行,node ./node_modules/.bin/tsc
我也会收到此错误error TS2307: Cannot find module '@reactivex/rxjs'.
UPDATE
更新
Also doing
也在做
import { Observable } from 'rxjs/Observable'
UPDATE 2
更新 2
Well this seems to be more related to TS itself than to RxJS.
好吧,这似乎与 TS 本身有关而不是与 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
拥有这个 ^ 配置似乎可以解决 VSCode 问题,但运行tsc
时src/index.ts
不起作用
node_modules/rxjs/Observable.d.ts(69,60): error TS2693: 'Promise' only refers to a type, but is being used as a value here.
回答by Abhishek Chadha
Try setting the "moduleResolution": "node"
inside your compileOptions
to specify module resolution strategy for TS.
尝试"moduleResolution": "node"
在您的内部设置compileOptions
为 TS 指定模块解析策略。
npm install rxjs --save
npm install rxjs --save
and in your Typescript
在你的打字稿中
import { Observable } from 'rxjs/Rx';
import { Observable } from 'rxjs/Rx';
That fixed it for me.
那为我修好了。
回答by Aman
For me I had the error below:
error TS2307: Cannot find module 'rxjs-compat/Observable'.
对我来说,我遇到了以下错误:
错误 TS2307:找不到模块“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';
回答by martin
For RxJS 5 you're supposed to use:
对于 RxJS 5,你应该使用:
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
有关更多信息,请参阅:https: //github.com/ReactiveX/rxjs/#installation-and-usage
回答by sboisse
I had the same problem in a brand new 'Hello World' project in Visual Studio 2017, for which I had executed npm install rxjs
.
我在 Visual Studio 2017 的一个全新的“Hello World”项目中遇到了同样的问题,为此我执行了npm install rxjs
.
Basically just had this in my server.ts
file:
基本上我的server.ts
文件中有这个:
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:
根据为编译选项module
和设置的值lib
,这产生了错误:
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.
这使得两个错误都消失了。
回答by cfphpflex
This is a hard nut to crack: my fix
in package.json dependencies confirm this code exists:
"rxjs": "^5.5.2",
In file /node_modules/@angular/core/src/application_ref.d.ts
//import { Observable } from 'rxjs/Observable';
// yeah, sure, I'm not supposed to, but I did and i worked
import { Observable } from 'rxjs/Rx';
in file node_modules/rxjs/src/tsconfig.json
{
"compilerOptions": {
"removeComments": false,
"preserveConstEnums": true,
"sourceMap": true,
"declaration": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"suppressImplicitAnyIndexErrors": true,
"moduleResolution": "node",
"target": "es6",
"outDir": "dist/es6",
"lib": [ "es2016.array.include" ]
},
"formatCodeOptions": {
"indentSize": 2,
"tabSize": 2
},
"bazelOptions": {
"suppressTsconfigOverrideWarnings": true
},
"files": [
"src/Rx.ts"
]
}
for this ng --version
Angular CLI: 1.6.7
Node: 8.11.1
OS: darwin x64
Angular: 5.2.10
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
@angular/cli: 1.6.7
@angular-devkit/build-optimizer: 0.0.42
@angular-devkit/core: 0.0.29
@angular-devkit/schematics: 0.2.0
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.9.7
@schematics/angular: 0.1.17
typescript: 2.4.2
webpack-dev-server: 2.11.2
webpack: 3.10.0
回答by smac89
Use this syntax:
使用以下语法:
import { Observable } from 'rxjs/Rx'
Or
或者
import Rx from 'rxjs/Rx';
Rx.Observable.of(1,2,3)
Or
或者
import { Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/map';
Observable.of(1,2,3).map(x => x + '!!!'); // etc
All these examples can be found on the website: http://reactivex.io/rxjs/manual/installation.html
所有这些例子都可以在网站上找到:http: //reactivex.io/rxjs/manual/installation.html