RequireJS “jQuery not defined”

This is a super quick and simple post… but maybe it’ll save someone time down the line. I sure won’t make the mistake again. I spent more time than I would like to admit figuring out this issue. There are a lot of posts sprinkled around about different causes of the error, none of which were the cause of my error.

Given the following RequireJS config:

require.config({
    "baseUrl":"\/path\/to\/dir\/",
    "paths":{
        "jQuery":"jquery.js"
    }
});

I was trying to do the following:

require([jQuery], function ($) {
    console.log($);
});

That gave me the dreaded “jQuery not defined” error!

Solution

Add quotes around “jQuery”!

require(["jQuery"], function ($) {
    console.log($);
});

Without the quotes jQuery was being treated as a Javascript variable instead of a string. So when it was looking for the jQuery dependency it did not exist. “jQuery” on the other hand does.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>