Meteor 1.3 and RequireJS conflict

I had RequireJS 2.1.8 running fine with my Meteor 1.2.1 application.

I upgraded to Meteor 1.3, but now I have the following error: “require.config is not a function”. I explained the problem on StackOverflow, but I just also found out that you guys don’t like SO that much, and I can understand why. To save time, here’s my full question:

Specifically, in my Iron Router routes I had:

waitOn: function() {
    return IRLibLoader.load(requireUrl, {
        success: function() {
             require.config({
                baseUrl: Meteor.settings.public.rumeurUrl
             });
        }
    });
}

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?

PS: finally found out the answer and added it as a reply below for anyone facing the same problem.

You could try attaching the require.config from requirejs to Meteor’s require

e.g.

import requirejs from 'requirejs';
require.config = requirejs.config
1 Like

Good idea, thank you! Fortunately I could use requirejs instead.

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 :slight_smile:
    });

1 Like