'Module not found: Error: Can't resolve 'react-dom/client'
I am using react with the following packages:
{
"name": "demo",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.0.1",
"@testing-library/user-event": "^13.5.0",
"h3-js": "^3.7.2",
"leaflet": "^1.7.1",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-leaflet": "3.0.2",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
My index.js
looks like the following:
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
reportWebVitals();
My App.js
is like the following:
import React from "react";
import { render } from "react-dom";
import LeafletMap from "./Map";
class App extends React.Component {
state = { resolution: 8, kRing: 0 };
constructor(props) {
super(props);
this.state = { resolution: 8, kRing: 0 };
}
render() {
return (
<div>
<LeafletMap
resolution={this.state.resolution}
kRing={this.state.kRing}
/>
Resolution:
<input
type="number"
min={0}
max={15}
onChange={this.onChangeResolution}
defaultValue={8}
/>
<br />
K Rings:
<input
type="number"
min={0}
max={100}
onChange={this.onChangeKRings}
defaultValue={0}
/>
</div>
);
}
onChangeResolution = (e) => {
this.setState({ resolution: Number.parseInt(e.target.value) });
};
onChangeKRings = (e) => {
this.setState({ kRing: Number.parseInt(e.target.value) });
};
}
export default App;
When I run my app with npm run start
I get the following error:
Compiled with problems:X
ERROR in ./src/index.js 5:0-40
Module not found: Error: Can't resolve 'react-dom/client' in '/home/Desktop/Code/demo_app/src'
I reinstalled all packages and its also listed in npm list
:
> npm list
[email protected] /home/Desktop/Code/demo_app
├── @testing-library/[email protected]
├── @testing-library/[email protected]
├── @testing-library/[email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]
Any suggestions why I have problems compiling my application?
Solution 1:[1]
The final solution that worked for me was simply to change the React 18 index.js
file to the following:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
reportWebVitals();
Solution 2:[2]
You still have the older versions of "react" and "react-dom" in your dependencies. To solve it:
- delete these two lines:
"react": "^17.0.1",
"react-dom": "^17.0.1",
- Replace them with:
"react": "^18.1.0",
"react-dom": "^18.1.0",
- delete the "node_modules" folder
- do
npm install
This should solve your issue and update your app to react18
Solution 3:[3]
I had a similar problem and I solved it by adding
"noImplicitAny": false
on the tsconfig.json
Solution 4:[4]
You might need to downgrade React and react-dom, here is index.js
:
React 17 example:
import React from "react";
import { render } from "react-dom";
import "./index.css";
import App from "./App";
const root = document.getElementById("root");
render(<App />, root);
React 18 example:
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
Solution 5:[5]
For react version 18.1 (with typescript), this works for me
import React from "react";
import { createRoot } from "react-dom/client";
import reportWebVitals from "./reportWebVitals";
import App from "./App";
const root = createRoot(
document.getElementById("root") as HTMLElement
);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
reportWebVitals();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | jonrsharpe |
Solution 2 | Boshra Jaber |
Solution 3 | Asabeneh |
Solution 4 | Giorgi Digmelashvili |
Solution 5 | Bhanu Sharma |