Search code examples
javascriptrequirejsrequire

RequiredJS failed to Load a JS file


I have this simple example in RequiredJS and tried to load the jQuery file but throws an error. What am I doing wrong?

Hierarchy

index.html
scripts/
        libs/
             jquery.js

        compute/

config.js

requirejs.config({

        baseUrl: 'scripts/libs'
    });


    requirejs(['jquery'],
    function(sub) {
        console.log(sub);
    });

SCRIPT5022: Script error for "jquery" http://requirejs.org/docs/errors.html#scripterror require.js (7,175)


Solution

  • Something like below works for me:

    requirejs.config({
        // Path mappings for the logical module names
        paths: (function () {
            var pathObj = {};
            pathObj = {         
                'jquery': '../your/path/to/js/libs/jquery/jquery-3.1.1.min'          
            };        
            return pathObj;
        })(),   
    
        // Shim configurations for modules that do not expose AMD
        shim: {
            'jquery': {
                exports: ['jQuery', '$']
            }
        }    
    });
    require(['jquery'],
            function ($) {
                 $.ajax({/*some ajax call for example */});
            }
        );