Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-slick: Import CSS from slick-carousel fails

React beginner here. My app uses create-react-app, and I'm pulling in react-slick for a carousel. I'm trying to follow the directions that are provided in the setup for react-slick, but the following doesn't work for me:

@import "~slick-carousel/slick/slick.css"; @import "~slick-carousel/slick/slick-theme.css";

I get the following error:

./src/components/Homepage.scss Module not found: Can't resolve '~slick-carousel/slick/slick.css' in '/Users/simon/Dev/frischideas/site-new/src/components'

It works if I add the css to index.html from the CDN, but I'd rather avoid that network call, as I believe it's affecting the rendering of the page on initial load.

I tried following the instructions for configuring create-react-app to use relative paths, but that wasn't working for me either. Maybe I'm completely misunderstanding, but I thought the ~ notation would allow me to import scss from a package in /node_modules.

Any suggestions/clarification would be greatly appreciated.

Update: adding the setup from my webpack.config file (most of which is the default from create-react-app).

{
            test: /\.scss$/,
            use: [
              require.resolve('classnames-loader'),
              require.resolve('style-loader'), {
                loader: require.resolve('css-loader'),
                options: {
                  importLoaders: 1,
                  modules: 1,
                  localIdentName: "[name]__[local]___[hash:base64:5]"  
                },
              },{
                loader: require.resolve('postcss-loader'),
                options: {
                  // Necessary for external CSS imports to work
                  // https://github.com/facebookincubator/create-react-app/issues/2677
                  ident: 'postcss',
                  plugins: () => [
                    require('postcss-flexbugs-fixes'),
                    autoprefixer({
                      browsers: [
                        '>1%',
                        'last 6 versions',
                        'Firefox ESR',
                        'not ie < 9', // React doesn't support IE8 anyway,'
                      ],
                      remove: false,
                      flexbox: 'no-2009',
                    }),
                  ],
                },
              },{
                loader: require.resolve('sass-loader'),
                options: {
                  outputStyle: 'expanded'
                }
              }
            ],
          }
like image 876
flysi3000 Avatar asked Feb 14 '18 03:02

flysi3000


People also ask

How do I import slick carousel?

I install jquery via npm, so that the slick-carousel could run and I install it with npm install jquery --save , so that (from what I've gathered) it's included in dependencies in my package. json file. Then I install slick carousel in the same way, using npm install slick-carousel --save.

Does react slick use jquery?

Adding React Slick to Project React Slick does not provide own CSS styling. Instead, it uses the theme of the original Slick jQuery plugin.


2 Answers

you change to be this :

import "slick-carousel/slick/slick.css";

import "slick-carousel/slick/slick-theme.css";

delete ~

like image 138
putra irawan Avatar answered Sep 28 '22 09:09

putra irawan


you might have missed this:

npm install slick-carousel
like image 32
Andrian Tam Avatar answered Sep 28 '22 10:09

Andrian Tam