webpack 模块未找到:错误:无法解析“./css/fontawesome-free/fontawesome.css”

inkz8wg9  于 12个月前  发布在  Webpack
关注(0)|答案(6)|浏览(155)

我尝试在webpack 4中使用font-awesome 5.1.0。webpack配置文件的内容:

const HtmlWebPackPlugin = require("html-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const path = require("path");
const autoprefixer = require('autoprefixer');

module.exports = {
  entry: ["./src/index.js", "./src/index.scss", "react-hot-loader/patch"],
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "bundle.[hash].js"
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
          options: {
            presets: ["@babel/preset-env", "@babel/preset-react"]
          }
        }
      },
      {
        test: /\.html$/,
        use: [
          {
            loader: "html-loader",
            options: {
              minimize: true
            }
          }
        ]
      },
      {
        test: /\.(sass|scss)$/,
        use: [
          {
            loader: "file-loader",
            options: {
              name: "bundle.css"
            }
          },
          { loader: "extract-loader" },
          {
            loader: "css-loader"
          },
          {
            loader: "postcss-loader",
            options: {
              plugins: () => [autoprefixer({ grid: false })]
            }
          },
          {
            loader: "sass-loader",
            options: {
              includePaths: ["./node_modules"]
            }
          }
        ]
      },
      {
        test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
        use: [
          {
            loader: "file-loader",
            options: {
              name: "[name][hash].[ext]",
              outputPath: "fonts/"
            }
          }
        ]
      }
    ]
  },
  plugins: [
    new HtmlWebPackPlugin({
      template: "./public/index.html",
      filename: "./index.html"
    }),
    new CopyWebpackPlugin([
      {
        from: "public"
      }
    ])
  ]
};

字符串
在index.scss文件中:

@import "@fortawesome/fontawesome-free/css/fontawesome.css";
@import "@material/button/mdc-button.scss";
@import "@material/drawer/mdc-drawer.scss";
@import "@material/top-app-bar/mdc-top-app-bar.scss";
@import "@material/list/mdc-list.scss";
@import "./App/scss/app.scss";

* {
  padding: 0;
  margin: 0;
}

html, body {
  height: 100%;
  width: 100%;
}

#root {
  display: flex;
  flex-direction: row;
  box-sizing: border-box;
  height: 100%;
  width: 100%;
}


正如你可以在图片上:
x1c 0d1x的数据
Font-awesome是从正确的地方导入的,但我遇到了编译器错误:

ERROR in ./src/index.scss
Module not found: Error: Can't resolve './@fortawesome/fontawesome-free/css/fontawesome.css' in '/home/developer/Desktop/reasonreact/cockpit/src'
 @ ./src/index.scss
 @ multi (webpack)-dev-server/client?http://localhost:9000 webpack/hot/dev-server ./src/index.js ./src/index.scss react-hot-loader/patch


我做错了什么?

更新

我把webconfig改成:

{
    test: /\.(sass|scss)$/,
    use: [
      {
        loader: "file-loader",
        options: {
          name: "bundle.css"
        }
      },
      { loader: "extract-loader" },
      {
        loader: "css-loader"
      },
      {
        loader: "postcss-loader",
        options: {
          plugins: () => [autoprefixer({ grid: false })]
        }
      },
      {
        loader: "sass-loader",
        options: {
          includePaths: ["./node_modules", "src", "."]
        }
      }
    ]
  },


我得到了以下错误:

