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!

Michail Michailidis
  • 11,792
  • 6
  • 63
  • 106
  • 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. – chrisbajorin Mar 01 '16 at 18:26
  • Thanks @cdbajorin but in my require js config "underscore" points to the lodash library.. so technically there is no underscore – Michail Michailidis Mar 01 '16 at 18:27
  • Post the code you're using to load lodash – chrisbajorin Mar 01 '16 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 – Michail Michailidis Mar 01 '16 at 18:34
  • @cdbajorin even tried adding in my shim: ```"lodash": { exports: "_" }, "underscore": { exports: "_" },``` without any luck – Michail Michailidis Mar 01 '16 at 18:50
  • I'm still not fully understanding what you've posted, as you have some code in your post, and some explanations in the comments. It's hard to put together. I believe your issue is that you need to add it to the `define` section as `["underscore"], function(_) { ... })`, not in the shim. – chrisbajorin Mar 01 '16 at 18:57
  • @cdbajorin hopefully the setup is more clear now. Thanks in advance – Michail Michailidis Mar 01 '16 at 19:18
  • Possible duplicate of [Mismatched anonymous define() module](http://stackoverflow.com/questions/15371918/mismatched-anonymous-define-module) – Louis Mar 03 '16 at 14:38
  • @Louis this doesn't answer my question - the only thing I changed was the path from underscore to lodash in my config. I don't see why this would cause my application to fail. The error is coming directly from lodash library that does ```return _;``` – Michail Michailidis Mar 03 '16 at 14:49
  • @MichailMichailidis The set of possible causes for the error you are getting limited to a well defined set, and these causes are addressed in the answers to the other question. – Louis Mar 03 '16 at 15:10
  • @Louis I have read the documentation but that doesn't answer the question why keeping everything the same and switching from underscore to lodash I get this error. I want to know why specifically lodash. – Michail Michailidis Mar 03 '16 at 17:03

0 Answers0