反应/节点构建错误。找不到模块“history”的声明文件。'node_modules/history/index.js' 隐式具有 'any' 类型

Tia*_*Qin 6 javascript node.js npm history.js reactjs

我的项目突然出现构建错误。我当前的存储库仍然没有问题,只有当我将存储库克隆到新文件夹并再次安装包然后执行 npm run build 时才会发生这种情况。所以我现在很害怕更新软件包......

我检查了合并历史记录,我认为任何代码合并都不会导致此问题。

这是我收到的错误消息:

$ npm run build

> container-client@2.32.0 build I:\ds\projects\new\container-service\client
> craco build

Creating an optimized production build...
Failed to compile.

I:/ds/projects/new/container-service/client/src/views/add-application/AddApplicationPage.tsx
TypeScript error in I:/ds/projects/new/container-service/client/src/views/add-application/AddApplicationPage.tsx(4,25):
Could not find a declaration file for module 'history'. 'I:/ds/projects/new/container-service/client/node_modules/history/index.js' implicitly has an 'any' type.
  If the 'history' package actually exposes this module, consider sending a pull request to amend 'https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/history`  TS7016

    2 | import './_index.scss';
    3 | import store from '../../store';
  > 4 | import { History } from 'history';
      |                         ^
      
Run Code Online (Sandbox Code Playgroud)

这是package.json:

  {
  "private": false,
  "dependencies": {
    "axios": "^0.19.2",
    "bootstrap": "^4.6.0",
    "history": "^4.10.1",
    "identity-obj-proxy": "^3.0.0",
    "react": "~17.0.1",
    "react-app-polyfill": "~2.0.0",
    "react-dom": "~17.0.1",
    "react-router-dom": "~5.2.0"
  },

Looks like the library "history" is causing the problem. 

Run Code Online (Sandbox Code Playgroud)

我试过:

  1. 删除node_module --> npm install --> npm run build --> 同样的错误
  2. 再次克隆项目 --> npm install --> npm run build --> 同样的错误
  3. 克隆项目的旧版本 --> npm install --> npm run build --> 相同的错误

似乎什么都不起作用。

Tia*_*Qin 3

看来新的history需要一个依赖库@types/history。我做了一个npm i @types/history@4.7.9,然后又做了一个npm i。这解决了我的问题。