React/node build error. Could not find a declaration file for module 'history'. 'node_modules/history/index.js' implicitly has an 'any' type
Asked Answered
K

3

6

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. 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

> [email protected] 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:

  {
  "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
  2. clone the project again --> npm install --> npm run build --> same error
  3. clone the older version of the project --> npm install --> npm run build --> same error

Nothing seems to work.

Khania answered 10/2, 2022 at 3:2 Comment(0)
K
4

Looks like the new history needs a dependency library @types/history. I did a npm i @types/[email protected] and then did a npm i again. This fixed my problem.

Khania answered 12/2, 2022 at 16:23 Comment(0)
F
3

the problem seems to be from the history package that you are using

Could not find a declaration file for module 'history'.

try installing types for the package

npm i @types/history

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. updating the package should fix the issue:

npm install history@latest
Faubion answered 10/2, 2022 at 8:8 Comment(3)
I tried both, the first one. I got the same error. Then I tried to use required instead of import, it is showing another error… history.push() is not defined.. Any more tips please?Khania
yep. I just visited their github page and it looks like they have added the declaration types inside the package itself. to cut it short, you don't need the @types/history package. just update history package to its latest versionFaubion
Why was my comment deleted? There was a big change from history v4 to history v5, and lots of people are unable to upgrade to v5. So if they've added a declaration file to v5, that won't work for those of us on v4. Upgrading to the latest version won't work for everybody. Do we know if there is a solution for v4 users?Lightly
S
0

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 -

  • typeRoots for where the compiler needs to look
  • types - the actual library names that can be found in typeRoots
"typeRoots": [
  "./node_modules/@types"
],
"types": ["history"]
Subsolar answered 1/7, 2022 at 15:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.