Create-react-app: jQuery plugin says "jQuery is not defined"

Created on 9 Sep 2016  路  7Comments  路  Source: facebook/create-react-app

I have run the following command line arguments:
npm install --save jquery
npm install --save jquery-slimscroll

Inside of my index.js file, the first three lines import these modules:

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

But I receive the following error:

jquery.slimscroll.js:474 Uncaught ReferenceError: jQuery is not defined

Any suggestions?

Most helpful comment

require('jquery-slimscroll');

instead of

import 'jquery-slimscroll';

All 7 comments

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

Alright, now when I do:

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

I still get the same error message:

jquery.slimscroll.js:474 Uncaught ReferenceError: jQuery is not defined

require('jquery-slimscroll');

instead of

import 'jquery-slimscroll';

Wow that was it. I didn't realize there was a difference between import and require in this case. Thanks for your help.

Went through this debugging exercise before. Here was my observation of the babel transpired js:

  • babel respects the order of requires, along with other statements.
  • babel will put the imports at the top, before other statements ( in this case window.$ = window.jQuery = jquery;), which caused the issue.

There are probably some intricate reasons of babel's behavior, but I should stop being an ultracrepidarian ;-)

Seems like the problem is that jquery-slimscroll apparently doesn鈥檛 attempt to do require('jquery') even though it鈥檚 on npm. 馃槩

There are probably some intricate reasons of babel's behavior, but I should stop being an ultracrepidarian ;-)

Yes, per ES6 spec imports always execute first.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alleroux picture alleroux  路  3Comments

xgqfrms-GitHub picture xgqfrms-GitHub  路  3Comments

Aranir picture Aranir  路  3Comments

barcher picture barcher  路  3Comments

Evan-GK picture Evan-GK  路  3Comments