2
votes

I have been trying to get the moment.js library to work with an Ember application using the Ember-cli. Note I am fairly new to ES2015.

Ember 2.2.0 Ember-cli 1.13.13

my ember-cli-build

/*jshint node:true*/
/* global require, module */
var EmberApp = require('ember-cli/lib/broccoli/ember-app');

module.exports = function(defaults) {
    var app = new EmberApp(defaults, {
        //options
    });

    app.import('bower_components/moment/moment.js');

    return app.toTree();
};

and my .jshintrc

"predef": [
    "document",
    "window",
    "-Promise",
      "moment"
  ],

and my controller

/* global moment:true */
import Ember from 'ember';

export default Ember.Controller.extend({
    firstName: null,
    lastName: null,
    day: function () {
        return moment('Dec 25, 1995');
    }.property()
});

I always get an error that 'moment' has not been defined. I read that the latest moment.js has stopped producing a global 'moment' but I'm not sure what to do about it.

The builds work. If I pull up the generated vendor.js I can see the complete contents of the moment.js file in it. However when I look at vendor.js in the chrome debugger the contents are omitted. I wasn't sure if this was due to some optimization.

1
I'm not 100% sure, but you could try import moment from 'bower_components/moment/moment.js';. If moment.js now use ES6-Modules this should work. (And I'm curious myself) - Jeff
I think you got your globals wrong it should be /* global moment */, so you might be getting just jshint warnings - Bek
just for fun I tried @Jeff's suggestion, I was curious as well... this produced an error on the route Error while processing route: index Could not find module bower_components/moment/moment.js imported from es2015-test/controllers/application Error: Could not find module bower_components/moment/moment.js imported from es2015-test/controllers/application - hal9000
@bek it turns out that I had it working.. and for the record both /* global moment / and / global moment:true */ work. - hal9000

1 Answers

9
votes

the best way to install moment.js in ember app is to use ember moment addon after installing it you will be able to use it as

import moment from 'moment';

plus you will get various helpers