surfspider
surfspider

Reputation: 767

Visual Studio Code error TS2305 with moment.js example from angular material design tutorial

I tried the following code from https://material.angular.io/components/datepicker/overview#choosing-a-date-implementation-and-date-format-settings (see below). It complies and I can run the code but I get an error in visual studio code which says that 'default is not an exported member of moment. I am using Visual Studio Code 1.24.1

import { default as _rollupMoment} from 'moment';

enter image description here

ERROR in src/app/component.ts(20,10): error TS2305: Module '"../frontend/node_modules/moment/moment"' has no exported member 'default'.

import {Component} from '@angular/core';
import {FormControl} from '@angular/forms';
import {MAT_MOMENT_DATE_FORMATS, MomentDateAdapter} from '@angular/material-moment-adapter';
import {DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE} from '@angular/material/core';

// Depending on whether rollup is used, moment needs to be imported differently.
// Since Moment.js doesn't have a default export, we normally need to import using the `* as`
// syntax. However, rollup creates a synthetic default module and we thus need to import it using
// the `default as` syntax.
import * as _moment from 'moment';
// tslint:disable-next-line:no-duplicate-imports
import {default as _rollupMoment} from 'moment';

const moment = _rollupMoment || _moment;

/** @title Datepicker that uses Moment.js dates */
@Component({
  selector: 'datepicker-moment-example',
  templateUrl: 'datepicker-moment-example.html',
  styleUrls: ['datepicker-moment-example.css'],
  providers: [
    // `MomentDateAdapter` and `MAT_MOMENT_DATE_FORMATS` can be automatically provided by importing
    // `MatMomentDateModule` in your applications root module. We provide it at the component level
    // here, due to limitations of our example generation script.
    {provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE]},
    {provide: MAT_DATE_FORMATS, useValue: MAT_MOMENT_DATE_FORMATS},
  ],
})
export class DatepickerMomentExample {
  // Datepicker takes `Moment` objects instead of `Date` objects.
  date = new FormControl(moment([2017, 0, 1]));
}

Upvotes: 1

Views: 2628

Answers (5)

Zelena
Zelena

Reputation: 63

I have the same error with Diff, has no exported member 'Diff'. So, I have to solve it in the next way. Hope it will be helpful for smb

import moment from 'moment';
import Diff = moment.unitOfTime.Diff;

Upvotes: 0

Tim K.
Tim K.

Reputation: 224

Note: If you have trouble importing moment, try adding "allowSyntheticDefaultImports": true in compilerOptions in your tsconfig.json file and then use the syntax

https://momentjs.com/docs/#/use-it/typescript/

it worked for me :)

Upvotes: 1

Weekend Baf
Weekend Baf

Reputation: 307

I have the same error. Then I change my code like this:

import * as moment from 'moment';
type Moment = moment.Moment;
const moment1 = moment;

export class AppComponent {
date = new FormControl(moment1());
}

And it works.

Hope this helps.

Upvotes: 0

surfspider
surfspider

Reputation: 767

I just commented out the second statement as follows:

import * as _moment from 'moment';
// import { default as _rollupMoment} from 'moment';
// const moment = _rollupMoment || _moment;
const moment = _moment;

Upvotes: 0

surfspider
surfspider

Reputation: 767

I left away the second import statement for moment as proposed in Importing moment into Angular gives error. Now it works...

Upvotes: 0

Related Questions