gpt4 book ai didi

javascript - 找不到模块 : Error: Can't resolve 'react-dom/client'

转载 作者:行者123 更新时间:2023-12-05 00:24:39 25 4
gpt4 key购买 nike

我正在使用对以下软件包使用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 run start 运行我的应用程序时我收到以下错误:
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 中列出:
>  npm list
demo_app@0.1.0 /home/Desktop/Code/demo_app
├── @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-dom@17.0.1
├── react-leaflet@3.0.2
├── react-scripts@5.0.1
├── react@17.0.1
└── web-vitals@2.1.4
任何建议为什么我在编译我的应用程序时遇到问题?

最佳答案

对我有用的最终解决方案是简单地更改 React 18 index.js文件到以下内容:

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();

关于javascript - 找不到模块 : Error: Can't resolve 'react-dom/client' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71913692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com