1

I am working on an application that uses requirejs and loads underscore with it but also loads underscore as a node_module using a script. Since we needed functionality found in lodash I decided to replace underscore with lodash in requirejs and in script tags. While it was working with underscore when I replaced it with lodash I started getting:

Uncaught Error: Mismatched anonymous define() module: function () {
      return _;
    }
http://requirejs.org/docs/errors.html#mismatch

I am using the following npm versions:

requirejs "2.1.5" underscore: "1.3.3" and I want to install lodash: "4.5.1"

Here is the requirejs config part that I can post:

 require.config({
        waitSeconds: 15,
        "paths": {
            underscore: "../../node_modules/lodash/lodash",
            jquery: "lib/jquery-1.7.2",
            Backbone: "lib/backbone",
            moment: "lib/moment-2.0.0",
            text: "lib/text",
            alertify: "lib/alertify",
            datatables: "lib/jquery.dataTables",
            datatablesjqueryui: "lib/dataTables.jqueryui"
        },
        "shim": {
           XYZ: ["underscore","jquery", ...]
           ...
        }
    });
  • The above is part of a concat file that contains also node_modules/requirejs/require.js before it

  • I also have another concat after it with the rest of the libraries that include lodash in it.

  • Both concat files are added using a <script> tag.

  • In the last concat I have a lot of files that do

var _ = require("underscore"); or have it in their define dependencies like define(["underscore"],function(_) { ... });

so hopefully i won't need to change anything there

I would appreciate if someone can explain why this happens only with lodash and not underscore. And if there is a solution that will let me add lodash in the concatenated file after it was imported/required through requirejs.

Per possible duplicate: I made sure there are no anonymous defines in the application and I made sure there is no underscore loaded anywhere. So the question is different because I only changed the path from underscore to dash library for the whole application and nothing else.

Thanks!

11
  • It doesn't happen only with lodash. It happens when you use lodash AND underscore. They both use the same variable name _. If you're going to use lodash, get rid of underscore. It's a superset anyway. Commented Mar 1, 2016 at 18:26
  • Thanks @cdbajorin but in my require js config "underscore" points to the lodash library.. so technically there is no underscore Commented Mar 1, 2016 at 18:27
  • Post the code you're using to load lodash Commented Mar 1, 2016 at 18:29
  • @cdbajorin I posted the require.config (part of it) that I am using.. lodash is also concatenated to a concat file that is loaded after the require.js configuration. There are a LOT of places that use underscore as a require/define dependency Commented Mar 1, 2016 at 18:34
  • @cdbajorin even tried adding in my shim: "lodash": { exports: "_" }, "underscore": { exports: "_" }, without any luck Commented Mar 1, 2016 at 18:50

0

Browse other questions tagged or ask your own question.