Create-react-app: trouble importing jquery

Created on 5 Sep 2017  Â·  5Comments  Â·  Source: facebook/create-react-app

Having difficulty when importing jquery in create-react-app project. when adding in import $ from 'jquery' with a npm -v of 3.10.10, will receive the following when logging $ : // The jQuery object is actually just the init constructor 'enhanced' // Need init if jQuery is called (just allow error to be thrown if not included) return new jQuery….
current repo: https://github.com/faurenlarr/personalsite

Most helpful comment

it would not be working as usual as in including it with a script tag, you need to import $ every time you want to use jquery in a file.

so adding import $ from 'jquery' to the top of this file
https://github.com/faurenlarr/personalsite/blob/master/src/main.js file should make the commented code work. (if you uncomment it of course :D)

All 5 comments

I think this is the expected output. It just prints the jQuery function which is the default import of jquery package. https://github.com/jquery/jquery/blob/c18d608537d8945de6c5855a9475657177fa74ac/src/core.js#L31

What do you expect to print instead?

Gotcha. Wasn't expecting anything in particular, although I am not able to use jquery in my app currently. Wanted to show the log just in case that was not expected. But since it is expected, I am unsure why it would not be working as usual.

it would not be working as usual as in including it with a script tag, you need to import $ every time you want to use jquery in a file.

so adding import $ from 'jquery' to the top of this file
https://github.com/faurenlarr/personalsite/blob/master/src/main.js file should make the commented code work. (if you uncomment it of course :D)

I’ll close this but if @viankakrisna’s answer doesn’t help, let us know, and we can look at it again!
To rephrase it: you need to import $ from 'jquery' in every file where you try to use $.

That helped, thanks!

Was this page helpful?
0 / 5 - 0 ratings