Moments in Ionic 2

Hi All,

I am currently using Ionic 2 (Angular 2 RC 4)

I am trying to make use of Moments in order to format dates.

The latest version of Moment is 2.14.1, which is avaialble for Angular 2 RC 6. So when I run the install command, I get:

npm install moment --save
myApp@ E:\Development\IDE\ionic-apps\myApp
+-- UNMET PEER DEPENDENCY @angular/core@2.0.0-rc.4
`-- moment@1.7.2

The problem is with 1.7.2, I get the following TS error:

import * as Moment from 'moment';
    [ts] 
    Cannot find module 'moment'.
ERROR in ./app/pages/chats/chats.ts
(1,25): error TS2307: Cannot find module 'moment'.

If I install the latest version 2.14.1, of Moments, I do not get this error. But it does compalin that I need Angular 2 RC 6.

As advised here, if we are using Ionic 2, we should stay on Angular RC 4, and not upgrade to RC 5, let alone 6.

Does anyone know how to get Moments to work on Ionic 2?

This and this (which I am trying to implement when I get this error) seem to indicate it is possible with Ionic 2. So I am guessing my versions are not compatible.

Thanks

p.s. moment, not to be confused with angular2-moment.

  "dependencies": {
    "@angular/common": "^2.0.0-rc.4",
    "@angular/compiler": "^2.0.0-rc.4",
    "@angular/core": "^2.0.0-rc.4",
    "@angular/forms": "^0.2.0",
    "@angular/http": "^2.0.0-rc.4",
    "@angular/platform-browser": "^2.0.0-rc.4",
    "@angular/platform-browser-dynamic": "^2.0.0-rc.4",
    "@ionic/cloud-angular": "^0.4.0",
    "angular2-moment": "^1.0.0-beta.1",
    "es6-shim": "^0.35.0",
    "ionic-angular": "^2.0.0-beta.11",
    "ionic-native": "1.3.2",
    "ionicons": "3.0.0",
    "moment": "^1.7.2",
    "reflect-metadata": "^0.1.3",
    "rxjs": "5.0.0-beta.6",
    "socket.io": "^1.4.8",
    "zone.js": "^0.6.12"
  },

I use “moment”: “^2.14.1” with “@angular/common”: “^2.0.0-rc.4”

I import the file like following

 import * as moment from 'moment';

But what you may miss is the definition file. Did you install it?
Something like

typings install dt~moment --global --save

Thanks for the fast reply.

I did:

npm install moment --save

let me try it your way.

When I try it your way, at run time I get the following error:

TypeError: core_1.NgModule is not a function

I think this means it is trying to use RC 5 or 6

sudo npm install moment --save

=> this install the library

but furthermore to that you’ll need the typescript definition file. that’s why you “have” to write one or install one using typings like I suggested above

1 Like

That’s probably my problem. How do you write typescript definition file?

If I run the following, is this sufficient (excuse my ignorance)?

typings install dt~moment --global --save
sudo npm install moment --save

then import like this:

import * as moment from 'moment';

That should work fine (I hope). You still face an error?

Thanks, I am just trying it.

I do get this error when I install:

npm install moment --save
myApp@ E:\Development\IDE\ionic-apps\myApp
+-- UNMET PEER DEPENDENCY @angular/core@2.0.0-rc.4
`-- moment@2.14.1

error

angular2-moment@1.0.0-beta.1 requires a peer of @angular/core@^2.0.0-rc.5 but none was installed.

And at runtime:

TypeError: core_1.NgModule is not a function

What version of angular2-moment are you using?

Sorry I don’t have angular2-moment installed. I think you’ve got there a problem of version. Is it possible to install an older version of angular2-moment? Just an idea, never used it.

And just for the record, if I understand correctly, you add two different issues here

A npm error: angular2-moment.1.0.0-beta.1 need rc5 of angular where you use rc4

A typescript error: moment module wasn’t found. this should be I hope fixed now.

If I downgrade my angular2-moment then it works for me.

npm install angular2-moment@0.8.2 --save

So it looks like later version of angular2-moment want RC 5.

2 Likes

Coolio. And the other typescript error “Cannot find module ‘moment’.” is also gone/solved?

Yes, I get no errors now. I only got that error when I used an old version of moment, but the latest ^2.14.1 fixes that.

I appreciate your help. You saved me a lot of time.

1 Like

Awesome, my pleasure, glad to hear everything’s fine now!

Got it to work using npm install angular2-moment@0.8.2 --save

For those that also want moment-timezone use version 0.5.4

2 Likes