Yulric Sequeira
Yulric Sequeira

Reputation: 1452

Importing moment into TypeScript project

I am trying to use moment in my TypeScript project but when I use the line,

import moment from 'moment';

I get the error:

'node_modules/moment/moment' has no default export.

I have also tried,

import moment from 'moment/src/moment';

but then I get the error:

'Cannot find module moment/src/moment'.

Does anybody know a way of doing this? Thanks.

Upvotes: 18

Views: 18261

Answers (2)

Tim K.
Tim K.

Reputation: 224

I had the same issue and solved the problem in my project by adding allowSyntheticDefaultImports: true in compilerOptions in my tsconfig.json file and then I used the syntax

import moment from 'moment';

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

Upvotes: 11

rgvassar
rgvassar

Reputation: 5811

The correct syntax is:

import * as moment from 'moment';

Upvotes: 29

Related Questions