Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using moment.js with Ember, Ember-CLI

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.

like image 728
hal9000 Avatar asked Mar 02 '26 02:03

hal9000


1 Answers

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

like image 192
Bek Avatar answered Mar 04 '26 12:03

Bek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!