0

I am writing code that uses Underscore JS in Drupal 9 that should be loaded from /core/assets/vendor/underscore.

I am using Chrome DevTools to see the sources and though jquery, jquery-once, and modernizer are all loading from /core/assets/vendor/ the underscore library is not.

I am almost positive that this was working last week. I have updated my code since then but it is the same use of underscore. I do this in Drupal 8 and it works for sure.

I have cleared the cache multiple times. I have verified that the files for underscore are in my filesystem.

Thanks, Josh

1
  • 4
    You have to load a library that is dependent on this. If not, it doesn't matter if the files exist, they won't load unless called or depended on by library definitions.
    – Kevin
    Jun 13 at 22:11
0

Since the library was not loaded by the theme in my case I needed to do a form alter and then load the library. You can use something like this:

    //Load JS
    $form['#attached']['library'][] = 'core/underscore';
    $form['#attached']['library'][] = 
           'my_module/my_forms.js';
}

If you are adding your own library will need to define that in my_module.libraries.yml. Something like this:

my_module.js:
  js:
    js/my_forms.js: {}
  dependencies:
    - core/jQuery
    - core/underscore 

Hope this helps.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.