Unexpected token < error in react router component

The “unexpected token” error can show up for a number of different reasons. I ran into a similar problem, and in my case the problem was that the script tag to load the generated bundle in the html was like so:

<script src="https://stackoverflow.com/questions/29718481/scripts/app.js"></script>

When navigating to a route with a parameter (same thing will happen to a nested route or a route with more than one segment), browser would try to load the script using the wrong url. In my case the route path was ‘user/:id’, and the browser made a request to ‘http://127.0.0.1:3000/user/scripts/app.js’ instead of ‘http://127.0.0.1:3000/scripts/app.js‘. The solution was easy, change the script tag to this:

<script src="https://stackoverflow.com/scripts/app.js"></script>

Leave a Comment