我有一个使用 npx create-react-app --template typescript
的 TypeScript React 应用程序。当我运行 npm start
时,我的一个文件中出现错误:
TypeScript error in /<path>/App.tsx:
Cannot find module 'moment'. TS2307
导入:
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"]
}
在package.json
中使用"moment": "^2.25.0"
。使用 npm。
查看 node_modules
目录,我可以看到 moment
包,并且 package.json
文件显示 moment
版本为 2.25.0
我尝试清除npm缓存,删除node_modules
和package-lock.json
,重新安装,像import * as moment from 'moment'
.
有什么想法吗?这只是今天随机开始发生的。提前致谢。
最佳答案
更新
2.25.1版本发布。这解决了问题。
旧答案
这是矩2.25.0版本的问题,
https://github.com/moment/moment/issues/5486
删除您的package-lock.json和node_modules文件夹,替换package.json中的这行代码
"moment": "2.24.0",
注意,删除^,否则它将继续安装2.25.0
然后npm install
这应该可以解决问题。
关于reactjs - typescript 错误: Cannot find module 'moment' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61537526/