mingyong.g@foxmail.com
mingyong.g@foxmail.com
  • 发布:2021-02-23 17:07
  • 更新:5 天前
  • 阅读:529

【报Bug】使用cli方式创建的项目本地 运行 报exports is not defined

分类:uni-app

产品分类: uniapp/H5

PC开发环境操作系统: Windows

PC开发环境操作系统版本号: win 10

HBuilderX类型: 正式

浏览器平台: Chrome

浏览器版本: 和浏览器版本无关

项目创建方式: CLI

CLI版本号: 2.0.0-31220210205004

示例代码:

// package.json文件

{
"name": "my_tools",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "npm run dev:h5",
"build": "npm run build:h5",
"build:app-plus": "cross-env NODE_ENV=production UNI_PLATFORM=app-plus vue-cli-service uni-build",
"build:custom": "cross-env NODE_ENV=production uniapp-cli custom",
"build:h5": "cross-env NODE_ENV=production UNI_PLATFORM=h5 vue-cli-service uni-build",
"build:mp-360": "cross-env NODE_ENV=production UNI_PLATFORM=mp-360 vue-cli-service uni-build",
"build:mp-alipay": "cross-env NODE_ENV=production UNI_PLATFORM=mp-alipay vue-cli-service uni-build",
"build:mp-baidu": "cross-env NODE_ENV=production UNI_PLATFORM=mp-baidu vue-cli-service uni-build",
"build:mp-kuaishou": "cross-env NODE_ENV=production UNI_PLATFORM=mp-kuaishou vue-cli-service uni-build",
"build:mp-qq": "cross-env NODE_ENV=production UNI_PLATFORM=mp-qq vue-cli-service uni-build",
"build:mp-toutiao": "cross-env NODE_ENV=production UNI_PLATFORM=mp-toutiao vue-cli-service uni-build",
"build:mp-weixin": "cross-env NODE_ENV=production UNI_PLATFORM=mp-weixin vue-cli-service uni-build",
"build:quickapp-native": "cross-env NODE_ENV=production UNI_PLATFORM=quickapp-native vue-cli-service uni-build",
"build:quickapp-webview": "cross-env NODE_ENV=production UNI_PLATFORM=quickapp-webview vue-cli-service uni-build",
"build:quickapp-webview-huawei": "cross-env NODE_ENV=production UNI_PLATFORM=quickapp-webview-huawei vue-cli-service uni-build",
"build:quickapp-webview-union": "cross-env NODE_ENV=production UNI_PLATFORM=quickapp-webview-union vue-cli-service uni-build",
"dev:app-plus": "cross-env NODE_ENV=development UNI_PLATFORM=app-plus vue-cli-service uni-build --watch",
"dev:custom": "cross-env NODE_ENV=development uniapp-cli custom",
"dev:h5": "cross-env NODE_ENV=development UNI_PLATFORM=h5 vue-cli-service uni-serve",
"dev:mp-360": "cross-env NODE_ENV=development UNI_PLATFORM=mp-360 vue-cli-service uni-build --watch",
"dev:mp-alipay": "cross-env NODE_ENV=development UNI_PLATFORM=mp-alipay vue-cli-service uni-build --watch",
"dev:mp-baidu": "cross-env NODE_ENV=development UNI_PLATFORM=mp-baidu vue-cli-service uni-build --watch",
"dev:mp-kuaishou": "cross-env NODE_ENV=development UNI_PLATFORM=mp-kuaishou vue-cli-service uni-build --watch",
"dev:mp-qq": "cross-env NODE_ENV=development UNI_PLATFORM=mp-qq vue-cli-service uni-build --watch",
"dev:mp-toutiao": "cross-env NODE_ENV=development UNI_PLATFORM=mp-toutiao vue-cli-service uni-build --watch",
"dev:mp-weixin": "cross-env NODE_ENV=development UNI_PLATFORM=mp-weixin vue-cli-service uni-build --watch",
"dev:quickapp-native": "cross-env NODE_ENV=development UNI_PLATFORM=quickapp-native vue-cli-service uni-build --watch",
"dev:quickapp-webview": "cross-env NODE_ENV=development UNI_PLATFORM=quickapp-webview vue-cli-service uni-build --watch",
"dev:quickapp-webview-huawei": "cross-env NODE_ENV=development UNI_PLATFORM=quickapp-webview-huawei vue-cli-service uni-build --watch",
"dev:quickapp-webview-union": "cross-env NODE_ENV=development UNI_PLATFORM=quickapp-webview-union vue-cli-service uni-build --watch",
"info": "node node_modules/@dcloudio/vue-cli-plugin-uni/commands/info.js",
"serve:quickapp-native": "node node_modules/@dcloudio/uni-quickapp-native/bin/serve.js",
"test:android": "cross-env UNI_PLATFORM=app-plus UNI_OS_NAME=android jest -i",
"test:h5": "cross-env UNI_PLATFORM=h5 jest -i",
"test:ios": "cross-env UNI_PLATFORM=app-plus UNI_OS_NAME=ios jest -i",
"test:mp-baidu": "cross-env UNI_PLATFORM=mp-baidu jest -i",
"test:mp-weixin": "cross-env UNI_PLATFORM=mp-weixin jest -i"
},
"dependencies": {
"@dcloudio/uni-app-plus": "^2.0.0-31220210205004",
"@dcloudio/uni-h5": "^2.0.0-31220210205004",
"@dcloudio/uni-helper-json": "",
"@dcloudio/uni-mp-360": "^2.0.0-31220210205004",
"@dcloudio/uni-mp-alipay": "^2.0.0-31220210205004",
"@dcloudio/uni-mp-baidu": "^2.0.0-31220210205004",
"@dcloudio/uni-mp-qq": "^2.0.0-31220210205004",
"@dcloudio/uni-mp-toutiao": "^2.0.0-31220210205004",
"@dcloudio/uni-mp-vue": "^2.0.0-31220210205004",
"@dcloudio/uni-mp-weixin": "^2.0.0-31220210205004",
"@dcloudio/uni-quickapp-native": "^2.0.0-31220210205004",
"@dcloudio/uni-quickapp-webview": "^2.0.0-31220210205004",
"@dcloudio/uni-stat": "^2.0.0-31220210205004",
"@vue/shared": "^3.0.0",
"core-js": "^3.6.5",
"flyio": "^0.6.2",
"regenerator-runtime": "^0.12.1",
"vue": "^2.6.11",
"vuex": "^3.2.0"
},
"devDependencies": {
"@dcloudio/types": "
",
"@dcloudio/uni-automator": "^2.0.0-31220210205004",
"@dcloudio/uni-cli-shared": "^2.0.0-31220210205004",
"@dcloudio/uni-migration": "^2.0.0-31220210205004",
"@dcloudio/uni-template-compiler": "^2.0.0-31220210205004",
"@dcloudio/vue-cli-plugin-hbuilderx": "^2.0.0-31220210205004",
"@dcloudio/vue-cli-plugin-uni": "^2.0.0-31220210205004",
"@dcloudio/vue-cli-plugin-uni-optimize": "^2.0.0-31220210205004",
"@dcloudio/webpack-uni-mp-loader": "^2.0.0-31220210205004",
"@dcloudio/webpack-uni-pages-loader": "^2.0.0-31220210205004",
"@vue/cli-plugin-babel": "~4.5.0",
"@vue/cli-service": "~4.5.0",
"babel-plugin-import": "^1.11.0",
"cross-env": "^7.0.2",
"jest": "^25.4.0",
"mini-types": "",
"miniprogram-api-typings": "
",
"postcss-comment": "^2.0.0",
"vue-template-compiler": "^2.6.11"
},
"browserslist": [
"Android >= 4",
"ios >= 8"
],
"uni-app": {
"scripts": {}
}
}

