找不到模块:错误:可以解决react-dom/client'

发布于 2025-01-22 02:57:00 字数 4418 浏览 2 评论 0原文

我正在使用以下软件包使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

疯到世界奔溃 2025-01-29 02:57:01

的版本是17.0.2),则索引。

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
 document.getElementById('root')
);

总而言之,如果您

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

使用的是React 17(我 问题,当我将React 18项目降级为React 17项目时。

npm install react@17 react-dom@17

用于打字稿项目,

npm install react@17 @types/react@17 react-dom@17 @types/react-dom@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,

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
 document.getElementById('root')
);

if You are using React 18, your index.js file should looks like the following

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

I was facing problem, when I downgraded my react 18 project to react 17 project.
i used,

npm install react@17 react-dom@17

For typescript project,

npm install react@17 @types/react@17 react-dom@17 @types/react-dom@17
苯莒 2025-01-29 02:57:01

好的!首先,这就是我所做的。我在React ^16.14.0上遇到了这个问题。我从index.js文件中的“ react-dom/client”中删除了“ client”。我还评论了

    const root = ReactDOM.createRoot(document.getElementById("root"));
    root.render(
    <React.StrictMode>
    <App />
    </React.StrictMode>
    );

那里并使用了

  ReactDOM.render(
    <App />,
  document.getElementById("root")
);

。另外,

import reportWebVitals from "./reportWebVitals"; and reportWebVitals();

也发表了评论。执行此操作后,您的代码应该成功编译。**

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

    const root = ReactDOM.createRoot(document.getElementById("root"));
    root.render(
    <React.StrictMode>
    <App />
    </React.StrictMode>
    );

that was there and used

  ReactDOM.render(
    <App />,
  document.getElementById("root")
);

instead. Also, commented out

import reportWebVitals from "./reportWebVitals"; and reportWebVitals();

also. After doing this your code should compile successfully.**

疾风者 2025-01-29 02:57:01

对于React版本18.1(带打字稿),
这对我有用

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

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();
一瞬间的火花 2025-01-29 02:57:01

解决此问题的简单解决方案是首先更新您的React版本。更改React 18的index.js

npm安装react-dom@最新react@最新

安装了react 18后,如果您仍面临错误,则必须将index.js文件更改为如下所述:

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

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:

npm install react-dom@latest react@latest

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:

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

困倦 2025-01-29 02:57:01
ReactDOM.render is no longer supported in React 18. Use createRoot instead. 
Until you switch to the new API, your app will behave as if it’s running React 
17.
LINK :https://reactjs.org/link/switch-to-createroot


// Before
import { render } from 'react-dom';
const container = document.getElementById('app');
render(<App tab="home" />, container);

// After
import { createRoot } from 'react-dom/client';
const container = document.getElementById('app');
const root = createRoot(container); 
root.render(<App tab="home" />);

upgrade react-dom to 18.2.0 using **npm i [email protected]**
.
.
.
If this don't work make sure react and react-dom packages should be downloaded 
as same version for ex:   "react": "^18.2.0",
                          "react-dom":"^18.2.0",
                                     or
                          "react": "^17.2.0",
                          "react-dom":"^17.2.0".
ReactDOM.render is no longer supported in React 18. Use createRoot instead. 
Until you switch to the new API, your app will behave as if it’s running React 
17.
LINK :https://reactjs.org/link/switch-to-createroot


// Before
import { render } from 'react-dom';
const container = document.getElementById('app');
render(<App tab="home" />, container);

// After
import { createRoot } from 'react-dom/client';
const container = document.getElementById('app');
const root = createRoot(container); 
root.render(<App tab="home" />);

upgrade react-dom to 18.2.0 using **npm i [email protected]**
.
.
.
If this don't work make sure react and react-dom packages should be downloaded 
as same version for ex:   "react": "^18.2.0",
                          "react-dom":"^18.2.0",
                                     or
                          "react": "^17.2.0",
                          "react-dom":"^17.2.0".
影子是时光的心 2025-01-29 02:57:01

我也有类似的问题,我通过

 "noImplicitAny": false

在tsconfig.json上添加来解决它

I had a similar problem and I solved it by adding

 "noImplicitAny": false

on the tsconfig.json

油饼 2025-01-29 02:57:01

我的问题是类型软件包也没有升级。我必须更新新的react> reactreact-dom软件包的@Type导入。

yarn add @types/react @types/react-dom

My issue was the types packages not being upgraded as well. I had to update the @type imports for the new react and react-dom packages.

yarn add @types/react @types/react-dom
暗藏城府 2025-01-29 02:57:01

当我使用

笔记:

仅修改package.json文件。运行npm install以更新您的已安装软件包和软件包 - 洛克.json。

我忘记运行npm install,React版本仍然旧(16.14.0)。

因此,在使用命令升级包版本之后,

$ ncu -f react -u

我们仍然需要运行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:

only modifies package.json file. Run npm install to update your installed packages and package-lock.json.

I forget to run npm install, the React version is still old (16.14.0).

So, after upgrading the package version using commands like:

$ ncu -f react -u

We still need to run npm install to update the installed packages in node_modules.

You can use npm ls react --depth=0 to check the installed React's version

写下不归期 2025-01-29 02:57:00

对我有用的最终解决方案只是将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();

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

谜泪 2025-01-29 02:57:00

您仍然在依赖项中拥有旧版本的“ react”和“ react-dom”。
要解决它:

  1. 删除这两行:
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
  1. 用:删除
    "react": "^18.1.0",
    "react-dom": "^18.1.0",
  1. “ node_modules”文件夹
  2. do npm npm install
    这应该解决您的问题并将您的应用更新为React18

You still have the older versions of "react" and "react-dom" in your dependencies.
To solve it:

  1. delete these two lines:
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
  1. Replace them with:
    "react": "^18.1.0",
    "react-dom": "^18.1.0",
  1. delete the "node_modules" folder
  2. do npm install
    This should solve your issue and update your app to react18
┈┾☆殇 2025-01-29 02:57:00

您可能需要降级react和react-dom,以下是index.js

React 17示例:
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示例:
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>
);

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>
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文