4

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.

1 Answer 1

7

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);
});
Sign up to request clarification or add additional context in comments.

4 Comments

Recently I used your code and saw that the jquery file is loading in Network Inspector but I am still confused why the require.js is not pushing the parameter in $ of the callback.
Not pushing the $ parameter in which callback?
require(['jquery/jquery'],function($){ //here $ is the parameter which is pushed or placed with the content of the jquery, right? $("#content").html("jquery am loaded"); });
What about using ./jquery/jquery instead (if jquery/jquery is relative to main.js). Note that is not the correct way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.