5

I have a fullstack project, which is made with Django-REST and React as the frontend.

Everytime i try to load a css file into my react application i get an error

import './Dashboard.css'

export default class Dashboard extends Component {

render() {
...

Dashboard.css

body {
margin:0;
}

Picture of the error here is my webpack.config.js

module.exports = {
module: {
       rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader"
                }
            }
        ]
    }
}

what is weird to me is that, inline css, works just fine, it's only when i try to load an external sheet, that i get issues. Does anybody know what may be the cause of the problem?

3
  • And when you leave the .css file empty, the error is still there? Commented Apr 12, 2019 at 12:56
  • 1
    As mentioned in the error, you likely need to use a CSS loader such as this one. Commented Apr 12, 2019 at 12:56
  • @josemartindev no only when there is CSS rules applied Commented Apr 12, 2019 at 12:56

1 Answer 1

5

You need a css-loader. Update webpack.config.js to this:

module.exports = {
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader'],
      },
    ]
  }
}

And install the loader:

npm install --save-dev css-loader

It should build properly now. You can read more in the Webpack docs

Sign up to request clarification or add additional context in comments.

5 Comments

should i add this to the existing rules?
or should i just add an existing module?
@baileyhaldwin I already added it for you. You can just copy paste my answer and replace what you have in your webpack.config.js
absolutely, I had to wait 4 minutes to accept the answer.
@baileyhaldwin woah, didn't realize I was that quick :D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.