Use Owl Carousel 2 with next.js and react

Viewed 6699

I want to use jQuery owl carousel with my next js react app. I dont want to use npm package react-owl-carousel only owl-carousel and jquery plugin.

I use lazy load with next.js dynamic and put the following code in my Webpack config:

import dynamic from 'next/dynamic';
const Slider = dynamic(() => import('...'), {
  ssr: false,
});

Webpack config:

config.plugins.push(new webpack.ProvidePlugin({
  $: 'jquery',
  jQuery: 'jquery',
  'window.jQuery': 'jquery',
}));

Slider component:

import 'owl.carousel';
import 'owl.carousel/dist/assets/owl.carousel.css';

When I use $('element').owlCarousel(...), I got the following error:

TypeError: this.owl.owlCarousel is not a function

3 Answers

In next.config.js:

const webpack = require('webpack');
module.exports = {
webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
config.plugins.push(new webpack.ProvidePlugin({
        $: 'jquery',
        jQuery: 'jquery',
        'window.jQuery': 'jquery'
    }))
return config;
}}

In your component:

import "owl.carousel/dist/assets/owl.carousel.css";
import "owl.carousel/dist/assets/owl.theme.default.css";
const OwlCarousel = dynamic(import("react-owl-carousel"), {ssr: false});
  1. Inject global window via jQuery and webpack inside next.config.js file at the root of your next.js project

         module.exports = {
            webpack: (config, {
                buildId,
                dev,
                isServer,
                defaultLoaders,
                webpack
            }) => {
                // Note: we provide webpack above so you should not `require` it
                // Perform customizations to webpack config
                config.plugins.push(
                    new webpack.ProvidePlugin({
                        $: "jquery",
                        jQuery: "jquery",
                        "window.jQuery": "jquery"
                    })
                );
                // Important: return the modified config
                return config;
            }
    
  2. Slider component which uses react-owl-carousel component

        import OwlCarousel from "react-owl-carousel";
    
        export default function MySlider({ sliders }) {
          return (
            <section>
                <OwlCarousel
                  loop={true}
                  items={1}
                  responsiveRefreshRate={0}
                  autoplay={true}
                  autoplayTimeout={7000}
                  autoplayHoverPause={true}
                  nav={true}
                  navText={[
                    "<i class='icon-arrow-prev'></i>",
                    "<i class='icon-arrow-next'></i>"
                  ]}
                  dots={false}
                  margin={20}
                >
                  <div class="item"></div>
                  <div class="item"></div>
                </OwlCarousel>
            </section>
          );
        }
    
  3. Import the component and for use onto your parent component. Kindly note you MUST use the ssr:false option. Next.js documentation recommends it

    const MySlider = dynamic(
      () => import("./Myslider"),
      // No need for SSR, when the module includes a library that only works in the
      // browser.
      { ssr: false }
    );
    

Happy codding.

after checking the bundle file i find that webpack pass another jQuery instance to owl.carousel file

here is webpack bundle code

__webpack_require__(/*! jquery */ "../../node_modules/owl.carousel/node_modules/jquery/dist/jquery.js")

as you can see jQuery pass to plugin from node_modules/owl.carousel/node_modules/jquery/dist/jquery.js instead of node_modules/jquery/dist/jquery.js

i fixed issue by deleting node_modules/owl.carousel/node_modules

after that webpack pass node_modules/jquery/dist/jquery.js as jquery instance

Related