The webpack vue-loader throws an error message stating "unexpected token {" when processing a single-page .vue component

As a C# backend developer, I am venturing into the world of Vue.js. I typically work with Visual Studio 2017 + ASP.NET MVC (using it as an API + one layout) + Vue.js + Webpack.
.vue single-page component files are loaded by vue-loader, while .js files are loaded by babel-loader with the es2015 preset.

Although app.js transpiles successfully into the output dist/script.js file using Babel, I keep getting syntax errors with my .vue files no matter what combinations I try. I even face the same error when my navigation.vue file is completely empty:

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

My Task Runner Explorer setup looks like this:

https://i.sstatic.net/hI8d2.png

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 could be causing this puzzling error? How do people usually go about debugging such issues?

Answer №1

The issue might not be stemming from your .vue file, but rather from the vue-loader itself. If you have upgraded to vue-loader >= 13.1 (and possibly one of the vue-loader 12 versions), it is important to have node 6.2 or higher installed on your machine. This is because vue-loader utilizes functionalities that are only present in that version. To check your node version, simply run:

node --version

If updating your node version is not an option, you can attempt to resolve the issue by installing an earlier release of vue-loader through the following command:

npm install <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="123456789abcdef0123456789abcdef">[email protected]</a> --save-dev

With this step, the error should hopefully disappear.

Additionally, it is recommended to transition to using babel-preset-env in place of babel-preset-2015 as the latter is now deprecated or in the process of being deprecated.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Accessing data in JSON format from a URL

I'm working on a website that analyzes data from the game Overwatch. There's this link () that, when visited, displays text in JSON format. Is there a way to use JavaScript to read this data and display it nicely within a <p> tag on my si ...

Slider - incorporating a heading onto a video with HTML styling

Is there a way to display a title on a slider when one of the slides contains a video? Here is an example code snippet: <!-- Swiper--> <div data-height="100vh" data-min-height="480px" data-slide-effect="fade" class="swiper-container swiper-s ...

When deciding between setup() and onMounted() in Vue.js, it is important to consider where to place the code for fetching data from the server to be displayed initially when the page loads

When using the options API, it's clear that we write code for fetching data from the server in the mounted method. However, with the composition API, I find myself a bit confused because the setup method is executed before the onMounted hook. ...

`In a React class, member variables are linked by reference and are considered 'shared'.`

Whenever I create multiple instances of a React class using React.createElement, I notice that some member variables are shared among the instances. It seems like arrays and objects are shared, while strings and booleans are not. This realization is unsett ...

What is the best way to place content in a single div without it being divided into several separate boxes

Here is my code snippet: <div class="col-md-9"> <div id="statbox"> {% for obj in product_type %} {% for obj1 in vastu %} <script type="text/javascript"&g ...

JavaScript error: Undefined variable or function

I'm facing an issue with the following code. When I position the brace in different places, I encounter errors like "var not defined" or "function not defined". My goal is to convert an array into a string so that I can analyze the data and decide how ...

The problem I am facing is with the React Ant d Modal Component, where I am unable to render the

Hey there! I'm currently working with React Js and the 'Ant design Modal Component'. When I click on a button, the modal should open and display the content of each user with their unique ID. However, I'm facing an issue where all modal ...

Difficulty encountered in closing div by clicking the background with the help of jquery

I am facing a challenge with properly closing a div container and restoring it to its original state when I click outside of it. Despite trying various solutions from stackoverflow and extensive internet research, I have been unable to find or come up with ...

When webpack transitions to production mode, the occurrence of undefined .env variables becomes apparent

I'm currently using the dot-env NPM package to pass simple variables to my webpack/express application. However, when I switch to PRODUCTION mode in webpack, all the variables from .env end up becoming undefined. As of now, I have set up separate de ...

What are the steps to implement localStorage in Vuejs3?

I'm feeling a bit lost when it comes to localStorage and its usage. I have a component called Statistic.vue which displays a modal at the end. Statistic.vue <template> <p class='modal'>{{numberOfGames}}</p> </template& ...

Changing the hover color of an Angular Material MD Button

<div class="non-active" layout layout-align='space-around center'> <md-button ng-click="$ctrl.widget.type = 'chart'; $ctrl.validateForm()" layout='column' ng-class="{selIcon : $ctrl.widget.type == ' ...

local individuals and local residents (duplicate) dispatched from the server

Upon analyzing my server's response, I have observed a duplicate of my locals within the locals object. Here is an example: Object { settings: "4.2", env: "development", utils: true, pretty: true, _locals: { settings: ...

Is there a way to execute v-for once the created() lifecycle hook has finished running?

In my current project, I am faced with the challenge of including avatars in notifications. Despite my efforts, I have not been able to solve this issue independently. The Vue.js template below demonstrates how I have attempted to add avatars to each notif ...

How can I halt the execution of the setInterval function in jQuery?

In my code, I have a timer function that triggers when it reaches below 1 minute. It then flashes specific text using a JavaScript function. You can see the initial setup in this jsfiddle: http://jsfiddle.net/8yned9f9/5/ $(document).ready(function(){ ...

Can you provide guidance on how to access and utilize images within vue components?

I am currently in the process of creating a Single Page Application (SPA) using vue.js. I am facing an issue with assigning a background image to a div element by referencing a file in the path below: https://i.stack.imgur.com/qWjaU.png Although I am att ...

Where can the Path be found for Json inside App Phonegap?

Having some trouble with my Phonegap App! Everything seems to be working fine when I test it in my browser. However, once I compile it into an APK and install it on my phone, it's unable to find the JSON data. I'm a beginner in programming and a ...

How can I apply JavaScript to aggregate child node values and assign them to the parent in JSON data format?

I receive a dynamic JSON from the server, which has varying structures. Each data entry consists of a chapter with stages and/or review sets at the root level. If a stage exists, there will be either a review set array or another stage. The review set cont ...

Displaying multiple elements in a grid layout

In our little stock application, we have a list of all available stocks. There is a component called stocks.vue shown in the code below, as well as a stock.vue component. There are currently 4 different stocks listed. The goal is to render them in a 2x2 gr ...

What could be causing my data to not appear in a new row?

Currently, I am facing an issue with how checked checkboxes are displayed. Is there a way to have each checkedbox appear on a new line instead of stacking beside each other in the same line? b-modal#modal-1.d-flex(title='Dodaj leki' hide-footer) ...

Integrate, Delay, Experimentalize, and Attach components

This inquiry might lean more towards a general browser/javascript discussion rather than a focused prototype question, but I believe this community possesses a deep understanding of javascript and browsers. With that said, here is my query: If the followi ...