找不到模块:错误:无法解析“ ./@fortawesome/fontawesome-free/css/fontawesome.css”

时间:2018-06-26 19:33:07

标签: css webpack font-awesome

我正在尝试在Webpack 4中使用真棒字体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%;
}

您可以在图片上显示:

enter image description here

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

5 个答案:

答案 0 :(得分:2)

有点晚了,但也许对某人有帮助:

我认为不是

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

应该是

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

答案 1 :(得分:0)

此导入路径似乎混乱了。

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文件夹中寻找强大的功能。

您可以明确尝试从node_modules导入它吗?

例如...

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

此外,我认为它不在node_modules中的原因是因为webpack需要具有一个SASS loader配置项,而您只有一个,但是我很好奇路径中的点表示法是否会抛出webpack。您也可以尝试以下方法吗?

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

答案 2 :(得分:0)

您可能只是忘了在加载程序中包含css个文件: test: /\.(sass|scss|css)$/,(因为您尝试导入fontawesome。 css ,而不是 scss

答案 3 :(得分:0)

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

答案 4 :(得分:0)

我遇到了同样的问题

webpack css-loader 不支持(scss 和 .css 捆绑)你需要 sass 来做

只需安装 sass-loader 和 node-sass(阅读笔记)

npm install -D sass-loader node-sass

网络包

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"],
    //   },
}