Importing moment in Angular 6

11,357

Solution 1

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.

Solution 2

Try the following to import moment:

import * as moment from 'moment';

Hopefully that helps!

Share:
11,357
tolga
Author by

tolga

Updated on June 04, 2022

Comments

  • tolga
    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
      VincenzoC about 5 years
    • Heretic Monkey
      Heretic Monkey about 5 years
      Consider using Luxon which is a modern rewrite of Moment.
    • amedina
      amedina about 5 years
      @HereticMonkey Wow, useful!
    • tolga
      tolga about 5 years
      No this is not a duplicate. The solution in that answer doesn't solve my problem. I explained with the UPDATE
    • Alexander Staroselsky
      Alexander Staroselsky about 5 years
      Your issue is related to the library angular-moment-timezone, not moment specifically. Try using moment-timezone instead.
  • tolga
    tolga about 5 years
    Please see the update in my question, I also have a problem with this solution.
  • Alexander Staroselsky
    Alexander Staroselsky about 5 years
    You’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
    tolga about 5 years
    I 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
    Alexander Staroselsky about 5 years
    That is a bigger issue potentially that can be resolved with updating versions of Angular. github.com/angular/angular/issues/21670
  • Alexander Staroselsky
    Alexander Staroselsky about 5 years
    So 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.