Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import Underscore into my Ember CLI addon as a module?

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?

like image 960
Sam Selikoff Avatar asked Oct 19 '22 14:10

Sam Selikoff


1 Answers

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 ;)).
like image 66
rollingBalls Avatar answered Nov 01 '22 12:11

rollingBalls