15

I have a TypeScript React app using npx create-react-app --template typescript. When I run npm start, I get an error in one of my files:

TypeScript error in /<path>/App.tsx:
Cannot find module 'moment'.  TS2307

Import:

import moment from 'moment'

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": false,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react",
    "noImplicitAny": false,
    "experimentalDecorators": true
  },
  "include": ["src", "husky.config.js", "lint-staged.config.js"]
}

Using "moment": "^2.25.0" in package.json. Using npm.

Looking in the node_modules directory, I can see the moment package, and the package.json file says moment is on version 2.25.0

I've tried clearing npm cache, deleting node_modules and package-lock.json, reinstalling, importing like import * as moment from 'moment'.

Any ideas? This just randomly started happening today. Thanks in advance.

4
  • did you change anything in the code that made this error appear? is the import used differently? Commented May 1, 2020 at 6:00
  • maybe you should post your package.json as well Commented May 1, 2020 at 6:01
  • I am also having the same issue here, I replaced import {Moment} from '../../moment/moment'; Now it works, but I don't know why it doesn't work when import {Moment} from 'moment'; It's happening in angular charts module by the way. Commented May 1, 2020 at 6:01
  • 3
    There seems to be an issue with moment version 2.25.0. github.com/moment/moment/issues/5486. Try using 2.24.0 Commented May 1, 2020 at 6:20

5 Answers 5

25

Update

moment version 2.25.1 is released. This fixes the issue.

Old Answer

It's an issue of moment version 2.25.0,

https://github.com/moment/moment/issues/5486

Delete your package-lock.json and node_modules folder, replace this line of code in your package.json

"moment": "2.24.0",

note, remove the ^, else it will keep installing 2.25.0

then npm install

This should resolve the issue.

Sign up to request clarification or add additional context in comments.

Comments

4

You have to install a package before actually using it in your code. So you can Install moment library using npm (node package manager)

npm install moment

and then import this library

import * as moment from 'moment'

Then you are good to go.

Comments

2

You have done exactly right, nothing wrong in the scaffolding the app. Just a mistake in the import statement as it has been updated.

As of version 2.13.0, Moment includes a typescript definition file.

Install via NPM npm install moment

Import and use in your Typescript file-

import * as moment from 'moment';
let now = moment().format('LLLL');

Note: If you have trouble importing moment For Typescript 2.x try adding "moduleResolution": "node" in compilerOptions in your tsconfig.json file(in your app root directory) and then use any of the below syntax

import * as moment from 'moment';
import moment = require('moment');

For Typescript 1.x try adding "allowSyntheticDefaultImports": true in compilerOptions in your tsconfig.json file and then use the syntax

import moment from 'moment';
let now = moment().format('LLLL');

Comments

0
  1. npm install moment

  2. delete node_modules <--- if you still found error

  3. then npm i <--- install the package again

  4. and then import this library import * as moment from 'moment'

Comments

0

happened to me too (version 2.30) closing and opening the IDE solved it.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.