Express.js, Unexpected token <
There seems to be a loopback occuring, since the *
rule is serving a index.html
every time, and when bundle.js
is not found, it will serve index.html
instead, thus trying to parse <
as javascript.
A sort of index-ception if you will...
Step 1
Folder structure:
public -> index.html -> bundle.js
Step 2
Configure static path like this
app.use(express.static('public'))
Step 3
Ensure if it is properly configured .Go to browser and access your js file directly like
localhost:3000/bundle.js
If you see your javascript ...Hurrray. If not then fight with step 2 then check step 3
Step 4
use same path in script tag in ui like
<script src="/bundle.js" type="text/javascript"></script>
I fixed the same issue by adding the following line:
app.use(express.static(path.join(__dirname, "public")));