0

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.

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

2 Comments

but why does my javascript load?
Updated answer to answer your comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.