Magento 2 & $.widget is not a function

I thought I was going crazy. More of my time spent of the last 3 years was working in the middle and back-end of Magento. Still, I touched the front-end enough that I felt confident I knew what I was doing. I know templates, JavaScript, jQuery, CSS, and HTML. I know Magento. One thing I could not get past was this $.widget is not a function error that was causing all my JavaScript to break.

After a few rounds of google and stackoverflow, here is what I found:

– I could be including my JavaScript files in the wrong order and loading before jQuery. This wasn’t it.
– I might not have properly wrapped my jQuery code to include jquery and jquery-ui. This wasn’t it.
– I just needed to tell Magento to combine all my JavaScript files. This wasn’t it.
– I maybe had the wrong version of jQuery UI installed. This wasn’t it.

Those were the common themes. I went in circles for hours with each of those, reading posts over and over. The point had been reached here I was convinced I was crazy. Something in the jQuery framework must have changed drastically over the last year and I didn’t know what I was doing anymore. That didn’t make sense at all.

I ran across one final post. This was either by accident or because God brought it to my attention (I’m going with this one). The post was simply talking about “How do you include jQuery in your Magento 2 theme”. The person asking the question was directed NOT to include their own jQuery library, but to use Magento’s.

This triggered a thought. “None of the rest of the site was built properly, maybe they used their own library somewhere.” Sure enough, in our footer template was this fatal line:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

We have an active map that fills the background of our footer. This is brought in by the Google Maps API. Google’s examples are very good. They even include the jquery library.

I commented this out, re-deployed our static content, and now things are working fine across all browsers.

I hope this helps your $.widget is not a function adventure!

Written by Dan
Welcome to my blog! Here you'll find my collection of bible study lessons, book reviews, and other posts I feel inspired to write. I am a Christian currently serving at First Baptist Church of Northville in Northville, MI.