typescript 无法解决在打字稿/webpack 中找不到模块“rxjs”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44377534/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:36:36  来源:igfitidea点击:

can't resolve Cannot find module 'rxjs' within typescript/webpack

javascripttypescriptwebpackrxjses6-modules

提问by ducin

I've got a problem to load rxjsinto a simple webpack setup (without angular). I'm running:

我有一个问题要加载rxjs到一个简单的 webpack 设置中(没有角度)。我在跑:

./node_modules/.bin/webpack --config webpack.config.js --watch

to start webpack. The only file with the app, src/app.ts, starts with:

启动 webpack。应用程序的唯一文件src/app.ts, 以:

import { Observable } from 'rxjs';

and this line is highlighted in VSCode and in webpack console with this error:

并且此行在 VSCode 和 webpack 控制台中突出显示,并显示以下错误:

Cannot find module 'rxjs'.

But the overall output works fine. Just this console error.

但整体输出工作正常。只是这个控制台错误。

tsconfig.json:

tsconfig.json

{
    "compilerOptions": {
         "target": "es2015"
    },
    "files": [
        "src/app.ts"
    ]
}

webpack.config.js:

webpack.config.js

module.exports = {
    entry: "./src/app.ts",
    output: {
        filename: "dist/bundle.js"
    },
    resolve: {
        // Add '.ts' and '.tsx' as a resolvable extension.
        extensions: [".webpack.js", ".web.js", ".ts", ".tsx", ".js"]
    },
    module: {
        loaders: [
            // all files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'
            { test: /\.ts?$/, loader: "ts-loader" }
        ]
    }
}

package.json(everything installed properly):

package.json(一切安装正确):

{
  "scripts": {
    "start": "./node_modules/.bin/webpack --config webpack.config.js --watch"
  },
  "devDependencies": {
    "ts-loader": "^2.1.0",
    "typescript": "^2.3.4",
    "webpack": "^2.6.1",
    "webpack-dev-server": "^2.4.5"
  },
  "dependencies": {
    "bootstrap": "^3.3.7",
    "rxjs": "^5.4.0"
  }
}

I can't find the reason for why can't ts/webpack resolve where is rxjs

我找不到 ts/webpack 无法解析在哪里的原因 rxjs

回答by Oles Savluk

You should change default module resolution strategy from Classic to Node. In order to do this, change your tsconfig.jsonfile:

您应该将默认模块解析策略从 Classic 更改为 Node。为此,请更改您的tsconfig.json文件:

{
    ...
    "compilerOptions": {
        ....
        "moduleResolution": "node"
    }
}

See documentation on Module Resolution

请参阅有关模块分辨率的文档

回答by Infoideas 2021

to ionic

离子

simple use this command in order to delete the plugin

简单地使用这个命令来删除插件

npm uninstall --save rxjs

npm uninstall --save rxjs-compat

and then run this to reinstall

然后运行它重新安装

npm install --save rxjs

npm install --save rxjs-compat

回答by enapupe

You must add node_modulesto your webpack resolve.modules config:

您必须添加node_modules到您的 webpack resolve.modules 配置:

 resolve: {
    modules: [
      "node_modules",
      path.resolve(__dirname, "app")
    ],
    extensions: [".js", ".json", ".jsx", ".css"],
 }

more info on https://webpack.js.org/configuration/resolve/

有关https://webpack.js.org/configuration/resolve/ 的更多信息