Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Couldn't find a declaration file for module 'react' #9891

Closed
JJ810 opened this issue Oct 24, 2020 · 12 comments
Closed

Couldn't find a declaration file for module 'react' #9891

JJ810 opened this issue Oct 24, 2020 · 12 comments

Comments

@JJ810
Copy link

JJ810 commented Oct 24, 2020

Describe the bug

I created a typescript template create-react-app by running npx create-react-app --template typescript and tried to import
useEffect and useState. After I import and save file, I got an error: Could not find a declaration file for module 'react'

Did you try recovering your dependencies?

6.14.6

Environment

current version of create-react-app: 4.0.0
running from /Users/healer/.npm/_npx/25783/lib/node_modules/create-react-app

System:
OS: macOS High Sierra 10.13.6
CPU: (6) x64 Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
Binaries:
Node: 10.22.1 - ~/.nvm/versions/node/v10.22.1/bin/node
Yarn: 1.22.4 - /usr/local/bin/yarn
npm: 6.14.6 - ~/.nvm/versions/node/v10.22.1/bin/npm
Browsers:
Chrome: 86.0.4240.111
Edge: 86.0.622.51
Firefox: 81.0.2
Safari: 13.0.5
npmPackages:
react: ^17.0.1 => 17.0.1
react-dom: ^17.0.1 => 17.0.1
react-scripts: 4.0.0 => 4.0.0
npmGlobalPackages:
create-react-app: Not Found

Steps to reproduce

  1. run npx create-react-app --template typescript
  2. open App.tsx and import useEffect and/or useState import React, { useEffect, useState } from 'react'
  3. save file and refresh

Actual behavior

I got error and couldn't run the project.

Screen Shot 2020-10-24 at 4 01 35 PM

@ehsankhfr
Copy link
Contributor

ehsankhfr commented Oct 25, 2020

This is already discussed in #9882

@BrandonGardner2
Copy link

BrandonGardner2 commented Nov 3, 2020

This is already discussed in #9882
@ehsankhfr

I don't think that issue is discussing the same problem. This appears to be in reference to the type declaration files, not React itself. I have the same error on a fresh CRA Typescript project today and the issue you reference already had a fix merged.

I was able to resolve this by removing node_modules and reinstalling.

@JJ810
Copy link
Author

JJ810 commented Nov 3, 2020

@BrandonGardner2, you should run

npm install @types/react

to fix this issue. But I couldn't find where to add this package in this repo.

@BrandonGardner2
Copy link

@BrandonGardner2, you should run

npm install @types/react

to fix this issue. But I couldn't find where to add this package in this repo.

yea, it looked as if the @types/react failed to install in the initial creation. The dependency is created in the package.json. I just wiped node_modules in case anything else was missed.

luckily that is all that was needed haha.

@JJ810 JJ810 closed this as completed Nov 3, 2020
@shyaboi
Copy link

shyaboi commented Feb 13, 2021

@BrandonGardner2, you should run

npm install @types/react

to fix this issue. But I couldn't find where to add this package in this repo.

This worked for me, and i used the create-react-app tsx starter template.

@jowanwondergemincentro
Copy link

Why does this work with everyone, iand not me.
I am already struggling with this for a long time.
Even after copy pasting the entire folder 'react-native-barcode-scanner-google' in the @types folder, it keeps throwing the same error. I also tried to add this line, to the tsconfig.json "noImplicitAny": false".

All without any success. Any suggestion would be greatly appreciated

@LudwigThePig
Copy link

I ran npx create-react-app my-app --template typescript as described in the create-react-app docs and saw the same error.

Why is this happening for clean runs?

@jtremback
Copy link

jtremback commented Mar 1, 2021

I'm getting this issue as well, after installing the typescript template, and then installing reactstrap.

Was able to solve it by deleting node_modules and running npm install again

@brandonwaring
Copy link

@BrandonGardner2, you should run

npm install @types/react

to fix this issue. But I couldn't find where to add this package in this repo.

yea, it looked as if the @types/react failed to install in the initial creation. The dependency is created in the package.json. I just wiped node_modules in case anything else was missed.

luckily that is all that was needed haha.

I had the same issue. Adding and restarting the server fixed it.

@johnjesse
Copy link

johnjesse commented Jul 28, 2021

I had this issue today - i think something is going on with yarn and npm

running

npm create react-app --template typescript foo

I can see I have foo/node_modules/@types/react

but after running

cd foo
npm install <package>

The types aren't there anymore

Investigating - I think this is because I have yarn installed globally - and CRA is therefore using yarn to install the deps - after running the npm install command I have both a yarn.lock and package-lock.json

Running

npm create react-app --template typescript foo --use-npm

initially - to force CRA to use npm seemed to fix this issue... is this just a weird conflict due to me using npm to create my react app and install deps - and CRA using global yarn to install deps?

@Dan503
Copy link

Dan503 commented Jan 5, 2022

Quick fix for an already existing project:

npm i @types/react -D

@suhrob008
Copy link

image

why is it like that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests