tolga
tolga

Reputation: 2810

Importing moment in Angular 6

I imported moment in angular 6:

import moment from 'moment'

and

let formatted = moment(startDate).tz(timezone).format();

which seems to work fine...but ng serve outputs the following error:

Module '"/my/project/node_modules/moment/moment"' has no default export.

OK, I understand this error; it says that moment is not exported as default...but when I try to export this way:

import { moment } from 'moment'

but than, error says that moment is in fact an object, not a function.

How did it work in the first step than? How should I import the "moment"?

UPDATE I also tried as suggested in the other question...

import * as moment from 'moment';

But than I got the following result:

ERROR in node_modules/@angular/core/src/render3/ng_dev_mode.d.ts(9,11): error TS2451: Cannot redeclare block-scoped variable 'ngDevMode'.
node_modules/angular-moment-timezone/node_modules/@angular/core/src/render3/ng_dev_mode.d.ts(9,11): error TS2451: Cannot redeclare block-scoped variable 'ngDevMode'.

Upvotes: 2

Views: 13972

Answers (3)

tolga
tolga

Reputation: 2810

I found a solution...Firstly I suggest to review this issue page.

  1. I abandoned angular-moment-timezone and started using moment-timezone, the reason for ngDevMode error is angular-moment-timezone package has another node-modules inside it. This is a terrible package managing.

  2. I than imported as:

    import * as moment from 'moment'; import 'moment-timezone';

and now my code is working...also the project builds.

Upvotes: 0

Vishal Sharma
Vishal Sharma

Reputation: 1061

You can import moment directly in angular by using this:-

import * as moment from 'moment';

Upvotes: -5

Alexander Staroselsky
Alexander Staroselsky

Reputation: 38827

Try the following to import moment:

import * as moment from 'moment';

Hopefully that helps!

Upvotes: 6

Related Questions