【问题标题】:Cannot find module '@material-ui/core'找不到模块'@material-ui/core'
【发布时间】:2020-05-30 07:36:33
【问题描述】:

我在将“@material-ui/core”导入我的 react/typescript/webpack 应用时遇到问题。总是在我跑的时候

webpack --config webpack.config.js

失败并出现错误

Cannot find module '@material-ui/core'

同样的错误也在 Visual Studio 代码中加了下划线。

我的 index.tsx 看起来像这样

import React from "react";
import ReactDOM from 'react-dom';
import { Button } from "@material-ui/core";

function App() {
    return <Button>Hi dudes</Button>
}
ReactDOM.render(<App />, document.getElementById("root"));

【问题讨论】:

    标签: reactjs typescript webpack


    【解决方案1】:

    yarn add @material-ui/corenpm i @material-ui/core 为我解决了这个问题。

    【讨论】:

    • 它不适合我。我在 node_modules 中有 @material-ui 并且仍然无法编译,即使我添加了 webpack 配置的直接路径
    • 您是否尝试过再次删除和安装节点模块?
    • 是的 :) 一切就绪,只是无法获得参考。我上面的解决方案有所帮助。
    • 那个问题是安装造成的还是我们每次都要手动修改才能修复错误?
    • 只要正确设置 webpack 配置就可以了:)
    【解决方案2】:

    如下设置tsconfig.json

    {
        "compilerOptions": {
            "outDir": "./dist",
            "target": "es5",
            "lib": ["es6", "dom"],
            "module": "ES6",
            "jsx": "react",
            "moduleResolution": "node",
            "noImplicitAny": true,
            "allowSyntheticDefaultImports": true,
            "allowJs": true
        },
        "exclude": [
            "dist"
        ]
    }
    

    allowJs 使其可导入和其他固定的 webpack 构建。

    【讨论】:

    • allowJs 设置为 true 对我不起作用。
    • 我刚刚修复了我在 tsconfig 文件中设置 "moduleResolution": "node" 的问题
    猜你喜欢
    • 1970-01-01
    • 2020-06-10
    • 2020-02-04
    • 2020-09-09
    • 2019-02-02
    • 2020-03-18
    • 2021-10-12
    • 2020-10-30
    • 2019-09-30
    相关资源
    最近更新 更多