Sergey Aldoukhov Sergey Aldoukhov - 1 year ago 3360
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


import {moment} from 'moment/moment';

And without:

npm install moment --save


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 Source

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';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download