requirejs http://requirejs.org/docs/errors.html#scripterror
- First of all, you should change all
/Scripts/*
toScripts/*
. - Second thing is, you have set your data-main to
Scripts/sandbox
, that is, your baseUrl is set toScripts/
. So yourapp.js
should look like:
require(["sandbox"], function(sandbox){
sandbox.showName('John');
});
And your sandbox.js
should be:
require.config({
paths:{
"jquery":"jquery-1.9.1"
}
});
define(["jquery"], function($){
var show_name = function(n){
alert(n);
};
return{
showName : display_name
}
});
TheSoul
Updated on June 13, 2022Comments
-
TheSoul almost 2 years
I am trying to get my hands on require.js which is new for me but I cannot have it work. After having followed some simple tutorials for beginners, I proceeded to create my first (fairly simple project).
I am using visual studio 2013 (as I am a .net guy) This is a quick way on my file structure
./content /*a bunch of css files* ./Scripts /jquery-1.9.1.js /sandbox.js (!! my library !!) .index.html (on the root) .app.js (on the root)
Here is my module: sandbox.js
//sandbox.js require.config({ paths:{ "jquery":"/Scripts/jquery-1.9.1" } }); define(["jquery"], function($){ var show_name = function(n){ alert(n); }; return{ showName : display_name } });
And now in the app.js I make use of the module sandbox.js. Here is how
// app.js require(["/Scripts/sandbox"], function(sandbox){ sandbox.showName('John'); });
Finally in the index.html, I load my require.js file:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> </head> <body> <script src="/Scripts/require.js" data-main="/Scripts/sandbox"></script> <script src="app.js"></script> </body> </html>
But when I run this code I get the error
0x800a139e - JavaScript runtime error: Script error for: /Scripts/sandbox http://requirejs.org/docs/errors.html#scripterror
This error occurs in the app.js
require(['/Scripts/sandbox'], function (sandbox) { sandbox.showName("John"); });
Obviously there is something wrong in my way of caling and using the module sandbox.js. But I cannot figure out what the error.
Any help would be greatly appreciated.
-
Louis over 8 yearsThe RequireJS documentation is clear that you should not add
.js
at the end of module names. -
TheSoul over 8 yearsThen I don't know why it is working now. This is how I am consuming my sandbox module in the app.js : require( ['Scripts/sandbox.js'], function(sandbox) { sandbox.showName("Jack") } ); with the extension .js And every thing works
-
Louis over 8 yearsI note in your answer here and in your comment you have the module name
"Scripts/sandbox.js"
, whereas in your question it is"/Scripts/sandbox"
. Note the presence of a leading/
in your question and its absence in your answer. Did you really just add.js
or did you also add a leading/
? -
TheSoul over 8 yearsthanks for pointing out. I did not realize it at first since I was playing around, trying to figure out what could go wrong. I must have removed it without even thinking. And yes if I put back the leading / it's not working