I am having a problem where almost-identical require.js config calls appear to work differently, in one case failing to set baseUrl
and path
.
Folder structure
- /js
- - app/
- - libs/
- - - hogan.js
- - - backbone
- - - - backbone.js
- - - - etc.
- - - boostrap
- - - jquery
- - - - jquery.js
- - - - etc.
- - - require
- - - - require.js
- - - - etc.
- - templates/
- - require.config.js
Script tag
<script data-main="/resources/js/require.config" src="/resources/js/libs/require/require.js"></script>
Require configuration
require.config(
{
baseUrl: "/resources/js/libs",
shim: {
"underscore": {
exports: "_"
},
"backbone": {
deps: [
"json2",
"underscore",
"jquery"
],
exports: "Backbone"
}
},
paths: {
"app": "/resources/js/app",
"backbone": "backbone/backbone",
"jquery": "jquery/jquery",
"templates": "/resources/js/templates",
"templateEngine": "hogan"
}
}
);
require(
[
"app/router"
],
function( AppRouter ){
AppRouter.execute();
}
);
In both cases, a script tag gets appended that shows the full contents of require.config.js, starting with:
<script type="text/javascript" charset="utf-8" data-requirecontext="_" data-requiremodule="require.config" src="/resources/js/require.config.js">
Definition in app/router
define(
[
'backbone',
'templateEngine'
],
// etc
);
However where the problem occurs, app/router is never loaded, only the following (no 404, nothing):
GET require.js 200 OK 180ms
GET require.config.js 200 OK 69ms
If I enter require(["app/router"]);
into the Firebug console I get the following:
"NetworkError: 404 Not Found - /resources/js/backbone.js
"NetworkError: 404 Not Found - /resources/js/templateEngine.js"
When what I expect to see is:
- /resources/js/libs/backbone/backbone.js
- /resources/js/libs/hogan.js
Solutions attempted
- I have attempted to use various combinations of relative and absolute paths for
baseUrl
andpaths
. For examplebaseUrl: libs
, andpaths: { "app" : "../app" }
, etc. - I tried
deps
andcallback
instead of usingrequire
belowconfig
. - I have added the property
context
, matchingbaseUrl
. - I have moved require.config.js into the libs directory (editing
baseUrl
andpaths
as appropriate; still get the same issue of ignored config, only it tries to load /resources/js/libs/backbone.js instead, for example).
Note
This folder structure and config file does work on my localhost, where I remove /resources/ from the paths as shown above. Instead all-relative paths (none starting with /) are used for baseUrl
and paths
. But on the actual server that is not an option.