Nuxt 2.5及更高版本未定义全局变量

时间:2019-07-10 21:17:15

标签: javascript vue.js webpack babeljs nuxt.js

我的应用使用了Nuxt 2.1,然后逐步更新,直到Nuxt 2.4一切正常,但是从2.5到更高的生产版本,出现global is not defined错误。

错误的位置在.nuxt/client.js@26中:

if (!global.fetch) { global.fetch = fetch }

我不知道如何解决此问题(据我所知需要定义global),也没有答案。像这样的错误报告一样,奇怪的是。

依赖项列表:

    "dependencies": {
        "@fortawesome/fontawesome-svg-core": "^1.2.7",
        "@fortawesome/free-regular-svg-icons": "^5.4.2",
        "@fortawesome/free-solid-svg-icons": "^5.4.2",
        "@fortawesome/vue-fontawesome": "^0.1.1",
        "@johmun/vue-tags-input": "^2.0.1",
        "@nuxtjs/auth": "^4.5.3",
        "@nuxtjs/axios": "^5.3.6",
        "@nuxtjs/dotenv": "^1.3.0",
        "@nuxtjs/google-analytics": "^2.2.0",
        "@nuxtjs/moment": "^1.0.0",
        "@saeris/vue-spinners": "^1.0.8",
        "apexcharts": "^2.0.9",
        "axios": "^0.18.0",
        "canvas": "^2.0.1",
        "deep-map": "^2.0.0",
        "deepmerge": "^3.3.0",
        "element-ui": "^2.4.7",
        "glob-all": "^3.1.0",
        "html-element-attributes": "^2.0.0",
        "json-loader": "^0.5.7",
        "laravel-echo": "^1.5.3",
        "lockr": "^0.8.5",
        "lodash": "^4.17.11",
        "moment": "^2.22.2",
        "nib": "^1.1.2",
        "nuxt": "^2.8.1",
        "nuxt-babel": "^1.0.1",
        "nuxt-i18n": "^5.12.7",
        "purgecss-webpack-plugin": "^1.5.0",
        "pusher-js": "^4.4.0",
        "raw-loader": "^3.0.0",
        "rupture": "^0.7.1",
        "s-grid": "^1.2.1",
        "stylus": "^0.54.5",
        "stylus-loader": "^3.0.2",
        "underscore": "^1.9.1",
        "v-viewer": "^1.3.1",
        "vue-apexcharts": "^1.2.1",
        "vue-awesome": "^3.2.0",
        "vue-cookie-law": "^1.10.0",
        "vue-filter": "^0.2.5",
        "vue-js-modal": "^1.3.28",
        "vue-lodash": "^2.0.0",
        "vue-nl2br": "^0.1.1",
        "vue-scrollto": "^2.15.0",
        "vue-the-mask": "^0.11.1",
        "vue-wysiwyg": "^1.7.2",
        "vue2-editor": "^2.6.6",
        "vue2-google-maps": "^0.10.6",
        "vue2-transitions": "^0.2.3",
        "vuedraggable": "^2.16.0",
        "vuejs-datepicker": "^1.5.4"
      },
      "devDependencies": {
        "@babel/core": "^7.5.4",
        "@babel/node": "^7.5.0",
        "@babel/plugin-proposal-export-default-from": "^7.5.2",
        "@babel/plugin-proposal-export-namespace-from": "^7.5.2",
        "@babel/plugin-proposal-object-rest-spread": "^7.5.4",
        "@babel/plugin-proposal-optional-chaining": "^7.2.0",
        "@babel/plugin-syntax-dynamic-import": "^7.2.0",
        "@babel/plugin-transform-runtime": "^7.5.0",
        "@babel/polyfill": "^7.4.4",
        "@babel/preset-env": "^7.5.4",
        "@babel/preset-flow": "^7.0.0",
        "babel-eslint": "^8.2.1",
        "cross-env": "^5.2.0",
        "deep-map-async": "^1.5.1",
        "electron": "2.0.0-beta.7",
        "electron-builder": "^20.8.1",
        "electron-devtools-installer": "^2.2.3",
        "es6-weak-map": "^2.0.3",
        "eslint": "^4.15.0",
        "eslint-friendly-formatter": "^3.0.0",
        "eslint-loader": "^1.7.1",
        "eslint-plugin-vue": "^4.0.0",
        "google-translate-api": "^2.3.0",
        "tosource": "^1.0.0",
        "translatte": "^2.4.3",
        "webpack-node-externals": "^1.7.2",
        "yargs": "^13.2.4"
      }

.babelrc:

{
  "presets": ["@babel/env"],
  "plugins": [
    "@babel/plugin-transform-runtime",
    "@babel/plugin-syntax-dynamic-import",
    "@babel/plugin-proposal-export-default-from",
    "@babel/plugin-proposal-object-rest-spread",
    "@babel/plugin-proposal-optional-chaining",
    "@babel/plugin-proposal-export-namespace-from"
  ]
}

1 个答案:

答案 0 :(得分:0)

经过数小时的调查,我发现问题出在此配置中:

extend(config, {isDev, isClient, isServer}) {
  if (isClient) {
    config.target = process.env.BUILD_TARGET || 'electron-renderer';
  }
})

要知道为什么它们在2.5之前都可以工作,而在之后才可以工作,但是情况就是这样