找不到模块:错误:可以解决react-dom/client'
我正在使用以下软件包使用React:
{
"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"
]
}
}
我的index.js
看起来如下:
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();
我的app.js
如下:
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;
当我使用运行我的应用程序时npm运行启动
我会收到以下错误:
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'
我重新安装了所有软件包,并且还列出了npm list 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]
为什么我遇到问题来编译应用程序的任何建议?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
的版本是17.0.2),则索引。
总而言之,如果您
使用的是React 17(我 问题,当我将React 18项目降级为React 17项目时。
我
用于打字稿项目,
In summary, if you are using React 17 (My version was 17.0.2), you index.js will have to be like the following,
if You are using React 18, your index.js file should looks like the following
I was facing problem, when I downgraded my react 18 project to react 17 project.
i used,
For typescript project,
好的!首先,这就是我所做的。我在React ^16.14.0上遇到了这个问题。我从index.js文件中的“ react-dom/client”中删除了“ client”。我还评论了
那里并使用了
。另外,
也发表了评论。执行此操作后,您的代码应该成功编译。**
Ok! Firstly this is what I did. I experienced this problem on React ^16.14.0. I removed the 'client' from 'import ReactDOM from "react-dom/client" in my index.js file. I also commented out the
that was there and used
instead. Also, commented out
also. After doing this your code should compile successfully.**
对于React版本18.1(带打字稿),
这对我有用
For react version 18.1 (with typescript),
this works for me
解决此问题的简单解决方案是首先更新您的React版本。更改React 18的index.js
。
安装了react 18后,如果您仍面临错误,则必须将index.js文件更改为如下所述:
A simple solution to fix this would be to first update your react version. Change your index.js for React 18.
Open your terminal in your project's root directory and run the following command:
After installed React 18, if you are still facing an error, you will have to change your index.js file to the as mentioned below:
我也有类似的问题,我通过
在tsconfig.json上添加来解决它
I had a similar problem and I solved it by adding
on the tsconfig.json
我的问题是类型软件包也没有升级。我必须更新新的
react> react
和react-dom
软件包的@Type导入。My issue was the types packages not being upgraded as well. I had to update the @type imports for the new
react
andreact-dom
packages.当我使用
笔记:
我忘记运行
npm install
,React版本仍然旧(16.14.0)。因此,在使用命令升级包版本之后,
我们仍然需要运行
npm install
以更新node_modules
中的已安装软件包。您可以使用
npm ls react -depth = 0
检查已安装的React的版本I met this error when I use npm-check-updates to upgrade React version.
Note:
I forget to run
npm install
, the React version is still old (16.14.0).So, after upgrading the package version using commands like:
We still need to run
npm install
to update the installed packages innode_modules
.You can use
npm ls react --depth=0
to check the installed React's version对我有用的最终解决方案只是将React 18
index.js
文件更改为以下内容:The final solution that worked for me was simply to change the React 18
index.js
file to the following:您仍然在依赖项中拥有旧版本的“ react”和“ react-dom”。
要解决它:
npm npm install
这应该解决您的问题并将您的应用更新为React18
You still have the older versions of "react" and "react-dom" in your dependencies.
To solve it:
npm install
This should solve your issue and update your app to react18
您可能需要降级react和react-dom,以下是
index.js
:React 17示例:
React 18示例:
You might need to downgrade React and react-dom, here is
index.js
:React 17 example:
React 18 example: