简体   繁体   English

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

[英]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:我试过了:

  1. delete the node_module --> npm install --> npm run build --> same error删除 node_module --> npm install --> npm run build --> 同样的错误
  2. clone the project again --> npm install --> npm run build --> same error再次克隆项目 --> npm install --> npm run build --> 相同的错误
  3. clone the older version of the project --> npm install --> npm run build --> same error克隆项目的旧版本 --> npm install --> npm run build --> 相同的错误

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")常量历史 = 要求(“历史”)

update更新

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 for where the compiler needs to look typeRoots 编译器需要查找的位置
  • types - the actual library names that can be found in typeRoots types - 可以在 typeRoots 中找到的实际库名称
"typeRoots": [
  "./node_modules/@types"
],
"types": ["history"]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 模块构建失败(来自 ./node_modules/babel-loader/lib/index.js):错误:找不到模块“./src/data” - Module build failed (from ./node_modules/babel-loader/lib/index.js): Error: Cannot find module './src/data' 反应构建错误:模块构建失败(来自 ./node_modules/babel-loader/lib/index.js): - React build error: Module build failed (from ./node_modules/babel-loader/lib/index.js): 模块构建失败(来自 ./node_modules/babel-loader/lib/index.js):错误:找不到模块“babel-preset-react” - Module build failed (from ./node_modules/babel-loader/lib/index.js): Error: Cannot find module 'babel-preset-react' React 应用程序的编译错误:模块构建失败(来自./node_modules/babel-loader/lib/index.js) - Compile error for a React app: Module build failed (from ./node_modules/babel-loader/lib/index.js) 错误 in./src/index.js 模块构建失败(来自./node_modules/babel-loader/lib/index.js): - ERROR in ./src/index.js Module build failed (from ./node_modules/babel-loader/lib/index.js): 未捕获的错误:模块构建失败(来自./node_modules/babel-loader/lib/index.js) - Uncaught Error: Module build failed (from ./node_modules/babel-loader/lib/index.js) 错误:模块构建失败(来自 ./node_modules/babel-loader/lib/index.js):语法错误: - Error: Module build failed (from ./node_modules/babel-loader/lib/index.js): SyntaxError: React Jest 无法从“node_modules/@mui/styled-engine/node/index.js”中找到模块“@emotion/styled” - React Jest Cannot find module '@emotion/styled' from 'node_modules/@mui/styled-engine/node/index.js' 错误 in./src/app.js 模块构建失败(来自./node_modules/babel-loader/lib/index.js):错误:找不到模块'@babel/preset-present-env' - ERROR in ./src/app.js Module build failed (from ./node_modules/babel-loader/lib/index.js): Error: Cannot find module '@babel/preset-present-env' ./src/main.ts - 错误:模块构建失败(来自./node_modules/@angular-devkit/build-angular/node_modules/@ngtools/webpack/src/ivy/index.js): - ./src/main.ts - Error: Module build failed (from ./node_modules/@angular-devkit/build-angular/node_modules/@ngtools/webpack/src/ivy/index.js):
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM