打字稿错误:找不到模块“时刻”

Joh*_*ohn 10 npm momentjs typescript reactjs

我有一个使用npx create-react-app --template typescript. 当我运行时npm start,我的一个文件中出现错误:

TypeScript error in /<path>/App.tsx:
Cannot find module 'moment'.  TS2307
Run Code Online (Sandbox Code Playgroud)

进口:

import moment from 'moment'
Run Code Online (Sandbox Code Playgroud)

配置文件

{
  "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"]
}

Run Code Online (Sandbox Code Playgroud)

使用"moment": "^2.25.0"package.json。使用 npm。

查看node_modules目录,我可以看到moment包,package.json文件moment显示版本为 2.25.0

我试过清除 npm 缓存、删除node_modulespackage-lock.json、重新安装、导入import * as moment from 'moment'.

有任何想法吗?这只是今天随机开始发生的。提前致谢。

Jpl*_*us2 19

更新

发布了 2.25.1 版。这解决了这个问题。

旧答案

这是moment version 2.25.0的问题,

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

删除你的package-lock.jsonnode_modules文件夹,在你的package.json 中替换这行代码

"moment": "2.24.0",
Run Code Online (Sandbox Code Playgroud)

注意,删除^,否则它将继续安装 2.25.0

然后npm 安装

这应该可以解决问题。