简体   繁体   中英

require.js is not loading my module

I am using require.js and a library. Unfortunately it looks like when the library is loaded the result comes back as null. Sorry I may be a noob when it comes to using require.js

postmonger.js

    (function(root, factory){
       if(typeof define === 'function' && define.amd) {
        define('postmonger', [], function(){ return factory(root); });
        }else {
            root.Postmonger = factory(root);
        }
    }(this, function(root){
        root = root || window;
var Postmonger;
if (typeof(exports) !== 'undefined') {
        Postmonger = exports;
    } else {
        Postmonger = {};
    }
Postmonger.noConflict = function(){
        root.Postmonger = previous;
        return this;
    }
        console.log(Postmonger);
        return Postmonger;
    }));

Code that loads the postmonger library "customActivity"

define([
    'js/postmonger'
], function(Postmonger){
    'use strict';
   // console.log(postmonger);
    console.log(Postmonger);
});

My runtime script

<script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/require.js"></script>
    <script type="text/javascript">
        (function() {
            var config = {
                baseUrl: ''
            };

            var dependencies = [
                'customActivity'
            ];

            require(config, dependencies);
        })();
    </script>

When I load the page. The first console.log returns an object and the second returns Null. I must be missing something.

Try using postmonger instead of js/postmonger .

define([
    'postmonger'
], function(Postmonger){
    'use strict';
   // console.log(postmonger);
    console.log(Postmonger);
});

Edit - Why does javscript load but AMD module is null/undefined.

When requires sees path js/postmonger it tries to download the file at js/postmonger.js which it finds and loads successfully. But in this file we have a named module with name postmonger with following code

define('postmonger', [], function(){ return factory(root); });

So module with name js/postmonger is never defined and hence you get undefined. (You should be getting undefined as per my understanding, but you have mentioned it as null)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM