4

I am primarily a C# backend developer and trying to learn Vue.js. I use Visual Studio 2017 + ASP.NET MVC (as API + one layout) + Vue.js + Webpack.
.vue single-page component files are loaded by vue-loader, and .js files are loaded by babel-loader with es2015 preset.

app.js is transpiled successfully into output dist/script.js file by Babel, but .vue files give me syntax errors whichever combinations I use. I have the same error even if my navigation.vue error is absolutely empty:

ERROR in ./assets/component/navigation.vue
Module build failed: SyntaxError: Unexpected token {

Task Runner Explorer content:

enter image description here

nagivation.vue:

<template>
    <div>
        {{ greeting }}
    </div>
</template>

<script>
    export default {
        data: {
            greeting: 'Hello World'
        }
    }
</script>

app.js:

import Vue from "../vendor/vue.js";

Vue.component("navigation", require("../component/navigation.vue"));

const app = new Vue({
    el: "#app"
}); 

webpack.config.js:

module.exports = {
    entry: "./assets/core/app.js",
    output: {
        filename: "./dist/script.js"
    },
    module: {
        loaders: [
            {
                test: /\.js$/,
                loader: "babel-loader",
                exclude: /node_modules/,
                options: {
                    presets: ["es2015"]
                }
            },
            {
                test: /\.vue$/,
                loader: "vue-loader"
            }
        ]
    },
    resolve: {
      extensions: ["*", ".js", ".vue"]  
    },
    plugins: [
        new NotifierPlugin()
    ]
};

package.json:

{
  "version": "1.0.0",
  "name": "helloworld",
  "private": true,
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-vue": "^1.2.1",
    "clean-webpack-plugin": "^0.1.17",
    "webpack": "^3.8.1"
  },
  "dependencies": {
    "babel-core": "^6.26.0",
    "babel-eslint": "^8.0.2",
    "babel-loader": "^7.1.2",
    "babel-preset-env": "^1.6.1",
    "css-loader": "^0.28.7",
    "eslint": "^4.10.0",
    "eslint-cli": "^1.1.1",
    "eslint-loader": "^1.9.0",
    "eslint-plugin-react": "^7.4.0",
    "vue-loader": "^13.5.0",
    "vue-router": "^3.0.1",
    "vue-template-compiler": "^2.5.3",
    "webpack-notifier": "^1.5.0"
  }
}

What can be a cause of such cryptic error? How people usually debug such errors?

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
  • You should have installed the webpack template for vue https://github.com/vuejs-templates/webpack it was made so people wouldn't run over configuring bare webpack and vue as it's bound to happen. – samayo Nov 10 '17 at 12:13
  • @samayo I've tried to, but this template is like overkill. I don't need node.js hosting and many other things it has. Also, I want to understand all these things and that's why I need to do it from scratch, step-by-step by myself, without any premade templates. However, I will look over configuration files of this template, thank you very much! :) – Yeldar Kurmangaliyev Nov 10 '17 at 13:16

1 Answers1

3

The error likely isn't coming from your .vue file but from vue-loader itself. If you are using vue-loader >= 13.1 (and possibly one of the vue-loader 12 versions) then you will need to ensure you have node 6.2 or above on your machine, because vue-loader uses features that only became available in that version. You can check your node version by running:

node --version

If you can't update your node version then try installing one of the earlier releases of vue-loader by doing:

npm install vue-loader@13.0.1 --save-dev

And hopefully the error should go away.

As a side note, you should also start using babel-preset-env rather than babel-preset-2015 as that has now been (or is being) deprecated.

craig_h
  • 31,871
  • 6
  • 59
  • 68
  • Thank you! I will check this out tomorrow. – Yeldar Kurmangaliyev Nov 10 '17 at 13:55
  • Seems like it was node.js version issue. I have removed Visual Studio pre-installed node.js directory from options and have added `C:\Program Files\nodejs` with the latest version using [this instruction](https://stackoverflow.com/questions/43849585/update-node-version-in-visual-studio-2017) and it now works! Thank you again :) – Yeldar Kurmangaliyev Nov 13 '17 at 04:24
  • Downgrading from `vue-loader@13.5.0` to `vue-loader@13.0.1` helped to resolve my problem. Thanks! – Desprit Nov 25 '17 at 16:01