SyntaxError: expected expression, got '<'

217,804

Solution 1

This code:

app.all('*', function (req, res) {
  res.sendFile(__dirname+'/index.html') /* <= Where my ng-view is located */
})

tells Express that no matter what the browser requests, your server should return index.html. So when the browser requests JavaScript files like jquery-x.y.z.main.js or angular.min.js, your server is returning the contents of index.html, which start with <!DOCTYPE html>, which causes the JavaScript error.

Your code inside the callback should be looking at the request to determine which file to send back, and/or you should be using a different path pattern with app.all. See the routing guide for details.

Solution 2

Try this, usually works for me:

app.set('appPath', 'public');
app.use(express.static(__dirname +'/public'));

app.route('/*')
  .get(function(req, res) {
    res.sendfile(app.get('appPath') + '/index.html');
  });

Where the directory public contains my index.html and references all my angular files in the folder bower_components.

The express.static portion I believe serves the static files correctly (.js and css files as referenced by your index.html). Judging by your code, you will probably need to use this line instead:

app.use(express.static(__dirname));

as it seems all your JS files, index.html and your JS server file are in the same directory.

I think what @T.J. Crowder was trying to say was use app.route to send different files other than index.html, as just using index.html will have your program just look for .html files and cause the JS error.

Hope this works!

Solution 3

The main idea is that somehow HTML has been returned instead of Javascript.

The reason may be:

  • wrong paths
  • assets itself

It may be caused by wrong assets precompilation. In my case, I caught this error because of wrong encoding.

When I opened my application.js I saw application error Encoding::UndefinedConversionError at /assets/application.js

There was full backtrace of error formatted as HTML instead of Javasript.

Make sure that assets had been successfully precompiled.

Solution 4

I had same error. And in my case

REASON: There was restriction to that resources on server and server was sending login page instead of javascript pages.

SOLUTION: give access to user for resourses or remove restriction at all.

Solution 5

You can also get this error message when you place an inline tag in your html but make the (common for me) typo that you forget to add the slash to the closing-script tag like this:

<script>
  alert("I ran!");
<script> <!-- OOPS opened script tag again instead of closing it -->

The JS interpreter tries to "execute" the tag, which looks like an expression beginning with a less-than sign, hence the error: SyntaxError: expected expression, got '<'

Share:
217,804
underscore
Author by

underscore

You wouldn't find me anywhere as underscore but definitely as Samitha Hewawasam in LinkedIn, Facebook. I had hard time with Javascript few years back ( 7 years back ) but now life is full balancing with the JavaScript tasks. Specially React.Js and Node.Js.

Updated on January 22, 2022

Comments

  • underscore
    underscore over 2 years

    I got SyntaxError: expected expression, got '<' error in the console when i'm executing following node code

    var express = require('express');
    var app = express();
    app.all('*', function (req, res) {
      res.sendFile(__dirname+'/index.html') /* <= Where my ng-view is located */
    })
    var server = app.listen(3000, function () {
      var host = server.address().address
      var port = server.address().port
    })
    

    Error :enter image description here

    I'm using Angular Js and it's folder structure like bellow

    enter image description here

    What's I’m missing here ?

  • underscore
    underscore about 9 years
    So, What will be the ideal way to get index.html file ?
  • T.J. Crowder
    T.J. Crowder about 9 years
    @underscore: I just added a link to the routing guide, that describes in detail how to register routes.
  • T.J. Crowder
    T.J. Crowder about 9 years
    @underscore: For the "homepage" of the server (so, probably for your index.html), yes. Then you'll need another route for serving .js and other files (CSS, pages other than /, etc.). Again, the routing guide has the details.
  • Supercreature
    Supercreature over 8 years
    And you also missed the T in the opening tag
  • gigawatt
    gigawatt over 8 years
    Fixed! Thanks Sixtoo :-)
  • nerdess
    nerdess almost 8 years
    thanks for the hint with the wrong paths! the error went away for me when i changed e.g. <script src="js/min/jquery.scrollTo-min.js" type="text/javascript"></script> to <script src="/js/min/jquery.scrollTo-min.js" type="text/javascript"></script> :)
  • Nick Roz
    Nick Roz almost 8 years
    @nerdess, you are welcome. I gave this answer because it occured that the accepted one was not helpful for me
  • Chris Nielsen
    Chris Nielsen about 6 years
    What file does this get added to?
  • Moolshankar Tyagi
    Moolshankar Tyagi over 5 years
    This will be added to main js file which will be called from start script, you can find same in package.json "scripts" -> "start" or similar
  • str028
    str028 over 4 years
    great explination.