Intermittent "TypeError: "exports" is read-only" error with Webpack and Vue-CLI

Intermittent "TypeError: "exports" is read-only" error with Webpack and Vue-CLI

我有一个 Vue-CLI 应用程序,直到最近它都运行良好。现在,有时此消息会出现在控制台中,并且应用程序的其余部分无法加载:

TypeError: "exports" is read-only

直接原因似乎是我的模块之一,它使用 module.exports 导出其默认功能。 I understand that Webpack (which Vue CLI uses) reduced support for module.exports, at least in the case of a module which also contains ES2015 import statements. But that's not the case here。而且 Webpack 有时可以很好地编译它。

特别奇怪的是它是断断续续的。通常我可以通过 rm -rf node_modules; npm install 让问题暂时消失。 (纱线安装似乎不那么可靠)。但它又回来了。

可能是什么原因?也许是两个相互竞争的依赖关系?我的 package.json 看起来像这样:

  "dependencies": {
    "@turf/turf": "^5.1.6",
    "bluebird": "^3.5.3",
    "color": "^3.1.0",
    "mapbox-gl": "^0.50.0",
    "mapbox-gl-utils": "^0.4.0",
    "thenify": "^3.3.0",
    "vue": "^2.5.17",
    "vue-carousel": "^0.16.0-rc2"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "^3.0.5",
    "@vue/cli-plugin-eslint": "^3.2.1",
    "@vue/cli-service": "^3.0.5",
    "babel-eslint": "^10.0.1",
    "babel-loader": "^8.0.4",
    "copy-webpack-plugin": "^4.6.0",
    "eslint": "^5.9.0",
    "eslint-plugin-vue": "^5.0.0",
    "less": "^3.9.0",
    "less-loader": "^4.1.0",
    "pug": "^2.0.3",
    "pug-plain-loader": "^1.0.0",
    "vue-template-compiler": "^2.5.17"
  },
  "postcss": {
    "plugins": {
      "autoprefixer": {}
    }
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not ie <= 8"
  ]
}

我的 vue.config.js 是(简体):

const CopyWebpackPlugin = require('copy-webpack-plugin')
const path = require('path');

module.exports = {
  chainWebpack: config => {
    config.module
      .rule('eslint')
      .use('eslint-loader')
      .tap(options => {
        options.configFile = path.resolve(__dirname, ".eslintrc.js");
        return options;
      })
  },
  configureWebpack: {
    plugins: [
      new CopyWebpackPlugin([ 
        { ... }          
    ]
  }
}

我怀疑,但不确定,问题是在我更新我的模块时触发的,该模块使用 npm link 链接。

使用 Vue CLI 版本 2.1.1。

作为解决方法,如果我改用 ES2015 export 语句,是的,该应用程序可以运行,但我无法 运行 我的 NodeJS 测试套件。

我希望得到有关如何使我的环境更稳定以便不再出现此间歇性问题的任何建议。

根据 https://github.com/vuejs/vue-cli/issues/3227,这是由于一些可配置的行为。将此添加到您的 vue.config.js:

module.exports = {
  chainWebpack: (config) => {
    config.resolve.symlinks(false)
  }
}

它对我有效。

即使在添加

之后,我也 运行 遇到了这个问题
chainWebpack: (config) => {
  config.resolve.symlinks(false)
}

我的vue.config.js

为了解决,我删除了我的 node_modules 文件夹和 运行 一个新的 npm install