5

I'm trying to import Underscore as a named module into my Ember CLI addon. Looking at the Standard AMD Asset section of the guides, it seems this should work:

app.import(app.bowerDirectory + '/underscore/underscore.js', {
  exports: {
    'underscore': ['default']
  }
});

Here's the line from Underscore's source:

define('underscore', [], function() {
  return _;
});

I tried to import it in one of my files, /addon/utils/class.js:

import _ from 'underscore';

and got an error:

Could not find module underscore imported from ember-cli-mirage/utils/class

What'd I do wrong?

Sam Selikoff
  • 12,366
  • 13
  • 58
  • 104
  • Weird, I attempted the same thing, but I didn't run into the error. The app.import statement initializes _ as a global in my app, whereas if I try to do `import _ from 'underscore'` the app won't load at all. – Max Wallace Apr 26 '15 at 13:35

1 Answers1

3

This is the problem: if (typeof define === 'function' && define.amd)

define.amd is not defined in ember-cli's loader.js.

Solutions:

  • Wrap it yourself.
  • Use browserify to do the wrapping for you
  • Use LoDash instead (this is what I suggest - easy, peazy, problem solved & you get time for an extra round of mojitos at the beach bar ;)).
Patsy Issa
  • 11,113
  • 4
  • 55
  • 74
rollingBalls
  • 1,808
  • 1
  • 14
  • 25
  • 1
    nice, thanks! fyi, I believe [ember-browserify](https://github.com/ef4/ember-browserify) will soon lessen these pains points, since underscore also exports a commonjs module – Sam Selikoff Jun 17 '15 at 18:02