6

Am building a React App using create-react-app so the app is server ready.

When importing a bootstrap javascript plugin such as 'affix.js', I get the error 'Uncaught ReferenceError: jQuery is not defined', and the page no longer loads.

Am importing all the modules through the index.js file since with the create-react-app it dosent seem to load the 'src' and 'link' imports from the index.html file.

Am importing the bootstrap plugin like so: import 'bootstrap/js/affix.js'

Have tried importing jquery in the following ways, at the top of the index.js file but none is working:

  1. import jquery from 'jquery'
  2. import jquery from 'jquery', window.$ = window.jQuery = jquery;
  3. import jQuery from 'jquery'
  4. import $ from 'jquery'
  5. import 'jquery/dist/jquery.js';
  6. require('./node_modules/jquery/dist/jquery.js')

Any ideas?

5
  • 1
    i think, don't import jquery in this way. Just put the cdn jquery link in html file, it should work. use this: <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> Commented Jan 23, 2017 at 6:28
  • @MayankShukla thanks this worked! Finally. I tried lots of other ways before. I guess it is related to the way the index.html scans the folders directories when part of the create-react-app. So CDN would work in that case. Thanks! Could you post this as an official answer also? You just saved me rebuilding the app bottom up using expressJS, which I was going to do via tutorial since I have not built a nodeJs or expressJs app before :) you probably saved me like half a day. Thanks! Commented Jan 23, 2017 at 8:04
  • @MayankShukla also any ideas as to why my import methods above were not working? Commented Jan 23, 2017 at 8:12
  • Also btw the CDN you proided led to a dead URL for me. Used the ones provided at code.jquery.com instead. Commented Jan 23, 2017 at 8:22
  • No, you don't need CDN for this. Unfortunately you haven't provided the full code reproducing the issue so I can't say what exactly went wrong. Please see my answer. Commented Jan 23, 2017 at 21:25

4 Answers 4

9

Create React App maintainer here.

import $ from 'jquery';

and then using

$.something()

should work fine if you are using jQuery 2.x or higher.

If it doesn't please file an issue.


In your examples, the problem might be that you wrote import jquery from 'jquery' but used jQuery as variable name. You should have written import jQuery from 'jquery' (or any other name as long as you use it consistently). Variable names are case sensitive in JavaScript.

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

1 Comment

Thanks for the response. I already tried to " import $ from 'jquery' ", and it gives the same error. Also, regarding your previous comment, you can see the source code here where I currently am importing jquery via CDN (github.com/smaameri/keywords-finder). To test the bug you can remove remove the CDN link from index.html, and then try to import jquery as above in App.js. You will see the reference error. Will file issue as you said also.
5

In case someone land here. I had resolved similar issue for 'create-react-app' by using this:

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

and for importing other packages that needs jQuery you should import those packages after this, in affix.js case,

require 'bootstrap/js/affix.js';

2 Comments

Doesnt seem to work for my create-react-app. When I load up the app and in the console type window.jQuery I get undefined.
Using the CDN in my html file seems to be the only way I can get jQuery to load into my app.
0

You can copy jquery.js file in react "public" folder and import it in your index.html file

1 Comment

Isn't there a little more to it than that?
0

Solved it by editing index.html

Had a similar issue while trying to use jQuery inside the InnerHTML component. What solved the problem whas to import it`s source inside index.html:

<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>

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.