this is my first try at using Ejs, i think i got all my syntax right. cant understand whats wrong
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended:true}));
let today = new Date();
let options = {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric"
};
let day = today.toLocaleDateString("en-US", options);
let todoList = ["Sleep","Eat","?"];
app.get("/", function(req, res){
res.render("index", {
day: day,
todoList: todoList
} );
});
app.post("/", function(req, res){
let newInput = req.body.todoInput;
todoList.push(newInput);
});
app.listen(3000, function(){
console.log("Server started on port 3000.");
});
my ejs
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Ra's To-do List</title>
</head>
<body>
<p>
<%= day %> : Today's to-do list</p>
<ul>
<% for (var i=0, i< todoList.length, i++) { %>
<li> <%= todoList[i] %> </li>
<% } %>
</ul>
<form action="/" method="post">
<input type="text" name="todoInput" placeholder="Add to your to-do list here">
<button type="submit" name="button">Add stuff to-do</button>
</form>
</body>
</html>
Im using WSL and have installed all the modules properly. it gives the unexpected token < in .... while piling ejs. ive been at this for a couple of hours now and i might break my laptop soon :)
SyntaxError: Unexpected token < in /mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/views/index.ejs while piling ejs
If the above error is not helpful, you may want to try EJS-Lint: Or, if you meant to create an async function, pass async: true as an option. at new Function () at Templatepile (/mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/node_modules/ejs/lib/ejs.js:618:12) at Objectpile (/mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/node_modules/ejs/lib/ejs.js:389:16) at handleCache (/mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/node_modules/ejs/lib/ejs.js:212:18) at tryHandleCache (/mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/node_modules/ejs/lib/ejs.js:251:16) at View.exports.renderFile [as engine] (/mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/node_modules/ejs/lib/ejs.js:482:10) at View.render (/mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/node_modules/express/lib/view.js:135:8) at tryRender (/mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/node_modules/express/lib/application.js:640:10) at Function.render (/mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/node_modules/express/lib/application.js:592:3) at ServerResponse.render (/mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/node_modules/express/lib/response.js:1008:7)
this is my first try at using Ejs, i think i got all my syntax right. cant understand whats wrong
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended:true}));
let today = new Date();
let options = {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric"
};
let day = today.toLocaleDateString("en-US", options);
let todoList = ["Sleep","Eat","?"];
app.get("/", function(req, res){
res.render("index", {
day: day,
todoList: todoList
} );
});
app.post("/", function(req, res){
let newInput = req.body.todoInput;
todoList.push(newInput);
});
app.listen(3000, function(){
console.log("Server started on port 3000.");
});
my ejs
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Ra's To-do List</title>
</head>
<body>
<p>
<%= day %> : Today's to-do list</p>
<ul>
<% for (var i=0, i< todoList.length, i++) { %>
<li> <%= todoList[i] %> </li>
<% } %>
</ul>
<form action="/" method="post">
<input type="text" name="todoInput" placeholder="Add to your to-do list here">
<button type="submit" name="button">Add stuff to-do</button>
</form>
</body>
</html>
Im using WSL and have installed all the modules properly. it gives the unexpected token < in .... while piling ejs. ive been at this for a couple of hours now and i might break my laptop soon :)
Share Improve this question edited Jan 11, 2019 at 7:48 kRiZ 82510 silver badges14 bronze badges asked Jan 11, 2019 at 5:52 Pravesh RanaPravesh Rana 11 gold badge1 silver badge3 bronze badges 3SyntaxError: Unexpected token < in /mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/views/index.ejs while piling ejs
If the above error is not helpful, you may want to try EJS-Lint: https://github./RyanZim/EJS-Lint Or, if you meant to create an async function, pass async: true as an option. at new Function () at Template.pile (/mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/node_modules/ejs/lib/ejs.js:618:12) at Object.pile (/mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/node_modules/ejs/lib/ejs.js:389:16) at handleCache (/mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/node_modules/ejs/lib/ejs.js:212:18) at tryHandleCache (/mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/node_modules/ejs/lib/ejs.js:251:16) at View.exports.renderFile [as engine] (/mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/node_modules/ejs/lib/ejs.js:482:10) at View.render (/mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/node_modules/express/lib/view.js:135:8) at tryRender (/mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/node_modules/express/lib/application.js:640:10) at Function.render (/mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/node_modules/express/lib/application.js:592:3) at ServerResponse.render (/mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/node_modules/express/lib/response.js:1008:7)
- please post the plete error stack for quick understanding of the problem – kRiZ Commented Jan 11, 2019 at 6:04
- @kRiZ SyntaxError: Unexpected token < in /mnt/c/Users/PraveshRana/Desktop/webDev/ejs-todo/views/index.ejs while piling ejs – Pravesh Rana Commented Jan 11, 2019 at 6:11
- use semicolons ( ; ) instead of mas ( , ) in for loop – HubballiHuli Commented Jan 11, 2019 at 7:56
4 Answers
Reset to default 1I think the problem is just that you are using mas instead of semi-colons in your ejs statement.
So instead of:
<% for (var i=0, i< todoList.length, i++) { %>
It should be:
<% for (var i=0; i< todoList.length; i++) { %>
Cheers, Aidan.
Problem is, "<" less than symbol. Because you can not use on html. You should change html entity, like "& lt;"(delete one space char). And check it google another html entity symbol. Good luck.
I had the same (similar) error in my node-ejs app (SyntaxError: Unexpected token ; ...
). I think general answer for this error is to check your ejs code carefully. Usually there is a syntax error in your codes. For me, I noticed that when I used ejs variables in my code (for example <%= email %>
that email is a variable), I used the wrong syntax like this:
<% email =%>
And after correcting that, the error disappeared. The text shown in this error is somehow illusory in my opinion. The problem actually is not related to async function
and usually the character mentioned in token <
or token ;
or ... is not related to the real syntax error.
In my case, it's because of long string get break, when I use a HTML formatter
<td style="padding-left: 1rem">
<%= dateFns.format(new Date(date), 'long string format date' ) %>
</td>
when formatter is applied
<td style="padding-left: 1rem">
<%= dateFns.format(new Date(date), 'long string
format date' ) %>
</td>
my solution is to have a variable for the date format string
DATE_FORMAT = 'long string format date'
ejs.render('template.html', {...data, DATE_FORMAT})
<td style="padding-left: 1rem">
<%= dateFns.format(new Date(date),DATE_FORMAT ) %>
</td>