Score:0

Underscore JS Not Loading

yt flag

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

Kevin avatar
in flag
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.
Score:0
yt flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.