Sergey Aldoukhov Sergey Aldoukhov - 4 months ago 947
TypeScript Question

How to use moment.js library in angular 2 typescript app?

I tried to use it with typescript bindings:

npm install moment --save
typings install moment --ambient -- save


test.ts:

import {moment} from 'moment/moment';


And without:

npm install moment --save


test.ts:

var moment = require('moment/moment');


But when I call moment.format(), I get an error.
Should be simple, can anybody provide a command line/import combination that would work?

Answer

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';