Create-react-app: import styles from './myfile.css' returns empty styles object

Created on 5 Jan 2017  路  4Comments  路  Source: facebook/create-react-app

I must be making some basic mistake, but I can't get style imports to work.

The contents of myfile.css are

.someclass {
  background-color: blue;
}

then I have a .jsx file with

import styles from './myfile.css'
console.log(styles)
console.log(styles.someclass)
...
<div className={styles.someclass} />

when I open this, it logs

Object {}
undefined

and if I inspect the <div>, it doesn't have a class attribute.

Most helpful comment

@bw2 seems like you didn't enable an option { modules: true } for css-loader in webpack config

take a look

All 4 comments

it's not css-module

What you are trying is css-modules and as of now create-react-app doesn't support it.

The alternate is doing old school

/* myfile.css */
.someclass {
  background-color: blue;
}
// myComponents.js
import './myfile.css'

...


md5-1e862d5e9cc0b2343dacb4e71a594a92


That's working. Thanks!

@bw2 seems like you didn't enable an option { modules: true } for css-loader in webpack config

take a look

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fson picture fson  路  3Comments

JimmyLv picture JimmyLv  路  3Comments

rdamian3 picture rdamian3  路  3Comments

alleroux picture alleroux  路  3Comments

DaveLindberg picture DaveLindberg  路  3Comments