操作步骤:

通过 最新版的cli创建uni-app项目,选择简单模板(应该和模板类型没关系)

// 初步定位到将babel.config.js中的代码修改后错误消失
module.exports = {
presets: [
[
'@vue/app',
{
// modules: 'commonjs', 注释掉该条语句,错误消失。
useBuiltIns: process.env.UNI_PLATFORM === 'h5' ? 'usage' : 'entry'
}
]
],
plugins
}

预期结果:

报错exports is not defined
详细错误见上面错误描述

实际结果:

报错exports is not defined
详细错误见上面错误描述

bug描述:

// 通过 cli 方式创建的uni-app 项目(默认模板)本地运行浏览器控制台报错exports is not defined,详细说明如下:

chunk-vendors.js:58 Uncaught ReferenceError: exports is not defined
at Module../node_modules/@babel/runtime/helpers/objectSpread2/_index.mjs (chunk-vendors.js:58)
at webpack_require__ (index.js:854)
at fn (index.js:151)
at Object../node_modules/@babel/runtime/helpers/esm/objectSpread2.js (chunk-vendors.js:23)
at
webpack_require (index.js:854)
at fn (index.js:151)
at eval (main.js:5)
at Object../src/main.js (index.js:1192)
at
webpack_require (index.js:854)
at fn (index.js:151)
./node_modules/@babel/runtime/helpers/objectSpread2/_index.mjs @ chunk-vendors.js:58
webpack_require @ index.js:854
fn @ index.js:151
./node_modules/@babel/runtime/helpers/esm/objectSpread2.js @ chunk-vendors.js:23
__webpack_require
@ index.js:854
fn @ index.js:151
eval @ main.js:5
./src/main.js @ index.js:1192
webpack_require @ index.js:854
fn @ index.js:151
1 @ index.js:1370
webpack_require
@ index.js:854
checkDeferredModules @ index.js:46
(anonymous) @ index.js:994
(anonymous) @ index.js:997

