uniapp cannot find module 'vue-router\dist\vue-router.esm-bundler.js'如何解决?
在uniapp项目中运行时报错"cannot find module ‘vue-router\dist\vue-router.esm-bundler.js’",明明已经安装了vue-router却找不到模块。请问该如何解决这个依赖路径问题?需要配置什么特殊设置吗?
2 回复
检查uni-app项目是否误装了vue-router,uni-app自带路由,无需额外安装。卸载vue-router依赖即可解决。
在uni-app中出现 cannot find module 'vue-router\dist\vue-router.esm-bundler.js' 错误,通常有以下几种解决方案:
解决方案
1. 检查并安装依赖
npm install vue-router@next
# 或
yarn add vue-router@next
2. 检查uni-app版本兼容性
uni-app默认使用内置路由系统,如果确实需要使用vue-router,请确保:
- 使用Vue 3版本的uni-app(HBuilderX 3.2.0+)
- 在
manifest.json中配置使用Vue 3
3. 检查导入路径
确保导入语句正确:
// 正确方式
import { createRouter, createMemoryHistory } from 'vue-router'
4. 清理缓存并重新安装
# 删除node_modules和package-lock.json
rm -rf node_modules package-lock.json
# 重新安装
npm install
5. 检查构建配置
在vite.config.js或vue.config.js中确保正确配置:
export default {
resolve: {
alias: {
'vue-router': 'vue-router/dist/vue-router.esm-bundler.js'
}
}
}
重要提醒
uni-app强烈建议使用内置路由系统,而不是vue-router,因为:
- uni-app有自己完善的路由管理
- 使用vue-router可能导致页面生命周期和导航行为异常
- 小程序端不支持vue-router
如果必须使用vue-router,请确保项目是基于Vue 3的uni-app,并做好兼容性测试。

