Could not find a declaration file for module 'react-dom/client'
Asked Answered
S

5

25

I have following error when use react-dom/client

Compiled with problems:X

ERROR in src/index.tsx:2:28

TS7016: Could not find a declaration file for module 'react-dom/client'. 'C:/Users/Mohamad/Desktop/HIS/Administration/Administration/Administration/Administration.Endpoints.WebUI/ClientApp/node_modules/react-dom/client.js' implicitly has an 'any' type.
  Try `npm install @types/react-dom` if it exists or add a new declaration (.d.ts) file containing `declare module 'react-dom/client';`
    1 | import * as React from 'react';
  > 2 | import { createRoot } from 'react-dom/client';
      |                            ^^^^^^^^^^^^^^^^^^
    3 | import App from './App';
    4 |
    5 |
Superiority answered 23/9, 2022 at 11:18 Comment(5)
Have you installed @types/react-dom as it suggests?Vivanvivarium
Getting same error even after installing typesRevulsion
Did you find any solution? I'm running into this problem as well.Augmenter
stackoverflow.com/a/71809050Cutright
Actually, better to do this github.com/DefinitelyTyped/DefinitelyTyped/issues/…Cutright
J
37

Install @types/react-dom with @latest

 npm i @types/react-dom@latest
Jonasjonathan answered 18/11, 2022 at 17:37 Comment(2)
That worked for me, although I'd like to suggest installing types as a dev-dependency only, such as: npm i -D @types/react-dom@latestDemonstrate
Types should be installed as devDependencies. yarn add -D @types/react-domMatriarchate
C
9

Error message actually states the solution:

You need to install the package or define in .d.ts file

However for my case, I had to do both of them to resolve the issue.

Install the package:

npm install @types/react-dom

Define in .d.ts

declare module 'react-dom/client';
Countertenor answered 27/1, 2023 at 14:44 Comment(1)
Briliiant! Adding module declaration on my react-app-env.d.ts file help me fix my issue.Sweatband
O
1

Just to add to Sabri and David answer, when using yarn you can run

yarn add @types/react-dom

That works for yarn and worked for me

Overlay answered 13/12, 2023 at 17:14 Comment(0)
U
0

As the error trace suggests you need to install an additional package. If you keep getting the same error you should post your package.jsonhere.

Even though a similar answer was provided but I'd suggest installing the package with the --save-dev flag.

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

Unstained answered 30/12, 2023 at 19:19 Comment(0)
H
-2

Instead of ratholing on that, I added the following statement to the tsconfig.json file to disable the ‘any’ type errors:

"noImplicitAny": false

Hoodmanblind answered 7/6, 2023 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.