Require.js Uncaught TypeError: undefined is not a function

I just started learning Require.js

the file system is similar:

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 I know that the 1st file loaded in the DOM is require.js, after which it loads lib / main.js

Now main.js is located

 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 save jquery.js in the same folder as in main.js, the code works fine, but if I changed the path to jquery / jquery.js and changed main. js as

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

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

+4
source share
1 answer

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

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

Or use 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); }); 
+7
source

All Articles