模块未找到- webpack ts-loader @material-ui

carvr3hs  于 2023-04-21  发布在  Webpack
关注(0)|答案(3)|浏览(138)

我在将@material-ui导入到我的React组件时遇到错误。未找到模块:错误:无法解析“./utils”。我可以导入其他库,如lodash,但使用@material-ui时出现问题。我尝试重新安装node_modules,但仍然发生错误。

import React, { useEffect } from "react";
import _ from "lodash";
import Button from "@material-ui/core";

export default function Dashboard() {

  useEffect(() => {
    console.log("Test");
    const testVar = "test";
    console.log(_.split(testVar));
    return () => {
      // cleanup;
    };
  }, []);
  return (
    <div>
      <Button>Login</Button>
    </div>
  );

package.json

{
  "name": "views",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "@material-ui/core": "^4.10.2",
    "@material-ui/icons": "^4.9.1",
    "lodash": "^4.17.15",
    "react": "^16.13.1",
    "react-dom": "^16.13.1"
  },
  "devDependencies": {
    "@types/lodash": "^4.14.157",
    "@types/react": "^16.9.38",
    "@types/react-dom": "^16.9.8",
    "source-map-loader": "^1.0.0",
    "ts-loader": "^7.0.5",
    "typescript": "^3.9.5",
    "webpack": "^4.43.0",
    "webpack-cli": "^3.3.12"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "webpack --mode=development --watch --progress",
    "dev:nowatch": "webpack --mode=development"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

webpack.config.js

var path = require("path");

module.exports = {
  mode: "development",

  // Enable sourcemaps for debugging webpack's output.
  devtool: "source-map",
  entry: {
    dashboard: "./Dashboard/index",
    // creator: "./Creator/index",
  },

  output: {
    path: path.resolve(__dirname, ""),
    filename: "./[name]/dist/index.js",
  },
  resolve: {
    // Add '.ts' and '.tsx' as resolvable extensions.
    extensions: [".ts", ".tsx"],
    modules: [path.resolve(__dirname, "./"), "node_modules"],
  },

  module: {
    rules: [
      {
        test: /\.ts(x?)$/,
        exclude: /node_modules/,
        use: [
          {
            loader: "ts-loader",
          },
        ],
      },
      // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
      {
        enforce: "pre",
        test: /\.js$/,
        loader: "source-map-loader",
      },
    ],
  },

  // When importing a module whose path matches one of the following, just
  // assume a corresponding global variable exists and use that instead.
  // This is important because it allows us to avoid bundling all of our
  // dependencies, which allows browsers to cache those libraries between builds.
  externals: {
    react: "React",
    "react-dom": "ReactDOM",
  },
};

tsconfig.json

{
  "compilerOptions": {
    "lib": ["es6", "dom"],
    "noImplicitAny": true,
    "noImplicitThis": true,
    "strictNullChecks": true,
    "outDir": "./dist/",
    "sourceMap": true,
    "target": "es5",
    "jsx": "react",
    "allowJs": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true
  }
}

示例错误

ERROR in ./node_modules/@material-ui/core/esm/index.js
    Module not found: Error: Can't resolve './withMobileDialog' in 'C:\Users\user\Documents\Code\calendarapp\UI\Client\views\node_modules\@material-ui\core\esm'
     @ ./node_modules/@material-ui/core/esm/index.js 240:0-65 240:0-65 241:0-35 241:0-35
     @ ./Dashboard/Dashboard.tsx
     @ ./Dashboard/App.tsx
     @ ./Dashboard/index.tsx
    
    ERROR in ./node_modules/@material-ui/core/esm/index.js
    Module not found: Error: Can't resolve './withWidth' in 'C:\Users\user\Documents\Code\calendarapp\UI\Client\views\node_modules\@material-ui\core\esm'
     @ ./node_modules/@material-ui/core/esm/index.js 242:0-51 242:0-51 243:0-28 243:0-28
     @ ./Dashboard/Dashboard.tsx
     @ ./Dashboard/App.tsx
     @ ./Dashboard/index.tsx
gmol1639

gmol16391#

找到解决方案了。
在webpack.config.js中添加“.js”、“.jsx”进行解析。

resolve: {
    // Add '.ts' and '.tsx' as resolvable extensions.
    extensions: [".ts", ".tsx", ".js", ".jsx"],
    
  },
xxb16uws

xxb16uws2#

如果我可以添加一些东西,如果我没记错的话,import Button from "@material-ui/core";应该在“Button”周围有大括号,就像这样import {Button} from "@material-ui/core";

wljmcqd8

wljmcqd83#

首先创建你的react应用程序然后安装材料ui核心和图标模块然后去package.json并检查依赖关系,如果发现材料ui核心和图标,然后第一次复制图标行然后粘贴在你使用的文件,如index.js然后你只添加/图标名称例如:-从@material-ui/图标导入添加你只添加@material-ui/图标/添加

相关问题