SyntaxError: expected expression, got '<'
JavascriptAngularjsnode.jsExpressJavascript Problem Overview
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 :
I'm using Angular Js and it's folder structure like bellow
What's I’m missing here ?
Javascript Solutions
Solution 1 - Javascript
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 - Javascript
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 - Javascript
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 - Javascript
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 - Javascript
You can also get this error message when you place an inline
Solution 6 - Javascript
Just simply add:
app.use(express.static(__dirname +'/app'));
where '/app'
is the directory where your index.html resides or your Webapp.
Solution 7 - Javascript
I had this same error when I migrated a Wordpress site to another server. The URL in the header for my js scripts was still pointing to the old server and domain name.
Once I updated the domain name, the error went away.
Solution 8 - Javascript
I had a similar problem using Angular js. i had a rewrite all to index.html in my .htaccess.
The solution was to add the correct path slashes in
Solution 9 - Javascript
This problem occurs when your are including file with wrong path and server gives some 404 error in loading file.
Solution 10 - Javascript
This should work for you. If you are using SPA.
app.use('/', express.static(path.join(__dirname, 'your folder')));
// Send all other requests to the SPA
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'your folder/index.html'));
});
Solution 11 - Javascript
I was getting this error when trying to access an angular SPA via ASP.NET Core. Turns out the environment variables were not being loaded correctly, and the following line was not being called:
app.UseSpaStaticFiles();
After I ensured the line was getting called, the app started working again.
Solution 12 - Javascript
I got this type question on Django, and My issue is forget to add static
to the <script>
tag.
Such as in my template:
<script type="text/javascript" src="css/layer.js"></script>
I should add the static
to it like this:
<script type="text/javascript" src="{% static 'css/layer.js' %}" ></script>
Solution 13 - Javascript
May be this helps someone. The error I was getting was
> SyntaxError: expected expression, got '<'
What I did clicked on view source in chrome and there I found this error
> Notice: Undefined variable: string in
> D:\Projects\demo\ge.php on line 66
The issue was this snippet of code,
$string= ''; <---- this should be declared outside the loop
while($row = mysql_fetch_assoc($result))
{
$string .= '{ id:'.$row['menu_id'].', pId:'.$row['parent_id'].', name:"'.$row['menu_name'].'"},';
}
which fixed the problem.