【问题标题】:Starting the dev server in project generated by vue cli 3在 vue cli 3 生成的项目中启动开发服务器
【发布时间】:2018-10-04 12:38:27
【问题描述】:

我使用 npm i -g @vue/cli 在我的 Windows 系统上全局安装了 vue cli 3。

然后我使用vue create vue-project生成了一个项目

我通过提示选择了所需的插件。

这创建了一个 vue-project 文件夹。然后我将目录更改为该文件夹并运行npm run serve 命令。

但我得到以下错误

PS E:\rk\workspace\vue-project> npm run serve

> vue-project@0.1.0 serve E:\rk\workspace\vue-project
> vue-cli-service serve

INFO  Starting development server...
94% asset optimization

ERROR  Failed to compile with 1 errors                                                                 14:58:40

error  in ./src/main.js

Module build failed: Error: [BABEL] E:\rk\workspace\vue-project\src\main.js: The new decorators proposal is not supported yet. You must pass the `"decoratorsLegacy": true` option to @babel/preset-stage-2 (While processing: "E:\\rk\\workspace\\vue-project\\node_modules\\@vue\\babel-preset-app\\index.js$1")
    at E:\rk\workspace\vue-project\node_modules\@babel\preset-stage-2\lib\index.js:107:11
    at E:\rk\workspace\vue-project\node_modules\@babel\helper-plugin-utils\lib\index.js:18:12
    at E:\rk\workspace\vue-project\node_modules\@babel\core\lib\config\full.js:172:14
    at cachedFunction (E:\rk\workspace\vue-project\node_modules\@babel\core\lib\config\caching.js:42:17)
    at loadPresetDescriptor (E:\rk\workspace\vue-project\node_modules\@babel\core\lib\config\full.js:243:63)
    at E:\rk\workspace\vue-project\node_modules\@babel\core\lib\config\full.js:68:19
    at Array.map (<anonymous>)
    at recurseDescriptors (E:\rk\workspace\vue-project\node_modules\@babel\core\lib\config\full.js:66:36)
    at recurseDescriptors (E:\rk\workspace\vue-project\node_modules\@babel\core\lib\config\full.js:97:26)
    at loadFullConfig (E:\rk\workspace\vue-project\node_modules\@babel\core\lib\config\full.js:112:6)

@ multi (webpack)-dev-server/client/index.js (webpack)/hot/dev-server.js ./src/main.js

我该如何解决这个问题?

【问题讨论】:

  • 你选择了哪些插件?
  • 发生在我身上,只有 vue create 的单元测试(Mocha + Chai)选项。

标签: webpack vue.js vuejs2 babeljs vue-cli


【解决方案1】:

他们各自包的相关维护者都意识到了这个问题。根据这个 Github issuevue create &lt;project-name&gt; 目前已损坏。

我已经尝试了问题中提供的一些解决方案,但没有一个对我有用。你可以试一试,看看它们是否对你有用。

【讨论】:

    【解决方案2】:

    更新:该修复程序现已在 v3.0.0-beta.7 中提供。它目前是一个可选修复,因此您必须将 decoratorsLegacy:true 添加到新生成的 Vue 项目的 .babelrc 中:

    {
      "presets": [
        [
          "@vue/app",
          {
            "decoratorsLegacy": true
          }
        ]
      ]
    }
    

    要修复现有项目,请编辑.babelrc,如上所示,通过将beta.6 替换为beta.7 来更新您的package.json,然后重新运行yarn/npm install

    TLDR:有一个PR (vue-cli #1163) 可以解决这个问题,但IMO 的最佳解决方案是使用.babelrc.js(如下)。


    有一个 GitHub issue comment 表明 @babel/preset-stage-2@7.0.0-beta.44 会有所帮助,但安装它并没有帮助。另一个suggestion.babelrc 中的Babel presets 配置替换为以下内容确实解决了错误:

    {
      "presets": [
        // "@vue/app",  // REMOVE THIS
        ["@babel/preset-env", {
          "targets": {
            "browsers": [
              "> 5%",
              "last 2 versions",
              "not ie <= 8"
            ]
          },
          "modules": false,
          "exclude": [
            "transform-regenerator"
          ]
        }],
        ["@babel/preset-stage-2", {
          "useBuiltIns": true,
          "decoratorsLegacy": true
        }]
      ]
    }
    

    注意@vue/app 预设必须删除,因为它initializes @babel/preset-stage-2 没有所需的属性 (decoratorsLegacy: true)。此解决方案有效,但与 @vue/app 预设中所做的任何潜在改进(包括针对此问题的任何修复)都不向前兼容。

    .babelrc.js

    更向前兼容的解决方法是包装@vue/app 预设并修改@babel/preset-stage-2 的配置。当维护者修复@vue/app 时,我们可以简单地恢复为默认的.babelrc。要使其正常工作,请将 .babelrc 重命名为 .babelrc.js,并将其内容替换为:

    const vueBabelPreset = require('@vue/babel-preset-app');
    
    module.exports = (context, options = {}) => {
      // Cache the returned value forever and don't call this function again.
      context.cache(true);
    
      const {presets, plugins} = vueBabelPreset(context, options);
    
      // Find @babel/preset-stage-2, and update its config to enable `decoratorsLegacy`.
      const presetStage2 = require('@babel/preset-stage-2');
      const preset = presets.find(p => p[0] === presetStage2);
      if (preset) {
        preset[1].decoratorsLegacy = true;
      }
    
      return {
        presets,
        plugins
      };
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-29
      • 2019-01-22
      • 2020-12-21
      • 1970-01-01
      • 1970-01-01
      • 2019-02-25
      相关资源
      最近更新 更多