React项目安装react-router依赖失败,peer依赖缺失求助
Hey there! Those warnings are telling you that the packages you're trying to install (like react-router) need specific versions of React to work, but your project doesn't have React installed yet. Let's fix this step by step:
Step 1: Install React and ReactDOM
First, we need to add React and ReactDOM to your project—these are required peer dependencies for react-router and the other packages throwing warnings. We'll pick a version that's compatible with all the required packages (React 16.x works perfectly here):
npm install react@^16.0.0 react-dom@^16.0.0 --save
Step 2: Reinstall React Router (or use React Router DOM)
Since you're building a website for in-page navigation, I'd actually recommend using react-router-dom instead of just react-router—it's the browser-specific implementation that includes components like BrowserRouter, Route, and Link which you'll need for your homepage links. Run this command:
npm install react-router-dom@^5.0.0 --save
(If you specifically need the core react-router package, you can run npm install react-router@^5.0.0 --save instead, but react-router-dom is what most web projects use.)
Step 3: Verify the Installation
Check your package.json file to make sure react, react-dom, and react-router-dom (or react-router) are listed under the dependencies section. You should no longer see those peer dependency warnings when running install commands.
Quick Note on Peer Dependencies
Peer dependencies are packages that a library expects to be present in your project but doesn't install automatically—this is to avoid duplicate versions of large packages like React across your dependencies. The warnings you saw were just letting you know you needed to add those missing required packages yourself.
内容的提问来源于stack exchange,提问作者Frank Thijssen

