Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Require.js Uncaught TypeError: undefined is not a function

I just started learning Require.js

the file system is like :

enter image description here

This is my index.html

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>

    <script type="text/javascript" src="lib/require/require.min.js" data-main="lib/main"></script>

</head>
<body>
    <span id="content"></span>
</body>
</html>

Now, this I knows that the 1st file that loaded on to the DOM is require.js then after that it loads lib/main.js

Now the main.js is

require(['jquery'],function($){
     //this works since both **main.js** and **jquery.js** are in same folder
     $("#content").html("jquery am  loaded");
});

enter image description here Now here is the problem if I keep jquery.js in the same folder as that in main.js the code works fine, but if I change the path to jquery/jquery.js and change the main.js as

require(['jquery/jquery'],function($){
     //this thing shows 'Uncaught TypeError: undefined is not a function'
     $("#content").html("jquery am  loaded");
});

I understood the problem is that it is not loading the jquery.js if it is inside any other folder other that main.js is, but why, please shed some light and how that can be achieved.

like image 689
Ankur Verma Avatar asked Mar 24 '23 08:03

Ankur Verma


1 Answers

To use RequireJS and jQuery, you should either use the combined RequireJS/jQuery file, available at:

http://requirejs.org/docs/jquery.html

Or use a path.

http://requirejs.org/docs/api.html#config-paths

require.config({
    paths: {
        "jquery": 'http://code.jquery.com/jquery-1.9.1'
    }
});

require(["jquery"], function ($) {
    console.log($.fn.jquery);
});
like image 141
Paul Grime Avatar answered Apr 05 '23 13:04

Paul Grime