Bootstrap

vue-cli创建uni-app项目 exports is not defined 解决方法

错误描述:exports is not defined

错误原因:更新uni-app 的时候一并更新了@babel/runtime ,@babel/runtime 更新到了 7.13.x,有较大改动,引起兼容问题。

解决办法:

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

2.注释掉babel.config.js 文件中modules: 'commonjs', 语句,代码如下

const plugins = []

if (process.env.UNI_OPT_TREESHAKINGNG) {
  plugins.push(require('@dcloudio/vue-cli-plugin-uni-optimize/packages/babel-plugin-uni-api/index.js'))
}

if (
  (
    process.env.UNI_PLATFORM === 'app-plus' &&
    process.env.UNI_USING_V8
  ) ||
  (
    process.env.UNI_PLATFORM === 'h5' &&
    process.env.UNI_H5_BROWSER === 'builtin'
  )
) {
  const path = require('path')

  const isWin = /^win/.test(process.platform)

  const normalizePath = path => (isWin ? path.replace(/\\/g, '/') : path)

  const input = normalizePath(process.env.UNI_INPUT_DIR)
  try {
    plugins.push([
      require('@dcloudio/vue-cli-plugin-hbuilderx/packages/babel-plugin-console'),
      {
        file (file) {
          file = normalizePath(file)
          if (file.indexOf(input) === 0) {
            return path.relative(input, file)
          }
          return false
        }
      }
    ])
  } catch (e) {}
}

process.UNI_LIBRARIES = process.UNI_LIBRARIES || ['@dcloudio/uni-ui']
process.UNI_LIBRARIES.forEach(libraryName => {
  plugins.push([
    'import',
    {
      'libraryName': libraryName,
      'customName': (name) => {
        return `${libraryName}/lib/${name}/${name}`
      }
    }
  ])
})

const config = {
  presets: [
    [
      '@vue/app',
      {
        //modules: 'commonjs',
        useBuiltIns: process.env.UNI_PLATFORM === 'h5' ? 'usage' : 'entry'
      }
    ]
  ],
  plugins
}

const UNI_H5_TEST = '**/@dcloudio/uni-h5/dist/index.umd.min.js'
if (process.env.NODE_ENV === 'production') {
  config.overrides = [{
    test: UNI_H5_TEST,
    compact: true,
  }]
} else {
  config.ignore = [UNI_H5_TEST]
}

module.exports = config

;