// ----------------------------------------------------------------------
// 初步定位到将babel.config.js中的代码修改后错误消失
module.exports = {
presets: [
[
'@vue/app',
{
// modules: 'commonjs', 注释掉该条语句,错误消失。
useBuiltIns: process.env.UNI_PLATFORM === 'h5' ? 'usage' : 'entry'
}
]
],
plugins
}

2021-02-23 17:07 负责人:无 分享
已邀请:

最佳回复

DCloud_UNI_GSQ

DCloud_UNI_GSQ

问题原因:@babel/runtime 昨天更新到了 7.13.x,有较大改动,引起兼容问题

目前已经更新 preset,对已经安装了新版本 @babel/runtime 的用户提供以下两种解决方案:

一、回退 @babel/runtime 版本

删除 node_modules 目录和 package-lock.json 文件,在 package.json 文件中增加开发依赖 "@babel/runtime": "~7.12.0",执行 npm install 重新安装依赖即可

二、修改 babel 配置

打开 babel.config.js 文件,删除或注释掉 babel.config.js(如调整后部分模块编译异常请使用方案一)

  • 270315475@qq.com

    一、二 哪种是长久修复方案?比如后续升级都不影响的? 我现在是注释了babel.config.js,如果后续升级后,还需要打开吗

    5 天前

  • DCloud_UNI_GSQ

    回复 270315475@qq.com: 都可行,如果方案二现在未出现问题,便可直接使用。

    5 天前

  • 18306679493@163.com

    采用第二种方案:,注释掉modules: 'commonjs',

    1 天前

火车驶向梦中

火车驶向梦中 - wgg

好家伙,刚出的这问题

  • mingyong.g@foxmail.com (作者)

    // 按照下面的操作可以解决问题

    // 初步定位到将babel.config.js中的代码修改后错误消失

    module.exports = {

    presets: [

    [

    '@vue/app',

    {

    // modules: 'commonjs', 注释掉该条语句,错误消失。

    useBuiltIns: process.env.UNI_PLATFORM === 'h5' ? 'usage' : 'entry'

    }

    ]

    ],

    plugins

    }

    2021-02-23 18:06

250655604@qq.com

250655604@qq.com

我也不行 项目跑不起来了

  • mingyong.g@foxmail.com (作者)

    // 按照下面的操作可以解决问题

    // 初步定位到将babel.config.js中的代码修改后错误消失

    module.exports = {

    presets: [

    [

    '@vue/app',

    {

    // modules: 'commonjs', 注释掉该条语句,错误消失。

    useBuiltIns: process.env.UNI_PLATFORM === 'h5' ? 'usage' : 'entry'

    }

    ]

    ],

    plugins

    }

    2021-02-23 18:06

