0

Getting this error when using with create-react-app

ReferenceError: jQuery is not defined

I have this in my index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
//import '../node_modules/jquery/dist/jquery.min.js';

import jQuery from 'jquery';
import '../node_modules/product-xxx/dist/js/product-xxx.min.js'; // this file uses jQuery

I already tried still no use

/* eslint-disable import/first */
window.jQuery = window.$ = $;

This works if I load jQuery from CDN in the public/index.html jQuery version is 3x

Any pointers?

Update

jQuery is used in the module product-xxx like this

+function(t) {
    "use strict";
    function e() {
        var t = document.createElement("bootstrap")
          , e = {
            WebkitTransition: "webkitTransitionEnd",
            MozTransition: "transitionend",
            OTransition: "oTransitionEnd otransitionend",
            transition: "transitionend"
        };
        for (var i in e)
            if (void 0 !== t.style[i])
                return {
                    end: e[i]
                };
        return !1
    }
    t.fn.emulateTransitionEnd = function(e) {
        var i = !1
          , o = this;
        t(this).one("bsTransitionEnd", function() {
            i = !0
        });
        var n = function() {
            i || t(o).trigger(t.support.transition.end)
        };
        return setTimeout(n, e),
        this
    }
    ,
    t(function() {
        t.support.transition = e(),
        t.support.transition && (t.event.special.bsTransitionEnd = {
            bindType: t.support.transition.end,
            delegateType: t.support.transition.end,
            handle: function(e) {
                return t(e.target).is(this) ? e.handleObj.handler.apply(this, arguments) : void 0
            }
        })
    })
}(jQuery)

3 Answers 3

2

To install jquery with npm

npm install jquery --save

After you do that, on every page you must have this where you are using jquery

import $ from 'jquery';

Sign up to request clarification or add additional context in comments.

Comments

0

Have you installed jquery in your package.json? May be you can try this How to use jQuery dependant plugins in create-react-app or this issue #679

2 Comments

Its already there in package.json under dependencies "jquery": "^3.3.1",
@mrale81 don't put questions in answers, if you have questions, ask them in the comments.
-1

you need to use import jQuery from 'jquery'; in every file that you are using it

3 Comments

My understanding is src/index.js does that. I have also included it every file but not working.
you need to add the jquery import inside the product-xxx js file
anyway it will be better if you use a cdn version, and then use in your code window.jQuery

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.