ERROR in ./src/index.scss
Module build failed (from ./node_modules/extract-loader/lib/extractLoader.js):
NonErrorEmittedError: (Emitted value instead of an instance of Error) ReferenceError: require is not defined
    at runLoaders (/home/developer/Desktop/reason-react/cockpit/node_modules/webpack/lib/NormalModule.js:300:13)
    at /home/developer/Desktop/reason-react/cockpit/node_modules/loader-runner/lib/LoaderRunner.js:364:11
    at /home/developer/Desktop/reason-react/cockpit/node_modules/loader-runner/lib/LoaderRunner.js:230:18
    at context.callback (/home/developer/Desktop/reason-react/cockpit/node_modules/loader-runner/lib/LoaderRunner.js:111:13)
    at process._tickCallback (internal/process/next_tick.js:68:7)
 @ multi (webpack)-dev-server/client?http://localhost:9000 webpack/hot/dev-server ./src/index.js ./src/index.scss react-hot-loader/patch

ego6inou

ego6inou1#

有点晚了,但也许能帮上忙:
我觉得

@import "@fortawesome/fontawesome-free/css/fontawesome.css";

字符串
应当

@import '~@fortawesome/fontawesome-free/css/fontawesome.css';

htrmnn0y

htrmnn0y2#

安装它:

npm i @fortawesome/fontawesome-free

字符串

7gs2gvoe

7gs2gvoe3#

在终端中运行“npm audit fix”修复了我的问题

1tuwyuhd

1tuwyuhd4#

这条导入路径看起来很混乱。

ERROR in ./src/index.scss
Module not found: Error: Can't resolve './@fortawesome/fontawesome-free/css/fontawesome.css' in '/home/developer/Desktop/reasonreact/cockpit/src'
 @ ./src/index.scss

字符串
它不会在node_modules文件夹中查找fortawesome。
你能显式地尝试从node_modules导入它吗?
例如...

@import "../path/to/node_modules/@fortawesome/fontawesome-free/css/fontawesome.css";


另外,我认为它没有在node_modules中查找的原因是因为webpack需要有一个SASS加载器配置项,你有一个,但我很好奇你的路径中的点符号是否会以某种方式抛出webpack。你能试试下面吗?

/**
   * Config sassLoader paths for imports from node_modules
   */
  sassLoader: {
    includePaths: [
      'node_modules', 'src', '.'
    ]
  },

5m1hhzi4

5m1hhzi45#

您可能只是忘记在loader:test: /\.(sass|scss|css)$/,中包含css文件(因为您尝试导入fontawesome.css,而不是scss

kpbwa7wx

kpbwa7wx6#

我也有同样的问题
webpack css-loader不支持(scss和.css捆绑)你需要sass来做
只需安装sass-loader和node-sass(阅读说明)

npm install -D sass-loader node-sass

字符串
webpack

module.exports = {
  module: {
    rules: [
        {
        test: /\.scss$/,
        use: [
          'vue-style-loader',
          'css-loader',
          'sass-loader'<--------------------this is you need----------------
        ]
      }
    ]
  },
  // plugin omitted
}


注意事项:人们在安装npm install -d sass-loader时会犯错误,但是sass-loader带来了最新版本的sass,这意味着你得到了sass-loader:11.0.0或更高版本,所以webpack 4不支持最新的sass-loader
解决方案
你的webpack版本是4,所以你只需要低于sass loader版本10

npm install -D node-sass
npm install -D sass-loader@^10


我也附上我的webpack给你(供参考)我用的是vue框架

const HtmlWebpackPlugin = require('html-webpack-plugin')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const webpack = require('webpack')

module.exports = {
    entry: './src/main.js',
    module: {
        rules: [{
                test: /\.js$/,
                use: 'babel-loader'
            },
            {
                test: /\.vue$/,
                use: 'vue-loader'
            },
            {
                test: /\.(sass|scss|css)$/,
                use: ['vue-style-loader', 'css-loader','sass-loader'],
              
            },
           
        ]
    },
    devServer: {
        open: true,
        hot: true
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/index.html',
        }),
        new VueLoaderPlugin(),
        new webpack.HotModuleReplacementPlugin()
    ],
    // resolve: {
    //     alias: {
    //       vue$: "vue/dist/vue.runtime.esm.js",
    //     },
    //     extensions: ["*", ".js", ".vue", ".json"],
    //   },
}

相关问题