0

I am trying to load a css file in my component .js file but while doing so I get this error :

ERROR in ./stories/styles.css Module parse failed: Unexpected token (1:0) You may need an appropriate loader to handle this file type.

I have added following rule in my webpack.config.js file to take care of loaders but am still getting the error.

rules: [
      {
        test: /\.s?css$/,
        use: ['style-loader', 'raw-loader', 'sass-loader', 'css-loader'],
        include: [
          path.resolve(__dirname, '../css/'),
        ],
      },

My styles.css file looks like this:

.row {
    display: flex;
    flex-direction: row;
    width: 100%;
}

.row.dark {
    background: #303030;
}

.col {
    width: calc(100% - 400px);
    padding: 15px;
}

.row > .col:first-child {
    border-right: 1px solid #ccc;
    max-width: 400px;
}

Do I need to add any other loader ? I even tried using import instead of require to get the styles.css file but it made no difference.

1 Answer 1

1

Webpack loaders get processed from right to left, you need to change the config to:

rules: [
      {
        test: /\.scss$/,
        use: ['style-loader', 'raw-loader', 'css-loader', 'sass-loader'],
        include: [
          path.resolve(__dirname, '../css/'),
        ],
      },
  • I am also unsure what the use of the raw-loader is here since it is used to loads files as a string - you might not need it
Sign up to request clarification or add additional context in comments.

4 Comments

I did try your suggested change but it didn't work. I am still getting the same error.
I would also change the test to /\.scss$/, you would need to rename your files to have the .scss extension but any css should be valid sass. Also have you tried removing raw-loader?
So the problem was in the path that I was giving for css files. So after re-arranging the loaders like you suggested and changing the path it worked. Thanks :)
Ok great, glad you got it sorted...;)

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.