简体   繁体   中英

Meteor 1.3 and RequireJS

I had RequireJS 2.1.8 running fine with my Meteor 1.2.1 application. Specifically, in my Iron Router routes I had:

waitOn: function() {
    return IRLibLoader.load(requireUrl, {
        success: function() {
             require.config({
                baseUrl: 'http://...' // real URL here
             });
        }
    });
}

I upgraded to Meteor 1.3, but now I have the following error: "require.config is not a function".

Looking further, I can see that the modules-runtime package defines its own require variable.

I tried a "require = requirejs" right before the require.config call, which makes my require.config line run fine, but then Meteor hangs with the following error:

Exception from Tracker afterFlush function:
meteor.js?hash=ec96c6f…:913 TypeError: id.charAt is not a function
    at fileResolve (modules-runtime.js?hash=939c79d…:288)
    at require (modules-runtime.js?hash=939c79d…:90)
    at .<anonymous> (index.js:6)
    at blaze.js?hash=38069f4…:3331
    at Function.Template._withTemplateInstanceFunc (blaze.js?hash=38069f4…:3677)
    at fireCallbacks (blaze.js?hash=38069f4…:3327)
    at .<anonymous> (blaze.js?hash=38069f4…:3420)
    at blaze.js?hash=38069f4…:1773
    at Object.Blaze._withCurrentView (blaze.js?hash=38069f4…:2204)
    at blaze.js?hash=38069f4…:1772

Seems like Meteor doesn't want a "require" redefinition on its side.

How can I have Meteor 1.3 and RequireJS coexist happily.

Turns out that inside the waitOn function, Meteor 1.3 module.js had redefined the "require" variable. As a local variable, which is good practice, but it conflicted with RequireJS defining require as a global, so my code using the require global would fail.

Solved it with two steps: 1. Used requirejs.config instead of require.config 2. Rewrote my defines that use require inside of them as dependant upon require:

define([
    'require',
    ...
],
function (
    require,
    ...
) {
    require(...); // code that works :-)
});

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