Jimmy Scray
Jimmy Scray

Reputation: 154

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.

Upvotes: 0

Views: 1829

Answers (1)

Vishwanath
Vishwanath

Reputation: 6004

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)

Upvotes: 1

Related Questions