7

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.

0

2 Answers 2

13

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">
Sign up to request clarification or add additional context in comments.

6 Comments

I have that config on my webpack configuration file
This is the correct answer, however for newbies like myself - Make sure you then have the style-loader and css-loader plugins installed so webpack can read them.
@Birksy89 For even newer newbies run this npm install style-loader css-loader --save-dev and also if you decide to split the CSS into it's own file as per the documentation be sure to remove the explicit css marker in the require as is mentioned here
@nmu I am an even newer newbie, and you have saved me from a very, very long day.
@nmu so you are installing loaders for css and styles just on development and not using CSS in production?
|
-1
//package.json
"dependencies": {
    "my-package": "2.0.0"
}
//app.js
//Get a relative path to the installed css files:
require('../someRelativePathFromAppJs/node_modules/my-package/somePath/myNeeded.css')

Comments

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.