270315475@qq.com

270315475@qq.com - 接单-APP、小程序高性能接单,QQ:270315475、wx:jin_biao

卧槽@@@@@@@@@@@@@@@@@@@@@@@@@@@ 什么情况啊 莫名其妙的出现这个 官方解答下

mingyong.g@foxmail.com

mingyong.g@foxmail.com (作者) - hello word !

统一回复下,问题原因不详,但是按照下面得处理办法,可以修复


// 按照下面的操作可以解决问题  
// 初步定位到将babel.config.js中的代码修改后错误消失  
module.exports = {  
presets: [  
[  
'@vue/app',  
{  
// modules: 'commonjs', 注释掉该条语句,错误消失。  
useBuiltIns: process.env.UNI_PLATFORM === 'h5' ? 'usage' : 'entry'  
}  
]  
],  
plugins  
}
250655604@qq.com

250655604@qq.com

官方关注一下吧

DCloud_UNI_GSQ
  • 270315475@qq.com

    如果是bug 麻烦修复了在这里说一声,我现在是// modules: 'commonjs', 注释的该条语句

    6 天前

DCloud_UNI_GSQ

DCloud_UNI_GSQ

出现问题的项目是突然运行报错了吗,同一个项目之前是否成功运行过

  • dujun_h5@163.com

    是昨天突然运行后出的问题,打包成功,但是浏览报错,项目每天都会上测试环境,昨天就出问题了,但是通过hbuildx发行小程序并没有任何问题

    6 天前

  • DCloud_UNI_GSQ

    回复 dujun_h5@163.com: 是不是安装或者升级过 npm 依赖?

    6 天前

  • 270315475@qq.com

    回复 DCloud_UNI_GSQ: 没有安装 和升级 突然就这样了

    5 天前

DCloud_UNI_GSQ

DCloud_UNI_GSQ

