TypeScript error: Cannot find module 'moment'
Asked Answered
C

5

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.

Corollary answered 1/5, 2020 at 5:54 Comment(4)
did you change anything in the code that made this error appear? is the import used differently?Mymya
maybe you should post your package.json as wellMymya
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.Uuge
There seems to be an issue with moment version 2.25.0. github.com/moment/moment/issues/5486. Try using 2.24.0Uuge
U
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.

Unexceptionable answered 1/5, 2020 at 6:52 Comment(0)
I
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.

Inexpugnable answered 1/5, 2020 at 6:23 Comment(0)
S
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');
Silvestro answered 1/5, 2020 at 6:38 Comment(0)
M
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'

Metempirics answered 27/12, 2022 at 7:31 Comment(0)
T
0

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

Transcaucasia answered 16/4 at 11:1 Comment(1)
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.Stony

© 2022 - 2024 — McMap. All rights reserved.