简体   繁体   English

找不到模块:错误:无法解析“react-dom/client”

[英]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:我的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();

My App.js is like the following:我的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;

When I run my app with npm run start I get the following error:当我使用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'

I reinstalled all packages and its also listed in npm list :我重新安装了所有软件包,它也列在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

Any suggestions why I have problems compiling my application?任何建议为什么我在编译我的应用程序时遇到问题?

The final solution that worked for me was simply to change the React 18 index.js file to the following:对我有用的最终解决方案是将 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();

You still have the older versions of "react" and "react-dom" in your dependencies.您的依赖项中仍然有旧版本的“react”和“react-dom”。 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删除“node_modules”文件夹
  2. do npm install This should solve your issue and update your app to react18执行npm install这应该可以解决您的问题并将您的应用程序更新为 react18

You might need to downgrade React and react-dom, here is index.js :你可能需要降级 React 和 react-dom,这里是index.js

React 17 example:反应 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 example:反应 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>
);

In summary, if you are using React 17 (My version was 17.0.2), you index.js will have to be like the following,总之,如果你使用 React 17(我的版本是 17.0.2),你的 index.js 必须像下面这样,

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如果您使用的是 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>
);

I was facing problem, when I downgraded my react 18 project to react 17 project.当我将 React 18 项目降级为 React 17 项目时,我遇到了问题。 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

I had a similar problem and I solved it by adding我有一个类似的问题,我通过添加解决了它

 "noImplicitAny": false

on the tsconfig.json在 tsconfig.json

For react version 18.1 (with typescript), this works for me对于反应版本 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();

If you're from here from react-router 's tutorial on stackblitz.com, you can do the following:如果您来自react-router关于 stackblitz.com 的教程,您可以执行以下操作:

  1. Remove package-lock.json删除package-lock.json
  2. Update package.json (change 17 to 18):更新 package.json(将 17 更改为 18):
{
  "name": "tutorial",
  "private": true,
  "scripts": {
    "dev": "vite",
    "build": "tsc && vite build",
    "serve": "vite preview"
  },
  "dependencies": {
    "history": "^5.2.0",
    "react": "^18.0.0",
    "react-dom": "^18.0.0",
    "react-router-dom": "6.0.0-beta.8"
  },
  "devDependencies": {
    "@rollup/plugin-replace": "^2.2.1",
    "@types/node": "14.x",
    "@types/react": "^18.0.0",
    "@types/react-dom": "^18.0.0",
    "@vitejs/plugin-react": "^1.0.1",
    "typescript": "^4.3.5",
    "vite": "^2.5.0"
  }
}
  1. npm install

The problem is React version mismatch.问题是 React 版本不匹配。 React-router tutorial uses React v18+, while the template on stackblitz.com is configured to use React v17 React-router 教程使用 React v18+,而stackblitz.com 上的模板配置为使用 React v17

Ok.好的。 Firstly this is what I did.首先,这就是我所做的。 I experienced this problem on React ^16.14.0.我在 React ^16.14.0 上遇到了这个问题。 I removed the 'client' from 'import ReactDOM from "react-dom/client" in my index.js file.我从 index.js 文件中的“react-dom/client”中的“import ReactDOM”中删除了“client”。 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.**完成此操作后,您的代码应该可以成功编译。**

A simple solution to fix this would be to first update your react version.解决此问题的一个简单解决方案是首先更新您的反应版本。 Change your index.js for React 18.为 React 18 更改你的 index.js。

Open your terminal in your project's root directory and run the following command:在项目的根目录中打开终端并运行以下命令:

npm install react-dom@latest react@latest npm 安装 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:安装 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> );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM