skip to Main Content

I have created a sample project repo https://github.com/nikhilgoswami/webpack-5-react-with-jquery-plugin.git

I get error jQuery is not defined when importing the plugin flexslider

I tried expose-loader and provide-plugin but to no use

2

Answers


  1. Add the following configuration to your webpack.config.js file to ensure that jQuery is accessible globally and usable by your application:

       const webpack = require('webpack');
       module.exports = {
             // ...
        plugins: [
         new webpack.ProvidePlugin({
         $: 'jquery',
         jQuery: 'jquery',
          'window.jQuery': 'jquery',
          }),
         ],
         // ...
       };
    

    In your React component, import jQuery and Flexslider:

     import $ from 'jquery';
     import 'flexslider';
    
     class MyComponentClass extends React.Component {
       componentDidMount() {
       // Initialize Flexslider
       $(this.slider).flexslider();
      }
      render() {
        return (
           <div ref={(slider) => { this.slider = slider; }}>
            // Slides go here
          </div>
         );
    }
    }
     export default MyComponentClass;
    

    Make sure to include the required CSS and JavaScript files for Flexslider when rendering your React component on the server:

    import MyComponent from './MyComponentClass';
    import fs from 'fs';
    import path from 'path';
    
    // Read the Flexslider's JavaScript and CSS files.
    const flexsliderCss = fs.readFileSync(path.resolve(__dirname, 
    const flexsliderJs = fs.readFileSync(path.resolve(__dirname, 
    '../node_modules/flexslider/jquery.flexslider.js'), 'utf8');
    
    // Render the React component and include the CSS and JavaScript files for 
    Flexslider
    const html = ReactDOMServer.renderToString(
    <MyComponent />,
    );
    
    const finalHtml = `
      <!DOCTYPE html>
      <html>
       <head>
         <title>My Application</title>
         <style>${flexsliderCss}</style>
      </head>
     <body>
        <div id="app">${html}</div>
         <script src="/bundle.js"></script>
         <script>${flexsliderJs}</script>
       </body>
     </html>
     `;
    
    res.send(finalHtml);
    
    Login or Signup to reply.
  2. First of you can install plugin using below command

    npm install jquery flexslider --save
    

    Then after create new file jquery-global.js in your src folder and put below code

    import $ from 'jquery';
    window.jQuery = $;
    window.$ = $;
    

    Need to Webpack configuration file webpack.config.js add below code and configure as per your need

    const webpack = require('webpack');
    const path = require('path');
    
    module.exports = {
      plugins: [
        new webpack.ProvidePlugin({
          $: 'jquery',
          jQuery: 'jquery',
        }),
      ],  
    };
    

    Add below code in your server.js file

    import jquery from 'jquery';
    global.$ = global.jQuery = jquery;
    

    Here is your updated code can you try this

    import React, { Component } from 'react';
    import $ from 'jquery';
    import 'flexslider';
    
    class FlexSlider extends Component {
      componentDidMount() {
        $(this.slider).flexslider({
          animation: "slide",
          slideshowSpeed: 3000,
          controlNav: false,
          directionNav: true,
          prevText: "",
          nextText: ""
        });
      }
    
      render() {
        return (
          <div className="flexslider" ref={slider => (this.slider = slider)}>
            <ul className="slides">
              <li><img src="http://example.com/slide1.jpg" alt="Slide 1" /></li>
              <li><img src="http://example.com/slide2.jpg" alt="Slide 2" /></li>
              <li><img src="http://example.com/slide3.jpg" alt="Slide 3" /></li>
            </ul>
          </div>
        );
      }
    }
    
    export default FlexSlider;
    

    I Hope it should be work

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search