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.
package.json
as well – Mymyaimport {Moment} from '../../moment/moment';
Now it works, but I don't know why it doesn't work whenimport {Moment} from 'moment';
It's happening in angular charts module by the way. – Uuge