在TypeScript模块解析期间找不到Material-UI组件

时间:2019-10-02 22:30:19

标签: reactjs typescript material-ui visual-studio-2019

我有一个Visual Studio 2019(v16.3.2)React TypeScript项目,其中包括Material-UI组件。 TypeScript编译器无法解析任何@ material-ui导入,并且发生以下错误,导致相应的JavaScript无法生成。

Error   TS2307  (TS) Cannot find module '@material-ui/core/Grid'.

上述错误的模块是

import * as React from 'react';

// Material-UI
import Grid from '@material-ui/core/Grid';

// Components
import SkiWeekends from './SkiWeekends';

const EZForm: React.FC = () => {
  return (
    <React.Fragment>
      <Grid container spacing={3}>
        <Grid item xs={12}>
          <SkiWeekends/>
        </Grid>
        <Grid item xs={12}>
          A
        </Grid>
        <Grid item xs={12}>
          B
        </Grid>
        <Grid item xs={12}>
          A
        </Grid>
      </Grid>
    </React.Fragment>
  );
}

export default EZForm;

tsconfig.json是

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "es6"
    ],
    "noImplicitAny": true,
    "noImplicitThis": true,
    "strictNullChecks": true,
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react"
  },
  "include": [
    "src"
  ]
}

package.json是

{
  "name": "msc",
  "version": "3.0.0",
  "private": true,
  "dependencies": {
    "@types/jest": "24.0.18",
    "@types/node": "12.7.9",
    "@types/react": "16.9.4",
    "@types/react-dom": "16.8.2",
    "@types/react-redux": "7.1.4",
    "@types/react-router-dom": "5.1.0",
    "@types/redux-thunk": "2.1.32",
    "@material-ui/core": "4.5.0",
    "@material-ui/icons": "4.4.3",
    "@material-ui/styles": "4.5.0",
    "react": "16.10.1",
    "react-dom": "16.10.1",
    "react-redux": "7.1.1",
    "react-router-dom": "5.1.2",
    "react-scripts": "3.1.2",
    "redux": "4.0.4",
    "redux-devtools-extension": "2.13.8",
    "redux-thunk": "2.3.0",
    "typescript": "3.6.3"
  },
  "scripts": {
    "start": "rimraf ./build && react-scripts start",
    "build": "react-scripts build",
    "test": "cross-env CI=true react-scripts test --env=jsdom",
    "eject": "react-scripts eject",
    "lint": "eslint ./src/"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

我已验证@ material-ui软件包已安装在node_modules文件夹中。

enter image description here

所有其他软件包/导入正常工作。对于为何在TS编译期间未解决material-ui模块的想法表示赞赏。

5 个答案:

答案 0 :(得分:5)

我遇到了同样的问题,我的解决方法是将"moduleResolution": "node"添加到tsconfig.json。

答案 1 :(得分:2)

对我来说,此问题已解决,只需删除所有导入声明并再次添加即可。

答案 2 :(得分:1)

对我来说,通过运行 npm install / yarn install 解决了问题

答案 3 :(得分:0)

经过研究,发现tsconfig include部分需要从默认值进行修改才能在子目录中查找文件。

默认包含仅在src文件夹中查找文件

"include": [
  "src"
]

修改后的文件可以在任何子目录中找到

"include": [
  "src/**/*"
]

关于tsconfig.json的很好的参考

https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

答案 4 :(得分:0)

对我来说,问题是通过运行 yarn add @material-ui/data-grid 解决的 我在你的 package.json 中没有看到 @material-ui/data-grid 包,所以我认为它丢失了