1

i am trying to load a css file with webpack but i keep getting this error message as shown in the picuture.

pics showing error i have done a lot of searching so please don't, all questions don't seem to fit my case.

below given are the various file am working with

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;


 }

am currently using webpack v4.6.0 and npm v5.0.0

2 Answers 2

2

In your webpack.config.js,

please remove the "" from the regex expression.

// Whatever else you got here
rules: [
    {
      test: /\.css$/,
      use: ["style-loader", "css-loader"]
    }
]
Sign up to request clarification or add additional context in comments.

2 Comments

thanks alot @Ru, so why didn't it work with "/\.css$/" ??
Hi @lilfancy Regex does not work with "". I believe "" will cause the regex expression to become a string instead of a regex.
1

Your webpack config file should not have the file extension .json. Please change the extension to .js

Also, you need to tell webpack, that you want to use a config file and give it the path to this file. In your package.json, please modify the following prop:

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

1 Comment

Thanks for the reply, I would do the corrections and get back to you @Daniel

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.