I have a module that is loading a custom library. In that library I'm trying to set jquery and drupalSettings as dependancies, but they are not being loaded. Here is my library and code:

in questionnaire.libraries.yml:

  version: 1.1
      css/google_map.css: {}
    js/google_map.js: {}
    - core/jquery
    - core/jquery.once
    - core/drupalSettings

in the QuestionnaireController:

$element['#attached']['library'][] = 'questionnaire/google_map';          

I get this error when loading the page:

Uncaught TypeError: $ is not a function

I know the library is being loaded, because it is picking up on the javascript and css I have made. I'm assuming the dependencies are not being loaded, since that error means jquery is not being loaded. Any specific reason why I can't load dependancies from the core?

  • What if you replace the $ by jQuery ? – yvan Nov 3 '15 at 20:18
  • Most likely your custom JS is not wrapped in (function($) { code.. })(jQuery()); – user21641 Nov 4 '15 at 12:59
  • I tried that @IvanJaros and it didn't work. If I enter the element inspector and search for jquery, it's no where to be found. I ended up including jquery in the beginning of the twig file and it works for now... but I would like to get it working using the drupal way. – Rudy Nov 4 '15 at 16:48
  • 2
    As you said, the library works, so it has to be a problem with your JS code, which you need to post so that we can help you. Note that it's jQuery, not jquery, javascript and the console are case sensitive. – Berdir Nov 4 '15 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.