How do I use an external library inside ember-cli generate?


#1

I’d like to load SugarJS into Ember-CLI as I run my custom blueprint. I have SugarJS in both bower.json and package.json, have run bower install and npm install, and restarted my Ember server but I get an error loading the library.


#2

There are 3 ways to load external libraries: ember cli addon, bower import, and vendor import.

Usually libraries loaded via ember addon is the easiest, just ember install .... bower dependencies needs to be added to bower.json and imported manually in ember-cli-build.js. vendor dependencies are files you placed inside the vendor/ directory and manually imported in ember-cli-build.js.

Unfortunately, dependencies installed via npm cannot be imported directly into ember.


#3

Thanks, lightblade. Just to clarify, I’m not trying to add SugarJS to my EmberJS app but to add it into ember-cli itself to help me generate.my app. I have successfully added SugarJS to the EmberJS app, for what that’s worth.

I have this added to my ember-cli-build.js:

  app.import('bower_components/sugar/dist/sugar.js');

  return app.toTree();

This is the output from ember g:

   C:\code\web>ember g test-blueprint test/product
   installing test-blueprint
   Sugar is not defined
   ReferenceError: Sugar is not defined
       at CoreObject.locals (C:\code\web\blueprints\test-blueprint\index.js:22:24)
       at CoreObject.<anonymous> (C:\code\web\node_modules\ember-cli\lib\models\blueprint.js:816:20)
       at initializePromise (C:\code\web\node_modules\ember-cli\node_modules\rsvp\dist\rsvp.js:588:5)
       at PromiseExt.Promise (C:\code\web\node_modules\ember-cli\node_modules\rsvp\dist\rsvp.js:1076:31)
       at new PromiseExt (C:\code\web\node_modules\ember-cli\lib\ext\promise.js:32:8)
       at CoreObject._locals (C:\code\web\node_modules\ember-cli\lib\models\blueprint.js:815:12)
       at CoreObject._process (C:\code\web\node_modules\ember-cli\lib\models\blueprint.js:405:17)
       at CoreObject.install (C:\code\web\node_modules\ember-cli\lib\models\blueprint.js:441:17)
       at CoreObject.run (C:\code\web\node_modules\ember-cli\lib\tasks\generate-from-blueprint.js:58:49)
       at CoreObject.run (C:\code\web\node_modules\ember-cli\lib\commands\generate.js:64:17)

This is the code in my blueprint’s index.js that seems to be causing the error:

options.title = Sugar.String.titleize(options.entity.name);


#4

I’ve just figured this out. I needed to explicitly add an explicit reference to the index.js in my blueprint’s locals function

I thought that ember generate was using node.js as the runtime, and this seems to be confirmed as removing SugarJS from node-modules caused the error again. When I re-added it with “npm install sugar --save” it worked again.

module.exports = {

  locals: function(options) {

    let Sugar = require('Sugar');