mde / ejs

Embedded JavaScript templates -- http://ejs.co
Apache License 2.0
7.66k stars 834 forks source link

HTML comment with an embedded EJS scriptlet [<% ... %>] causing error:[SyntaxError: Unexpected token '.' in __dirname+\views\list.ejs while compiling ejs] #734

Closed RahulYavvari closed 1 year ago

RahulYavvari commented 1 year ago

This is the package.json file { "name": "todo-list-v1", "version": "1.0.0", "description": "", "main": "app.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "Rahul", "license": "ISC", "dependencies": { "body-parser": "^1.20.2", "ejs": "^3.1.9", "express": "^4.18.2" } }

With this HTML comment <!-- [<% .. %>] - is called a Scriptlet tag --> line in my list.ejs file in the views folder of project's root directory, when trying to run my server with nodemon app.js, it starts listening on the port 3000, then when trying to load localhost:3000, I am getting an error saying

-------------### [ERROR MESSAGE STARTS HERE]--------------

SyntaxError: Unexpected token '.' in {__dirname}\views\list.ejs while compiling ejs

If the above error is not helpful, you may want to try EJS-Lint: https: //github.com/RyanZim/EJS-Lint Or, if you meant to create an async function, pass async: true as an option. at new Function () at Template.compile ({dirname}\node_modules\ejs\lib\ejs.js:673:12) at Object.compile ({__dirname}\node_modules\ejs\lib\ejs.js:398:16) at handleCache ({dirname}\node_modules\ejs\lib\ejs.js:235:18) at tryHandleCache ({dirname}\node_modules\ejs\lib\ejs.js:274:16) at exports.renderFile [as engine] ({__dirname}\node_modules\ejs\lib\ejs.js:491:10) at View.render ({dirname}\node_modules\express\lib\view.js:135:8) at tryRender ({dirname}\node_modules\express\lib\application.js:657:10) at Function.render ({__dirname}\node_modules\express\lib\application.js:609:3) at ServerResponse.render ({dirname}\node_modules\express\lib\response.js:1039:7)

-------------### [ERROR MESSAGE ENDS HERE]--------------

[References]

app.js image

views/list.ejs image

console image

RyanZim commented 1 year ago

EJS is unaware of any HTML syntax; EJS will still interpolate normally within comments. Obviously .. is not valid Javascript, hence the error.