'Next.js is not recognizing '@types/react'

When I try to run my Next.js app with npm run dev I get an error message saying that I don't have the required packages to run Next with Typescript:

Please install @types/react by running:

        npm install --save-dev @types/react

If you are not trying to use TypeScript, please remove the tsconfig.json file from your package root (and any TypeScript files in your pages directory).

However, the module '@types/react' is installed. I tried running npm install --save-dev @types/react and got no error messages (just a bunch of warnings but I don't think they are the problem).

How can I solve this and run the project?



Solution 1:[1]

Seems like there is a bug in the current @types/react version (specifically v18.0.2), you can downgrade to 18.0.1 with npm install --save-dev @types/[email protected]

Source: https://github.com/vercel/next.js/issues/36085

Solution 2:[2]

I used yarn add -D @types/[email protected] and it worked perfectly! Netsu is right, seems like there is a bug in the current @types/react version (specifically v18.0.2).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Netsu
Solution 2 Rick Schultz