7

I am trying a simple demo with requirejs and jquery for AMD. Following is my folder structure structure:

├── index.html
└── js
    ├── lib
    │   ├── jquery.js
    │   └── require.js
    └── main.js

in my index.html file i have the following content:

  <head>
    <script data-main="js/main" src="js/lib/require.js"></script>
  </head>

  <body>
    <div id="hello">    </div>

  </body>

and my main.js file looks like this:

define(['lib/jquery'], function ($) {

  $("#hello").html("Wow this works!");

});

But when i do, i get an error: Uncaught TypeError: undefined is not a function in main.js line 3.

What is wrong? I can't understand?

4
  • Have you define to call sjuery library? on your html just call require.js Commented Oct 19, 2012 at 23:43
  • Is it possible that #hello does not exist at the time of script execution? Commented Oct 19, 2012 at 23:44
  • i tried this as well: require(['lib/jquery'], function ($) { $(function(){ $("#hello").text("Wow this works!"); }); }); This also gives same error. Commented Oct 19, 2012 at 23:49
  • Have you taken a look at this answer to a similar question? Commented Nov 6, 2012 at 20:58

3 Answers 3

5

Read this issue: https://github.com/jrburke/requirejs/issues/435

My understanding is this is because of how jquery defines itself. It is using a named define call:

define( "jquery", [], function () { return jQuery; } );

So now, if you require 'lib/jquery' it won't work. You have to exactly require 'jquery' for it to work.

EDIT:

If you want to put jquery in lib/ folder and your base url to be the parent folder of lib/ (lib/../) you can use a shim like this:

requirejs.config({
  baseUrl: 'js',
  shim: {
    'lib/backbone': {
      deps: ['lib/underscore', 'lib/jquery'],
      exports: 'Backbone'
    },
    'lib/underscore': {
      exports: '_'
    },
    'lib/jquery': {
      exports: '$'
    }
  }
});

requirejs(['lib/jquery'], function($) {
  // use $
});
Sign up to request clarification or add additional context in comments.

3 Comments

On [requirejs.org/docs/jquery.html]( requirejs.org/docs/jquery.html), it is clearly stated that; // THIS WILL NOT WORK define(['lib/jquery'], function ($) {...});
If you don't configure requirejs, it will not work. But As you see, I have configured it before requiring it.
I mean you should not use 'lib/jquery'
1

I have tried your example without "$" in first line of main.js and it worked properly.

Fixed main.js:

define(['lib/jquery'], function () {
     $("#hello").html("Wow this works!");
});

3 Comments

as per my reading of the docs, define first takes an array and then values from that array are passed to an anonymous function. Hence I passed $ to that function, whats wrong with my implementation?
I'm not too sure, but try to console.log the parameter of anonymous function - it will return undefined (it did that when I was playing your code). So jQuery's $ was overwriten by undefined variable, and hence no jQuery function were available.
This just creates an implicit dependency on global object $, which sort of defeats the whole purpose of loading jQuery as a module in the first place.
0
<script data-main="js" src="js/lib/require.js"></script>
<script src="js/main.js"></script>

is what you want. the data-main attribute just points to what should be the root of relative request for it. So you'll need to load in main directly or through an script tag or a require() call in an inline script block

1 Comment

-1. This causes an Uncaught ReferenceError: define is not defined from main.js. Also: data-main does more than "just points to what should be the root of the relative request to for it". In the OP's example, it will actually load a script that will have module id main.

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.