Reputation: 771
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.
Upvotes: 15
Views: 37611
Reputation: 1
happened to me too (version 2.30) closing and opening the IDE solved it.
Upvotes: 0
Reputation: 59
npm install moment
delete node_modules <--- if you still found error
then npm i <--- install the package again
and then import this library import * as moment from 'moment'
Upvotes: 0
Reputation: 2531
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.
Upvotes: 25
Reputation: 1243
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.
Upvotes: 4
Reputation: 294
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');
Upvotes: 2