使用 npm run info 打印一下依赖信息回复我

  • 270315475@qq.com

    > node node_modules/@dcloudio/vue-cli-plugin-uni/commands/info.js


    uni-app v3.0.4

    uni-app cli v2.0.0-alpha-30420201230001


    Environment Info:


    System:

    OS: macOS 10.14.6

    CPU: (8) x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz

    Binaries:

    Node: 10.15.3 - /usr/local/bin/node

    Yarn: 1.7.0 - /usr/local/bin/yarn

    npm: 6.14.10 - /usr/local/bin/npm

    Browsers:

    Chrome: 88.0.4324.192

    Firefox: 83.0

    Safari: 13.1

    npmPackages:

    @dcloudio/types: ^2.0.19 => 2.0.20

    @dcloudio/uni-app-plus: ^2.0.0-alpha-29920201203001 => 2.0.0-alpha-30420201230001

    @dcloudio/uni-app-plus-nvue: 0.0.1

    @dcloudio/uni-app-plus-nvue-v8: 0.0.1

    @dcloudio/uni-automator: ^2.0.0-alpha-29920201203001 => 2.0.0-alpha-30420201230001

    @dcloudio/uni-cli-shared: ^2.0.0-alpha-29920201203001 => 2.0.0-alpha-30420201230001

    @dcloudio/uni-h5: ^2.0.0-alpha-29920201203001 => 2.0.0-alpha-30420201230001

    @dcloudio/uni-helper-json: ^1.0.8 => 1.0.9

    @dcloudio/uni-migration: ^2.0.0-alpha-29920201203001 => 2.0.0-alpha-30420201230001

    @dcloudio/uni-mp-360: ^2.0.0-alpha-29920201203001 => 2.0.0-alpha-29920201212001

    @dcloudio/uni-mp-alipay: ^2.0.0-alpha-29920201203001 => 2.0.0-alpha-30420201230001

    @dcloudio/uni-mp-baidu: ^2.0.0-alpha-29920201203001 => 2.0.0-alpha-30420201230001

    @dcloudio/uni-mp-qq: ^2.0.0-alpha-29920201203001 => 2.0.0-alpha-30420201230001

    @dcloudio/uni-mp-toutiao: ^2.0.0-alpha-29920201203001 => 2.0.0-alpha-30420201230001

    @dcloudio/uni-mp-vue: ^2.0.0-alpha-29920201203001 => 2.0.0-alpha-30420201230001

    @dcloudio/uni-mp-weixin: ^2.0.0-alpha-29920201203001 => 2.0.0-alpha-30420201230001

    @dcloudio/uni-quickapp-native: ^2.0.0-alpha-29920201203001 => 2.0.0-alpha-30420201230001

    @dcloudio/uni-quickapp-webview: ^2.0.0-alpha-29920201203001 => 2.0.0-alpha-30420201230001

    @dcloudio/uni-stat: ^2.0.0-alpha-29220200930001 => 2.0.0-v3-24020191018001

    @dcloudio/uni-template-compiler: ^2.0.0-alpha-29920201203001 => 2.0.0-alpha-30420201230001

    @dcloudio/vue-cli-plugin-hbuilderx: ^2.0.0-alpha-29920201203001 => 2.0.0-alpha-30420201230001

    @dcloudio/vue-cli-plugin-uni: ^2.0.0-alpha-29920201203001 => 2.0.0-alpha-30420201230001

    @dcloudio/vue-cli-plugin-uni-optimize: ^2.0.0-alpha-29920201203001 => 2.0.0-alpha-30420201230001

    @dcloudio/webpack-uni-mp-loader: ^2.0.0-alpha-29920201203001 => 2.0.0-alpha-30420201230001

    @dcloudio/webpack-uni-nvue-loader: 0.0.1

    @dcloudio/webpack-uni-pages-loader: ^2.0.0-alpha-29920201203001 => 2.0.0-alpha-30420201230001

    @hap-toolkit/dsl-vue: 0.6.13

    @vue/babel-helper-vue-jsx-merge-props: 1.2.1

    @vue/babel-helper-vue-transform-on: 1.0.0

    @vue/babel-plugin-jsx: 1.0.0

    @vue/babel-plugin-transform-vue-jsx: 1.2.1

    @vue/babel-preset-app: 4.5.9

    @vue/babel-preset-jsx: 1.2.4

    @vue/babel-sugar-composition-api-inject-h: 1.2.1

    @vue/babel-sugar-composition-api-render-instance: 1.2.4

    @vue/babel-sugar-functional-vue: 1.2.2

    @vue/babel-sugar-inject-h: 1.2.2

    @vue/babel-sugar-v-model: 1.2.3

    @vue/babel-sugar-v-on: 1.2.3

    @vue/cli-overlay: 4.5.9

    @vue/cli-plugin-babel: ^4.5.8 => 4.5.9

    @vue/cli-plugin-router: 4.5.9

    @vue/cli-plugin-vuex: 4.5.9

    @vue/cli-service: ^4.5.8 => 4.5.9

    @vue/cli-shared-utils: 4.5.9

    @vue/component-compiler-utils: 3.2.0 (3.2.0)

    @vue/preload-webpack-plugin: 1.1.2

    @vue/shared: ^3.0.4 => 3.0.5

    @vue/web-component-wrapper: 1.2.0

    mpvue-page-factory: 1.0.1

    mpvue-template-compiler: 1.0.13

    uni-h5-vue: 2.6.10

    uni-mp-vue: 2.6.10

    vue: ^2.6.11 => 2.6.12

    vue-hot-reload-api: 2.3.4

    vue-jsonp: ^2.0.0 => 2.0.0

    vue-loader: 15.9.6 (16.1.2, 15.9.6)

    vue-pdf: ^4.2.0 => 4.2.0

    vue-resize-sensor: 2.0.0

    vue-router: 3.0.1

    vue-style-loader: 4.1.2

    vue-template-compiler: ^2.6.11 => 2.6.12 (2.6.12)

    vue-template-es2015-compiler: 1.9.1

    vuex: ^3.6.0 => 3.6.0

    npmGlobalPackages:

    @vue/cli: 4.5.4

    5 天前

dujun_h5@163.com

dujun_h5@163.com

官方太狠了,一句话也不提示,我这一上线直接报问题,大白屏啊,真够可以的,吓人不吓人

