Reputation: 1300
This is what my page looks like
<script>
// set the configuration for require.js syncronously
var require = {
context: 'default',
baseUrl: '/scripts',
bundles: {
'bundles/main': ['utils']
}
};
</script>
<script src='/scripts/libs/require.js'></script>
<script>
// this throws an error because it tries to load module1.js from the current relative path instead of /scripts/module1.js
require(['module1'],function(module1){
module1.test();
});
</script>
I've reviewed a couple similar questions on stackoverflow like this answer:Require JS is ignoring my config
and the documentation here: https://github.com/requirejs/requirejs/wiki/Patterns-for-separating-config-from-the-main-module
If I'm understanding this correctly, by declaring require syncronously before including the require script, the settings should be available to require by the time it loads module1, but it seems to not be working.
I initially tried it this way with the same result:
<script src='/scripts/libs/require.js'></script>
<script>
require.config({
context: 'default',
baseUrl: '/scripts',
bundles: {
'bundles/main': ['utils']
}
});
</script>
EDIT: Apparently when I remove the context line it works properly. I have no idea why that would break it though.
Upvotes: 1
Views: 73
Reputation: 151380
The problem is that you are issuing a require
call in a context different from the context you are using in your configuration. The default name for a context if you do not specify it is _
(not default
). With the code you show, the context in your configuration is default
. But you use the default require
call provided by RequireJS, which operates in the context named _
.
When you specify a context name different from the default, you want to save the return value from the require.config
call, and the use that value instead of the default require
:
var myReq = require.config({
context: 'default',
baseUrl: '/scripts',
bundles: {
'bundles/main': ['utils']
}
});
myReq(['module1'],function(module1){
module1.test();
});
See here for the documentation regarding how to use context
.
If you do not actually need to use a context, then the easiest solution is to just remove context: 'default'
from your configuration.
Upvotes: 1