Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting the dev server in project generated by vue cli 3

I installed vue cli 3 globally on my windows system using npm i -g @vue/cli.

Then I generated a project using vue create vue-project

I selected the necessary plugins needed via the prompts .

This created a vue-projject foler. Then I changed the directory into that folder and ran npm run serve command.

But i get the following error

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

> [email protected] 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

How shall I rectify this?

like image 862
boomboxboy Avatar asked Apr 24 '18 09:04

boomboxboy


People also ask

What server does Vue cli use?

The CLI Service ( @vue/cli-service ) is a development dependency. It's an npm package installed locally into every project created by @vue/cli . The CLI Service is built on top of webpack and webpack-dev-server.

How do I run a Vue project command?

To launch a VueJs project, you must type "npm run serve".

How do I start Vue cli ui?

Getting Started. We need to install the Vue CLI to be able to use it. To install it, open the command line and run npm install -g @vue/cli if you're using npm, or run yarn global add @vue/cli if you're using yarn. You can verify that it is properly installed by simply running vue .


1 Answers

UPDATE: The fix is now available in v3.0.0-beta.7. It's currently an opt-in fix, so you have to add decoratorsLegacy:true to the .babelrc of a newly generated Vue project:

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

To fix an existing project, edit the .babelrc as shown above, update your package.json by replacing beta.6 with beta.7, and re-run yarn/npm install.

TLDR: There's a PR (vue-cli #1163) to fix the problem, but the best solution IMO is to use .babelrc.js (below).


There's a GitHub issue comment that indicates @babel/[email protected] would help, but installing it did not help. Another suggestion to replace the Babel presets config in .babelrc with the following indeed resolved the error:

{
  "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
    }]
  ]
}

Note the @vue/app preset must be removed because it initializes @babel/preset-stage-2 without the required property (decoratorsLegacy: true). This solution works, but it's not forward compatible with any potential improvements made in the @vue/app preset (including any fixes for this very problem).

.babelrc.js

A more forward compatible workaround is to wrap the @vue/app preset and modify the config for @babel/preset-stage-2. When the maintainers fix @vue/app, we can simply revert to the default .babelrc. To get this working, rename .babelrc to .babelrc.js, and replace its contents with:

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
  };
}
like image 130
tony19 Avatar answered Oct 09 '22 21:10

tony19