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)

Commits related to this issue

Most upvoted comments

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.

npx create-react-app try-react -–template typescript

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)

npm install typescript

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.

npm i --save-dev @types/react

Next, error. Could not find a declaration file for module ‘react-dom’. ‘/node_modules/react-dom/index.js’ implicitly has an ‘any’ type.

npm i --save-dev @types/react-dom

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.

reportWebVitals(console.log)

Next, error.

import { ReportHandler } from 'web-vitals';

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 Palette

I’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 or npm i --save-dev @types/react

Captura

Fixed here in this version

Reboot my computer worked for me (not a joke), I use VSCode.

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 Palette

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 for react@17.x.x over here: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/48971#pullrequestreview-535291028

Gonna close this as it is not an issue related to create-react-app.