I'm trying to use a react component that includes a css file. I'm requiring the component as usual:
var Select = require('react-select');
I'd like to know how can I require the css needed for the component.
I've already tried this:
require('react-select/dist/react-select.css');
And this:
require('react-select.css');
And none have worked.
To be able to use CSS in your webpack app, you need to set up a new loader. Out-of-the-box, webpack only understands Javascript and JSON. With a loader, you can translate another type of file to a format that webpack understands and can work with. There are many webpack loaders and each loader has a specific purpose.
When importing a CSS file from node_modules , you have to make sure you have the specific package installed, e.g. by running npm install bootstrap . Note that you shouldn't use a relative path to import from node_modules . You should use an absolute path and omit the node_modules directory.
By default, Webpack will inline your CSS as Javascript tags that injects the CSS into the page. This sounds strange but it lets you do hot reloading in development. In production you extract them to a file using the ExtractTextPlugin, and require it with a normal link tag.
Make sure you have the following packages installed:
npm install style-loader css-loader --save-dev
and that your webpack configuration has this:
module: {
loaders: [
{ test: /\.css$/, loader: "style-loader!css-loader" }
]
}
This will require and bundle any css file that you require, and I would pull their exactly how you did it:
require('react-select/dist/react-select.css');
Another workaround that works for certain is that you copy that css file, and in the html you link it by:
<link rel="stylesheet" href="/path/to/react-select.css">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With