【问题标题】:Global is not defined in Nuxt 2.5 and higher在 Nuxt 2.5 及更高版本中未定义全局
【发布时间】:2019-11-20 13:21:54
【问题描述】:

我的应用使用 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"
  ]
}

【问题讨论】:

  • 升级前是否删除了 package-lock.json / yarn.lock?我猜你已经看过 notes 的发布了。
  • @Andrew1325 删除了 yarn.lock 并清除了 node_modules,将 nuxt 更新为 2.5 - 相同。开发构建工作,产品 - 没有
  • 什么节点版本?
  • @Aldarund 10.12 但这无关紧要,因为客户端出现错误
  • 应该使用 babel/nuxt 预设。所以我建议删除所有自定义 babel conf 和 deps 并尝试使用默认值。如果它有效 - 你可以自定义 babel nuxtjs.org/api/configuration-build/#babel

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


【解决方案1】:

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

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

知道为什么它在 2.5 之前都能正常工作,但在 2.5 之后就坏了,但情况就是这样

【讨论】:

    猜你喜欢
    • 2014-07-05
    • 1970-01-01
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 2014-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多