requirejs anonymous dependency not defined

2019-09-02 04:59发布

问题:

I have a problem with requirejs and a dependency being undefined.

My setup is the following:

var lib = function (){...};

define(function() {
    return lib;
});

And the modules are define like this

var mod = function (){
   ...
   lib('para') ...
};

define(["lib/lib"], function(lib) {
    return mod;
});

In my main.js I have this

require(['lib/lib'], function(lib){
   lib('para').mod();
})

The Problem: Lib is available in main.js but for mod I get an error Uncaught ReferenceError: lib is not defined

回答1:

So the actual problem was that everything was within an IIFE. This leads to define not being available, which leads to the If clause resolving in the else part.

Window.define is available however, so passing this to the IIFE solves my issue.