create-react-app: TypeScript 4.1: Could not find a declaration file for module 'react/jsx-runtime'
THIS IS NOT AN ISSUE WITH create-react-app; The typings for @types/react@17.x.x
are not released yet and need to address this issue. The WIP can be found here. I already created a note for this issue on the PR.
I actually don’t know whether this is an issue with the type definitions for react or react-scripts.
When upgrading to TypeScript 4.1.x I will get the following error:
Could not find a declaration file for module 'react/jsx-runtime'. '.../node_modules/react/jsx-runtime.js' implicitly has an 'any' type.
If the 'react' package actually exposes this module, consider sending a pull request to amend 'https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/react` TS7016
Adding the following to the react-app-env.d.ts
resolved the issue, but feels hacky.
declare module "react/jsx-runtime" {
export default any;
}
Would appreciate any information on whether that is an issue that needs to be solved within create-react-app or something that needs to be addressed by the type definitions.
Here the tsconfig.json for reference:
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": [
"src"
]
}
The behavior can be observed in this or build: https://github.com/n1ru4l/obs-character-info/pull/17
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 30
- Comments: 20 (4 by maintainers)
Links to this issue
Commits related to this issue
- Patch react-scripts and change jsx typescript option The bug was introduced in the most recent create-react-app: https://github.com/facebook/create-react-app/issues/10109 The typescript version needs... — committed to JanBussieck/tiliter-file-picker by JanBussieck 4 years ago
- redux-from-scratch: Install typescript https://github.com/facebook/create-react-app/issues/10109 — committed to bencerf/react-learning by deleted user 3 years ago
- redux-from-scratch: Install typescript https://github.com/facebook/create-react-app/issues/10109 — committed to bencerf/react-learning by bencerf 3 years ago
FWIW, ran into this issue after a Dependabot version bump to TypeScript 4.1.3 and
yarn add @types/react --dev
was enough to resolve the compilation error.Solve with> rm -rf node-modules, rm -rf package-lock.json, yarn install, and yarn add @types/react --dev
A search for this error, lands at this specific page. So, I am going to put what worked for me here, for those who come later. I understand there is a lot of linking, pointing out that and this. For me, I just want my students to get started with running react, without having to go through ten different links.
I am sure, this is not ‘the’ solution, but ‘a’ solution for someone who just wants to create a simple hello world react app with typescript. The search engines have already indexed this page as the top hit for this error, so yes. putting it here.
[exceprt from my developer work diary]
Okay, let’s try that again.
Manually changing files from .js to .tsx (for files with JSX content) and .ts (for files that dont use JSX)
Please install typescript by running npm install typescript. (error)
Now, I get the big error. Could not find a declaration file for module ‘react/jsx-runtime’. ‘/node_modules/react/jsx-runtime.js’ implicitly has an ‘any’ type.
Next, error. Could not find a declaration file for module ‘react-dom’. ‘/node_modules/react-dom/index.js’ implicitly has an ‘any’ type.
Next, error. TypeScript error in /src/index.tsx(17,1): Expected 1 arguments, but got 0.
solution: comment out // reportWebVitals(); or actually send something, like this.
Next, error.
add the above import to reportWebVitals.ts.
And, finally, it is done.
After deleting and re-installing your node modules, if the error persists and you’re using VS Code, make sure to run
TypeScript: Restart TS Server
in the Command PaletteI’m getting this issue outside of CRA with Typescript 4.1.2
still broken in stock CRA with typescript… solved with
yarn add @types/react --dev
ornpm i --save-dev @types/react
Fixed here in this version
Reboot my computer worked for me (not a joke), I use VSCode.
This worked for me!
npm i -D @types/react worked for me
“yarn add @types/react --dev” did the trick
export default
must be exist in component file.I noted this on the PR for updating
@types/react
forreact@17.x.x
over here: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/48971#pullrequestreview-535291028Gonna close this as it is not an issue related to create-react-app.
For reference:
https://github.com/facebook/react/blob/89d4fe141a28b54807901126063249cab6483f89/packages/react/jsx-runtime.js#L1-L9
It seems like this must be addressed in the @types/react package.
This got now addressed in https://github.com/DefinitelyTyped/DefinitelyTyped/pull/49701