[英]React/node build error. Could not find a declaration file for module 'history'. 'node_modules/history/index.js' implicitly has an 'any' type
My project is suddenly having a build error.我的项目突然出现构建错误。 My current repo still has no problem, it only happens when I clone the repo to a new folder and install the package again then do npm run build.
我当前的 repo 仍然没有问题,只有当我将 repo 克隆到新文件夹并再次安装包然后执行 npm run build 时才会发生这种情况。 So I am so scared to update the package right now...
所以我现在很害怕更新包......
I checked the merge history, I don't think any code merge should cause this issue..我检查了合并历史记录,我认为任何代码合并都不会导致这个问题..
Here is the error message I got:这是我收到的错误消息:
$ 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';
| ^
This is the package.json:这是 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.
I tried:我试过了:
Nothing seems to work.似乎没有任何效果。
Looks like the new history
needs a dependency library @types/history
.看起来新的
history
需要一个依赖库@types/history
。 I did a npm i @types/history@4.7.9
and then did a npm i
again.我做了一个
npm i @types/history@4.7.9
然后又做了一个npm i
。 This fixed my problem.这解决了我的问题。
the problem seems to be from the history
package that you are using问题似乎来自您正在使用的
history
包
Could not find a declaration file for module 'history'.
找不到模块“历史”的声明文件。
try installing types for the package尝试为包安装类型
npm i @types/history npm i @types/历史
if that didn't work, then change the import to require如果这不起作用,则将导入更改为要求
const history = require("history")常量历史 = 要求(“历史”)
the @types/history
is deprecated and since version 5 of history
, the type declarations are provided inside the package itself. @types/history
已被弃用,并且从history
版本 5 开始,类型声明在包本身内提供。 updating the package should fix the issue:更新软件包应该可以解决问题:
npm install history@latest
Assuming its as common package you would install your package and the type declarations假设它是通用包,您将安装包和类型声明
npm install history
npm install --save-dev @types/history
and include the types in your project within the tsconfig compilerOptions -并在 tsconfig compilerOptions 中包含项目中的类型 -
"typeRoots": [
"./node_modules/@types"
],
"types": ["history"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.