据说是webpack2不支持import,esports混用,不知道是不是这样,给个解释好吗

  • dujun_h5@163.com

    注释掉确实可行

    babel.config.js

    module.exports = {

    presets: [

    [

    '@vue/app',

    {

    modules: 'commonjs',

    useBuiltIns: process.env.UNI_PLATFORM === 'h5' ? 'usage' : 'entry'

    }

    ]

    ],

    plugins

    }

    6 天前

  • DCloud_UNI_GSQ

    目前分析是昨天 @babel/runtime 官方更新导致,降回 7.12.x 也可解决

    6 天前

  • 270315475@qq.com

    回复 DCloud_UNI_GSQ: 没安装什么@babel/runtime 呀

    5 天前

  • DCloud_UNI_GSQ

    回复 270315475@qq.com: 看置顶回复

    5 天前

270315475@qq.com

270315475@qq.com - 接单-APP、小程序高性能接单,QQ:270315475、wx:jin_biao

"dependencies": {
"@dcloudio/uni-app-plus": "^2.0.0-alpha-29920201203001",
"@dcloudio/uni-h5": "^2.0.0-alpha-29920201203001",
"@dcloudio/uni-helper-json": "^1.0.8",
"@dcloudio/uni-mp-360": "^2.0.0-alpha-29920201203001",
"@dcloudio/uni-mp-alipay": "^2.0.0-alpha-29920201203001",
"@dcloudio/uni-mp-baidu": "^2.0.0-alpha-29920201203001",
"@dcloudio/uni-mp-qq": "^2.0.0-alpha-29920201203001",
"@dcloudio/uni-mp-toutiao": "^2.0.0-alpha-29920201203001",
"@dcloudio/uni-mp-vue": "^2.0.0-alpha-29920201203001",
"@dcloudio/uni-mp-weixin": "^2.0.0-alpha-29920201203001",
"@dcloudio/uni-quickapp-native": "^2.0.0-alpha-29920201203001",
"@dcloudio/uni-quickapp-webview": "^2.0.0-alpha-29920201203001",
"@dcloudio/uni-stat": "^2.0.0-alpha-29220200930001",
"@vue/shared": "^3.0.4",
"better-scroll": "^2.0.6",
"core-js": "^3.8.1",
"cos-js-sdk-v5": "^1.1.2",
"cos-wx-sdk-v5": "^1.0.4",
"flyio": "^0.6.2",
"jweixin-module": "^1.6.0",
"regenerator-runtime": "^0.12.1",
"tim-js-sdk": "^2.8.5",
"tim-wx-sdk": "^2.8.5",
"uview-ui": "^1.8.3",
"vconsole": "^3.3.4",
"vue": "^2.6.11",
"vue-jsonp": "^2.0.0",
"vue-pdf": "^4.2.0",
"vuex": "^3.6.0",
"weixin-js-sdk": "^1.6.0"
},
"devDependencies": {
"@dcloudio/types": "^2.0.19",
"@dcloudio/uni-automator": "^2.0.0-alpha-29920201203001",
"@dcloudio/uni-cli-shared": "^2.0.0-alpha-29920201203001",
"@dcloudio/uni-migration": "^2.0.0-alpha-29920201203001",
"@dcloudio/uni-template-compiler": "^2.0.0-alpha-29920201203001",
"@dcloudio/vue-cli-plugin-hbuilderx": "^2.0.0-alpha-29920201203001",
"@dcloudio/vue-cli-plugin-uni": "^2.0.0-alpha-29920201203001",
"@dcloudio/vue-cli-plugin-uni-optimize": "^2.0.0-alpha-29920201203001",
"@dcloudio/webpack-uni-mp-loader": "^2.0.0-alpha-29920201203001",
"@dcloudio/webpack-uni-pages-loader": "^2.0.0-alpha-29920201203001",
"@vue/cli-plugin-babel": "^4.5.8",
"@vue/cli-service": "^4.5.8",
"babel-plugin-import": "^1.13.3",
"cross-env": "^7.0.3",
"jest": "^25.4.0",
"mini-types": "*",
"miniprogram-api-typings": "^3.2.0",
"postcss-comment": "^2.0.0",
"sass": "^1.30.0",
"sass-loader": "^10.1.0",
"vue-template-compiler": "^2.6.11"
},

要回复问题请先登录注册