37373

Uncaught ReferenceError: jQuery is not defined, even though jQuery is first imported script

Question:

This question has been asked before <a href="https://stackoverflow.com/questions/8886614/uncaught-referenceerror-jquery-is-not-defined" rel="nofollow">Uncaught ReferenceError: jQuery is not defined</a> and <a href="https://stackoverflow.com/questions/2075337/uncaught-referenceerror-is-not-defined" rel="nofollow">Uncaught ReferenceError: $ is not defined?</a> and <a href="https://stackoverflow.com/questions/4623982/test-if-jquery-is-loaded-not-using-the-document-ready-event" rel="nofollow">Test if jquery is loaded not using the document ready event</a>

But, I find the error on browser as

main.js:88 Uncaught ReferenceError: jQuery is not defined at main.js:88

My main.js looks like

(function($) { 'use strict'; // all functions here })(jQuery);

I make imports in my index.html as

<!-- build:js scripts/main.min.js --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <!--<script src="scripts/vendors/jquery3.2.0.min.js"></script>--> <script src="scripts/vendors/waypoints.min.js"></script> <script src="scripts/main.js"></script> <!-- endbuild -->

So, as per the other answers, jQuery is first import. I am using gulp as build system.<br /> My project is forked from <a href="https://github.com/google/web-starter-kit" rel="nofollow">https://github.com/google/web-starter-kit</a>

I run gulp serve:dist and see

$ gulp serve:dist [11:31:41] Requiring external module babel-register [11:31:43] Using gulpfile ~/WebstormProjects/my-website/gulpfile.babel.js [11:31:43] Starting 'clean'... [11:31:43] Finished 'clean' after 27 ms [11:31:43] Starting 'default'... [11:31:43] Starting 'styles'... [11:31:43] styles all files 7.41 kB [11:31:43] Finished 'styles' after 869 ms [11:31:43] Starting 'html'... [11:31:44] Starting 'scripts'... [11:31:44] Starting 'images'... [11:31:44] Starting 'copy'... [11:31:44] html index-rwd.html 14.96 kB [11:31:44] html index.html 3.94 kB [11:31:44] html all files 18.9 kB [11:31:44] Finished 'html' after 401 ms [11:31:45] scripts all files 99.11 kB [11:31:45] Finished 'scripts' after 1.83 s [11:31:45] copy all files 6.11 kB [11:31:45] Finished 'copy' after 1.64 s [11:31:45] images all files 542.87 kB [11:31:45] Finished 'images' after 1.79 s [11:31:45] Starting 'copy-sw-scripts'... [11:31:45] Finished 'copy-sw-scripts' after 3.05 ms [11:31:45] Starting 'generate-service-worker'... Total precache size is about 222 B for 1 resources. [11:31:45] Finished 'generate-service-worker' after 8.64 ms [11:31:45] Finished 'default' after 2.85 s [11:31:45] Starting 'serve:dist'... [11:31:45] Finished 'serve:dist' after 19 ms [WSK] Access URLs: ------------------------------------- Local: http://localhost:3001 External: http://192.168.1.65:3001 ------------------------------------- UI: http://localhost:3002 UI External: http://192.168.1.65:3002 ------------------------------------- [WSK] Serving files from: dist/public

What am I doing wrong?

Answer1:

You could try using this instead. Do you get the same error in multiple browsers?

(function($) { 'use strict'; // all functions here })(window.jQuery);

Answer2:

There were multiple problems.

First, in the index.html, I removed the section

<!-- build:js scripts/main.min.js --> <!-- endbuild -->

which gulp uses to combine JS and minify them in 1 file. The fix was to add the following

<!-- build:js scripts/main.min.js --> <script src="scripts/vendors/jquery-1.9.1.min.js"></script> <script src="scripts/vendors/waypoints.min.js"></script> <script src="scripts/main.js"></script> <!-- endbuild -->

Secondly, in gulp scripts task, I had to tell gulp as to what all scripts to copy. The fix looked like

gulp.task('scripts', () => gulp.src([ // Note: Since we are not using useref in the scripts build pipeline, // you need to explicitly list your scripts here in the right order // to be correctly concatenated './app/scripts/vendors/jquery-1.9.1.min.js', './app/scripts/vendors/waypoints.min.js', './app/scripts/main.js' // Other scripts ])

Lastly, as mentioned by @Wolfgang Leon in <a href="https://stackoverflow.com/questions/40091846/gulp-jquery-is-not-defined/43432867#43432867" rel="nofollow">Gulp - jQuery is not defined</a>, the jQuery v3.2.0 was somehow incompatible, so I replaced it with jQuery v1.9.1

This helped me fix the issue completely. #learnednewthings

Recommend

  • Grunt: including a generated file in usemin
  • SDN4 Config - java.lang.annotation.AnnotationFormatError: Invalid default: …EnableNeo4jRepositories.
  • Bluemix Push Notification service not working anymore?
  • TensorFlow stuck into endless loop using tf.while_loop()
  • How to disable auto download+open of Fabric.app on CI build server?
  • HTML tags within JSON (in Python)
  • Unexpected behavior with exception handling in async, possible bug?
  • How to stop a deadlock on one node from crashing entire cluster?
  • “Not a git repository: ” Error when trying to add RestKit to my repository
  • Integer Linear Programming Java: Multiple Open Source and Commercial tools are available. Which one
  • Getting rid of node-jsx
  • Serving files from BlobStore in GAE
  • Django how to modify database records by template
  • Java ClassLoader Confusion
  • AOT and JIT with build-optimizer
  • Python 2.7 on OS X: TypeError: 'frozenset' object is not callable on each command
  • When interface inheritance in Java is useful?
  • How many View Controllers should I have in my game?
  • Using Netbeans with Cygwin and SDL, including SDL.h creates strange error
  • Mercurial: Identify file name after rename
  • File extension of zlib zipped html page?
  • Python 3.2.2, error(scripts to exe)
  • How to generate and display a QR Code in ionic 2
  • error importing numpy
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • Seeking advice on Jetty HttpClient Hang
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • javaw.exe and eclipse startup problems
  • Shallow update not allowed (git > 1.9)
  • Google cloud sdk not working when python points python3
  • SVN: Merging two branches together
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • How do I rollback to a specific git commit
  • Is there a mandatory requirement to switch app.yaml?
  • Revoking OAuth Access Token Results in 404 Not Found
  • using HTMLImports.whenReady not working in chrome
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • UserPrincipal.Current returns apppool on IIS
  • Android Heatmap on canvas or ImageView