6***@qq.com
6***@qq.com
  • 发布:2026-03-23 18:33
  • 更新:2026-03-23 18:33
  • 阅读:17

将vue2升级到vue3时提示Uncaught ReferenceError: exports is not defined

分类:uni-app

reportJSException >>>> exception function:createInstanceContext, exception:white screen cause create instanceContext failed,check js stack ->Uncaught ReferenceError: exports is not defined
at (app-service.js:9841:25)
at (app-service.js:137665:3)
报错很简略,没有提哪个依赖或者哪个文件报的错,我该怎么定位这个错误

2026-03-23 18:33 负责人:无 分享
已邀请:
Ask小助手

Ask小助手 - Ask社区AI助手

欢迎到专业群( uni-app 官方技术交流群 1 ) 咨询,群中有相关专业的管理员和群友。

这个错误是因为 Vue 3 只支持 ES6 模块规范,不再支持 CommonJS 的 module.exportsexports 语法

错误原因

从 Vue 2 升级到 Vue 3 时,如果代码或第三方库中使用了 CommonJS 的导出方式:

// Vue 2 中的 CommonJS 写法(Vue 3 不支持)  
module.exports.X = X;  
// 或  
exports.X = X;

在 Vue 3 环境下会报 exports is not defined 错误,因为 Vue 3 要求使用 ES6 模块语法:

// Vue 3 支持的 ES6 模块写法  
export default { X };  
// 或  
export { X };

定位方法

  1. 全局搜索:在 HBuilderX 或 VSCode 中全局搜索 module.exportsexports 关键字,检查项目中的 js 文件(特别是工具函数、配置文件、第三方库)

  2. 检查第三方依赖:某些旧版本的 npm 包可能使用 CommonJS 导出,需要升级这些依赖包或寻找替代方案

  3. 查看报错堆栈:虽然报错信息简略,但 app-service.js:9841:25 这种行号信息可以通过 sourcemap 映射回原始文件,检查构建后的 sourcemap 或尝试在开发模式下运行以获取更详细的错误位置

解决方案

将项目中的所有 CommonJS 模块语法改为 ES6 模块语法:

// 修改前(Vue 2)  
module.exports = {  
  install(Vue) {  
    // ...  
  }  
}  

// 修改后(Vue 3)  
export default {  
  install(app) {  
    // ...  
  }  
}

详细迁移文档请参考:Vue 2 迁移到 Vue 3 指南 - 模块导出

内容为 AI 生成,仅供参考

要回复问题请先登录注册