4

I've installed moment.js using npm install moment --save and it is now in my node_modules folder, but I don't know how to reference it in my app.

Q) How can I use moment.js in my Ionic 2 app, when I've installed it using npm?

Here's an abbreviated version of my app.ts:

import {App, IonicApp, Platform, Modal, Events, Alert, MenuController} from 'ionic-angular';
import {Type} from 'angular2/core';
import {OnInit, OnDestroy} from 'angular2/core';

// native stuff
import {Keyboard} from 'ionic-native';


// tried this but it can't find the module
//import {moment} from 'moment';

@App({
  templateUrl: 'build/app.html',
  config: {}, // http://ionicframework.com/docs/v2/api/config/Config/
  providers: []
})
class MyApp {
  isLoadingData: boolean = false;
  rootPageToExitOn: string;
  rootPage: Type;
  pages: Array<{icon: string, title: string, component: Type}>;
  showMenu: boolean;

  constructor(
    private app: IonicApp,
    private platform: Platform,
    private menu: MenuController,
    private _events: Events
  ) {
    this.initializeApp();

    // how to use moment() here ...?

  }
}
Rob
  • 14,746
  • 28
  • 47
  • 65
Dave
  • 5,283
  • 7
  • 44
  • 66

1 Answers1

3

The following worked for me.

First, install the type definitions for moment.

typings install moment --save

(Note: NOT --ambient)

Then, to work around the lack of a proper export:

import * as moment from 'moment';

From : https://stackoverflow.com/a/36290343/3279156

Community
  • 1
  • 1
sreeramu
  • 1,213
  • 12
  • 19
  • thanks, but installing the typings doesn't solve the issue of getting the actual node module included in the page. All that does is stop TypeScript throwing up. – Dave Apr 11 '16 at 12:18