Question

I'm having an issue attempting to create a RequireJS shim for some javascript code that was written by another team in my organization. The script is loaded via a noraml HTML script as such:

<script src="MyCustomModule.js" type="text/javascript"></script>

My main.js contains the following:

requirejs.config({
    paths: { 'text': 'durandal/amd/text' },
    shim: {
        'MyCustomModule': { exports: 'My.Custom.Module' }
    }
});

And I have tried accessing the custom module in a variety of ways, but this is my current code:

define(['MyCustomModule'], function (require, MyCustomModule) {
 ...
}

But each time the page/app loads I get an error from RequireJS indicating that it failed to load app/MyCustomModule.js (and I can see the 404 error in the console where it attempted to request the file from the server). What am I doing wrong?

Was it helpful?

Solution

You also need to include MyCustomModule in paths:

requirejs.config({
    paths: {
        'text': 'durandal/amd/text'
        'MyCustomModule': 'path/to/MyCustomModule'
    },
    shim: {
        'MyCustomModule': {
            exports: 'My.Custom.Module'
        }
    }
});

If you don't do that, define(['MyCustomModule'] (...) will look for the dependency in the baseUrl location, in your case: app/MyCustomModule.


In other words: shim can't "pick up" global variables that are not loaded by RequireJS.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top