You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

学习React Router 6遇模块解析错误,寻求解决方案

React Router 6模块解析错误的解决方法

问题根源

该错误是因为当前项目的构建工具(如Webpack或Create React App)版本过低,无法解析@remix-run/router(React Router 6的核心依赖库)中使用的ES2020+新语法,导致模块解析失败。

解决步骤

  • 升级项目构建工具版本
    若使用Create React App,需确保react-scripts版本≥4.0.0,低版本不支持新ES特性。执行以下命令升级:
    npm install react-scripts@latest
    # Yarn用户执行:
    yarn add react-scripts@latest
    
  • 重新安装匹配版本的React Router
    确保安装的是稳定版React Router 6,先卸载再重新安装:
    npm uninstall react-router-dom
    npm install react-router-dom@6
    
  • 清理依赖缓存并重装依赖
    删除旧依赖目录和锁文件,避免缓存冲突:
    rm -rf node_modules package-lock.json
    npm install
    
  • 清理冗余代码
    你的代码中导入了未使用的Router组件,可删除该冗余导入,修正后的导入语句:
    import { BrowserRouter, Routes, Route } from 'react-router-dom';
    

验证修复

重启开发服务器:

npm start

访问/home路径,若能正常显示对应内容,则问题已解决。

内容的提问来源于stack exchange,提问作者SUNNY

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 02:42:50