在webpack中加载css文件时出错

时间:2018-04-20 23:29:47

标签: css webpack webpack-style-loader css-loader

我正在尝试使用webpack加载css文件,但我不断收到此错误消息,如图所示。

pics showing error 我已经做了很多搜索,所以请不要,所有问题似乎都不适合我的情况。

下面给出的

是使用

的各种文件

的package.json

{
  "name": "loading_files",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "compile": "webpack",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "css-loader": "^0.28.11",
    "lodash": "^4.17.5",
    "style-loader": "^0.21.0",
    "webpack": "^4.6.0",
    "webpack-cli": "^2.0.14"
   }
}

webpack.config.js

"use strict"

let path = require("path");

module.exports = {
  mode: "development",
  entry: "./src/index.js",

  output: {
    filename: 'bundler.js',
    path: path.resolve(__dirname, 'dist'),
  },
 module:{
   rules: [
            {
              test: "/\.css$/",
              use: ["style-loader", "css-loader"]
            }
         ]
       }
 }

index.js

import _ from 'lodash';
import css from './index.css';

function component() {
  var element = document.createElement('div');
  element.innerHTML = _.join(['Hello', 'webpack'], ' ');
  element.classList.add('hello');
  return element;
}

document.body.appendChild(component());

index.css

.hello{
  color: red;


 }

目前正在使用webpack v4.6.0npm v5.0.0

2 个答案:

答案 0 :(得分:1)

您的webpack配置文件不应包含文件扩展名.json。 请将扩展名更改为.js

此外,您需要告诉webpack,您要使用配置文件并为其指定此文件的路径。在你的package.json中,请修改以下prop:

{
   scripts: {
      "compile": "webpack --config webpack.config.js",
   }
}

答案 1 :(得分:1)

webpack.config.js

请从""表达式中删除regex

// Whatever else you got here
rules: [
    {
      test: /\.css$/,
      use: ["style-loader", "css-loader"]
    }
]