close

[Solved] vue cli – Uncaught SyntaxError: Unexpected token <

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error vue cli – Uncaught SyntaxError: Unexpected token < in vueJs. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How vue cli – Uncaught SyntaxError: Unexpected token < Error Occurs?

I create my project with vue-cli 3.0. Initially, it runs ok. But after I <ctrl>-c then npm run serve again, it keeps throwing errors.

Uncaught SyntaxError: Unexpected token <

How To Solve vue cli – Uncaught SyntaxError: Unexpected token < Error?

  1. How To Solve vue cli – Uncaught SyntaxError: Unexpected token

    To Solve vue cli – Uncaught SyntaxError: Unexpected token < Try by adding <base href="/" /> into the <head> of your index.html. Hope it will work.

  2. vue cli – Uncaught SyntaxError: Unexpected token

    To Solve vue cli – Uncaught SyntaxError: Unexpected token < Try by adding <base href="/" /> into the <head> of your index.html. Hope it will work.

Solution 1

Try by adding <base href="/" /> into the <head> of your index.html. Hope it will work.

Solution 2

You might have used “./” prefix for Relative path in src attributes of your index.html, Just replace it with “<%= BASE_URL %>” and it will work fine.

//<script src="./js/config.js">
<script src="<%= BASE_URL %>js/config.js">

This “./” usage won’t cause any problems with normal Vue Routing but when you are implementing Dynamic Route Matching ({ path: ‘/user/:id’, component: User }) your references in the index.html with “./” prefix won’t work as expected!

This is because Vue file structure will place your dependencies inside your route folder like

user/js/config.js

This structure will be the same, Even when your route has route params (http://yourdomain.com/user/1234) since you have Dynamic Route Matching implemented and using the same Vue component (User.vue).

At this time, “./” might point for user/1234/js/config.js so it ends up going to a default 404 page and it’s an HTML file that starts with “< html >”, so you are getting “Uncaught SyntaxError: Unexpected token <“ in line 1 of your referenced file.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read

Leave a Comment