How To Fix Module Not Found: Can’t Resolve ‘React-dom’

Module not found: Can't resolve 'react-dom'

If you don’t know how to fix the error “Module not found: Can’t resolve ‘react-dom’“. It took you an hour to find a way to fix it. This article has the solution for you.

The reason for the error “Module not found: Can’t resolve ‘react-dom’ 

This error occurs when you have not installed the react-dom library or imported it incorrectly, like in the example below:

Code:

import React from 'react';
import ReactDOM from 'react-dom/client';
const root = ReactDOM.createRoot(document.getElementById('root'));

Output:

ERROR in ./public/src/index.js Module not found: Error: Cannot resolve module 'react-dom' 

The IDE is showing an error in the terminal probably because your program is suffering from one of the installation errors I mentioned above. Follow the next part of the article to be able to fix this error.

How to fix this error?

Install react-dom

You can use the react-dom package’s DOM-specific methods at the top level of your app as an escape route from the React model if you ever need to.

React’s DOM and server renderers can be accessed through this package. It is intended to be used with the standard React package, which comes with npm as react.

Because the error “Module not found: Can’t resolve ‘react-dom'” in React may be caused by your IDE not finding the library in the program. You need to install this library to be able to use the Module inside.

Install with npm:

npm i react-dom

Install with yarn:

yarn add react-dom 

So you have installed the react-dom library in your program and can import its internal methods and use it directly.

Check ‘dependencies’ in ‘package.json’ file

This way, you will check if the ‘dependencies’ in the ‘package.json’ file of the program contain the react-dom library. Your package.json file should look like the below:

Code:

 "dependencies": {
     "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-router-dom": "^6.4.2",
}

You can see that react-dom is 18.2.0 at the moment, and the program detected the installed library. If your “dependencies” don’t look like that because your IDE is where you have the library installed, you can manually add it. I hope you find these strategies helpful.

Summary

We have just shown you how to fix the error “Module not found: Can’t resolve ‘react-dom’“. You can fix it by Installing react-dom or checking ‘dependencies’ in the ‘package.json’ file. Let’s try two methods. Good luck for you!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *