18

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.

4
  • 1
    Does this work? import moment = require('./bower_components/moment/src/moment'); ? Or import moment = module('moment'); ? Commented Apr 27, 2016 at 14:45
  • See @Mr.Polywhirl's answer, "'node_modules/moment/moment' has no default export." means the script isn't a module Commented Apr 27, 2016 at 14:53
  • When I try import moment = module('moment') it gives the error "Cannot find namespace 'module'". Am I supposed to be including something for that? When I try the first piece of code (I am using node_modules) I get the error "Cannot find module '../node_modules/moment/src/moment'". Commented Apr 27, 2016 at 15:02
  • @Ozrix No it doesn't. It means that none of the exports are defined as default, so you need to tell it what you want to import. You can use * to import everything or curly braces to specify what you want. Commented Apr 27, 2016 at 15:55

2 Answers 2

29

The correct syntax is:

import * as moment from 'moment';
Sign up to request clarification or add additional context in comments.

7 Comments

You might need to use moment.Moment. E.g., {date: moment.Moment;}
be careful if you use babel on top of the above syntax, babel will prevent the function moment() from work as expected, due to how it associates with the spec. You can add "allowSyntheticDefaultImports": true to your tsconfig.json to allow import moment from "moment"; to no longer throw an error.
@DavidD thanks! That tsconfig.json setting seems needed for rollup, as using import * as moment from 'moment' gives an error: Cannot call a namespace ('moment') otherwise
Can anyone explain why import * as moment from 'moment' is the correct syntax for TS, but import moment from 'moment' is sufficient in JS?
This is not working with TS Version 3.6.2. even with "moduleResolution": "node", and "allowSyntheticDefaultImports": true, in tsconfig
|
11

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/"

2 Comments

You saved me some time, thankyou. This answer is the answer that works in all cases including angular libraries.
this should be the only correct answer, while most answers you can find in SO are just workarounds which are all clumsy, and I think Angular compiler can't be so dump.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.