Importing moment in Angular 6
Solution 1
I found a solution...Firstly I suggest to review this issue page.
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.
-
I than imported as:
import * as moment from 'moment'; import 'moment-timezone';
and now my code is working...also the project builds.
Solution 2
Try the following to import moment:
import * as moment from 'moment';
Hopefully that helps!
tolga
Updated on June 04, 2022Comments
-
tolga almost 2 years
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'.
-
VincenzoC about 5 yearsPossible duplicate of How can moment.js be imported with typescript?
-
Heretic Monkey about 5 yearsConsider using Luxon which is a modern rewrite of Moment.
-
amedina about 5 years@HereticMonkey Wow, useful!
-
tolga about 5 yearsNo this is not a duplicate. The solution in that answer doesn't solve my problem. I explained with the UPDATE
-
Alexander Staroselsky about 5 yearsYour issue is related to the library angular-moment-timezone, not moment specifically. Try using moment-timezone instead.
-
-
tolga about 5 yearsPlease see the update in my question, I also have a problem with this solution.
-
Alexander Staroselsky about 5 yearsYou’re indicating that if you remove this import and anything related to moment in your code, you do no see this issue when running build?
-
tolga about 5 yearsI can run the code with "import moment from 'moment'"... but I can't build or start ng serve again. This is what I found strange.
-
Alexander Staroselsky about 5 yearsThat is a bigger issue potentially that can be resolved with updating versions of Angular. github.com/angular/angular/issues/21670
-
Alexander Staroselsky about 5 yearsSo your issue looks to be with angular-moment-timezone library specifically. Can you just use moment-timezone instead? Try even removing angular moment timezone and removing any code related to timezone and it should build.